1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { AuthService, Notifier, RedirectService, ServerService } from '@app/core'
3 import { UserService, UserValidatorsService } from '@app/shared'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { UserRegister } from '@shared/models/users/user-register.model'
6 import { FormGroup } from '@angular/forms'
7 import { About, ServerConfig } from '@shared/models/server'
8 import { InstanceService } from '@app/shared/instance/instance.service'
9 import { HooksService } from '@app/core/plugins/hooks.service'
10 import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
11 import { ActivatedRoute } from '@angular/router'
14 selector: 'my-register',
15 templateUrl: './register.component.html',
16 styleUrls: [ './register.component.scss' ]
18 export class RegisterComponent implements OnInit {
19 @ViewChild('accordion', { static: true }) accordion: NgbAccordion
23 success: string = null
31 moderationInformation: '',
35 formStepUser: FormGroup
36 formStepChannel: FormGroup
38 private serverConfig: ServerConfig
41 private route: ActivatedRoute,
42 private authService: AuthService,
43 private userValidatorsService: UserValidatorsService,
44 private notifier: Notifier,
45 private userService: UserService,
46 private serverService: ServerService,
47 private redirectService: RedirectService,
48 private instanceService: InstanceService,
49 private hooks: HooksService,
54 get requiresEmailVerification () {
55 return this.serverConfig.signup.requiresEmailVerification
59 this.serverConfig = this.route.snapshot.data.serverConfig
61 this.instanceService.getAbout()
66 this.aboutHtml = await this.instanceService.buildHtml(about)
69 err => this.notifier.error(err.message)
72 this.hooks.runAction('action:signup.register.init', 'signup')
75 hasSameChannelAndAccountNames () {
76 return this.getUsername() === this.getChannelName()
80 if (!this.formStepUser) return undefined
82 return this.formStepUser.value['username']
86 if (!this.formStepChannel) return undefined
88 return this.formStepChannel.value['name']
91 onUserFormBuilt (form: FormGroup) {
92 this.formStepUser = form
95 onChannelFormBuilt (form: FormGroup) {
96 this.formStepChannel = form
100 if (this.accordion) this.accordion.toggle('terms')
103 onCodeOfConductClick () {
104 if (this.accordion) this.accordion.toggle('code-of-conduct')
110 const body: UserRegister = await this.hooks.wrapObject(
111 Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value }),
113 'filter:api.signup.registration.create.params'
116 this.userService.signup(body).subscribe(
118 this.signupDone = true
120 if (this.requiresEmailVerification) {
121 this.info = this.i18n('Now please check your emails to verify your account and complete signup.')
126 this.authService.login(body.username, body.password)
129 this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username })
132 err => this.error = err.message
136 err => this.error = err.message