1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { FormGroup } from '@angular/forms'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, Notifier, UserService } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { InstanceService } from '@app/shared/shared-instance'
7 import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { UserRegister } from '@shared/models'
10 import { About, ServerConfig } from '@shared/models/server'
13 selector: 'my-register',
14 templateUrl: './register.component.html',
15 styleUrls: [ './register.component.scss' ]
17 export class RegisterComponent implements OnInit {
18 @ViewChild('accordion', { static: true }) accordion: NgbAccordion
22 success: string = null
30 moderationInformation: '',
34 formStepUser: FormGroup
35 formStepChannel: FormGroup
37 private serverConfig: ServerConfig
40 private route: ActivatedRoute,
41 private authService: AuthService,
42 private notifier: Notifier,
43 private userService: UserService,
44 private instanceService: InstanceService,
45 private hooks: HooksService,
50 get requiresEmailVerification () {
51 return this.serverConfig.signup.requiresEmailVerification
55 this.serverConfig = this.route.snapshot.data.serverConfig
57 this.instanceService.getAbout()
62 this.aboutHtml = await this.instanceService.buildHtml(about)
65 err => this.notifier.error(err.message)
68 this.hooks.runAction('action:signup.register.init', 'signup')
71 hasSameChannelAndAccountNames () {
72 return this.getUsername() === this.getChannelName()
76 if (!this.formStepUser) return undefined
78 return this.formStepUser.value['username']
82 if (!this.formStepChannel) return undefined
84 return this.formStepChannel.value['name']
87 onUserFormBuilt (form: FormGroup) {
88 this.formStepUser = form
91 onChannelFormBuilt (form: FormGroup) {
92 this.formStepChannel = form
96 if (this.accordion) this.accordion.toggle('terms')
99 onCodeOfConductClick () {
100 if (this.accordion) this.accordion.toggle('code-of-conduct')
106 const body: UserRegister = await this.hooks.wrapObject(
107 Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value }),
109 'filter:api.signup.registration.create.params'
112 this.userService.signup(body).subscribe(
114 this.signupDone = true
116 if (this.requiresEmailVerification) {
117 this.info = this.i18n('Now please check your emails to verify your account and complete signup.')
122 this.authService.login(body.username, body.password)
125 this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username })
128 err => this.error = err.message
132 err => this.error = err.message