]>
Commit | Line | Data |
---|---|---|
421d935d | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
f8b2c1b4 | 2 | import { AuthService, Notifier, RedirectService, ServerService } from '@app/core' |
b247a132 | 3 | import { UserService, UserValidatorsService } from '@app/shared' |
b1d40cff | 4 | import { I18n } from '@ngx-translate/i18n-polyfill' |
1d5342ab C |
5 | import { UserRegister } from '@shared/models/users/user-register.model' |
6 | import { FormGroup } from '@angular/forms' | |
ba430d75 | 7 | import { About, ServerConfig } from '@shared/models/server' |
421d935d | 8 | import { InstanceService } from '@app/shared/instance/instance.service' |
ba7b7e57 | 9 | import { HooksService } from '@app/core/plugins/hooks.service' |
421d935d | 10 | import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap' |
ba430d75 | 11 | import { ActivatedRoute } from '@angular/router' |
a184c71b C |
12 | |
13 | @Component({ | |
b247a132 C |
14 | selector: 'my-register', |
15 | templateUrl: './register.component.html', | |
16 | styleUrls: [ './register.component.scss' ] | |
a184c71b | 17 | }) |
421d935d C |
18 | export class RegisterComponent implements OnInit { |
19 | @ViewChild('accordion', { static: true }) accordion: NgbAccordion | |
20 | ||
d8c9996c | 21 | info: string = null |
df98563e | 22 | error: string = null |
1d5342ab | 23 | success: string = null |
d8c9996c | 24 | signupDone = false |
a184c71b | 25 | |
421d935d C |
26 | about: About |
27 | aboutHtml = { | |
28 | description: '', | |
29 | terms: '', | |
30 | codeOfConduct: '', | |
31 | moderationInformation: '', | |
32 | administrator: '' | |
33 | } | |
34 | ||
1d5342ab C |
35 | formStepUser: FormGroup |
36 | formStepChannel: FormGroup | |
37 | ||
ba430d75 C |
38 | private serverConfig: ServerConfig |
39 | ||
df98563e | 40 | constructor ( |
ba430d75 | 41 | private route: ActivatedRoute, |
43e9d2af | 42 | private authService: AuthService, |
e309822b | 43 | private userValidatorsService: UserValidatorsService, |
f8b2c1b4 | 44 | private notifier: Notifier, |
5afdd0a5 | 45 | private userService: UserService, |
d9eaee39 | 46 | private serverService: ServerService, |
b1d40cff | 47 | private redirectService: RedirectService, |
421d935d | 48 | private instanceService: InstanceService, |
ba7b7e57 | 49 | private hooks: HooksService, |
b1d40cff | 50 | private i18n: I18n |
a184c71b | 51 | ) { |
8a19bee1 C |
52 | } |
53 | ||
d9eaee39 | 54 | get requiresEmailVerification () { |
ba430d75 | 55 | return this.serverConfig.signup.requiresEmailVerification |
d9eaee39 JM |
56 | } |
57 | ||
421d935d | 58 | ngOnInit (): void { |
ba430d75 C |
59 | this.serverConfig = this.route.snapshot.data.serverConfig |
60 | ||
421d935d C |
61 | this.instanceService.getAbout() |
62 | .subscribe( | |
63 | async about => { | |
64 | this.about = about | |
65 | ||
66 | this.aboutHtml = await this.instanceService.buildHtml(about) | |
67 | }, | |
68 | ||
69 | err => this.notifier.error(err.message) | |
70 | ) | |
ba7b7e57 RK |
71 | |
72 | this.hooks.runAction('action:signup.register.init', 'signup') | |
421d935d C |
73 | } |
74 | ||
1d5342ab C |
75 | hasSameChannelAndAccountNames () { |
76 | return this.getUsername() === this.getChannelName() | |
77 | } | |
78 | ||
79 | getUsername () { | |
80 | if (!this.formStepUser) return undefined | |
81 | ||
82 | return this.formStepUser.value['username'] | |
83 | } | |
84 | ||
85 | getChannelName () { | |
86 | if (!this.formStepChannel) return undefined | |
87 | ||
88 | return this.formStepChannel.value['name'] | |
89 | } | |
90 | ||
91 | onUserFormBuilt (form: FormGroup) { | |
92 | this.formStepUser = form | |
93 | } | |
94 | ||
95 | onChannelFormBuilt (form: FormGroup) { | |
96 | this.formStepChannel = form | |
a184c71b C |
97 | } |
98 | ||
421d935d C |
99 | onTermsClick () { |
100 | if (this.accordion) this.accordion.toggle('terms') | |
101 | } | |
102 | ||
103 | onCodeOfConductClick () { | |
104 | if (this.accordion) this.accordion.toggle('code-of-conduct') | |
105 | } | |
106 | ||
ba7b7e57 | 107 | async signup () { |
df98563e | 108 | this.error = null |
a184c71b | 109 | |
ba7b7e57 RK |
110 | const body: UserRegister = await this.hooks.wrapObject( |
111 | Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value }), | |
0912f1b4 | 112 | 'signup', |
ba7b7e57 RK |
113 | 'filter:api.signup.registration.create.params' |
114 | ) | |
a184c71b | 115 | |
1d5342ab | 116 | this.userService.signup(body).subscribe( |
a184c71b | 117 | () => { |
d8c9996c C |
118 | this.signupDone = true |
119 | ||
d9eaee39 | 120 | if (this.requiresEmailVerification) { |
1d5342ab | 121 | this.info = this.i18n('Now please check your emails to verify your account and complete signup.') |
d8c9996c | 122 | return |
d9eaee39 | 123 | } |
d8c9996c | 124 | |
43e9d2af | 125 | // Auto login |
1d5342ab | 126 | this.authService.login(body.username, body.password) |
43e9d2af C |
127 | .subscribe( |
128 | () => { | |
1d5342ab | 129 | this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username }) |
43e9d2af C |
130 | }, |
131 | ||
132 | err => this.error = err.message | |
133 | ) | |
a184c71b C |
134 | }, |
135 | ||
f7354483 | 136 | err => this.error = err.message |
df98563e | 137 | ) |
a184c71b C |
138 | } |
139 | } |