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