]>
Commit | Line | Data |
---|---|---|
6f03f944 C |
1 | import { CdkStep } from '@angular/cdk/stepper' |
2 | import { Component, OnInit, ViewChild } from '@angular/core' | |
1d5342ab | 3 | import { FormGroup } from '@angular/forms' |
67ed6552 | 4 | import { ActivatedRoute } from '@angular/router' |
d92d070c | 5 | import { AuthService } from '@app/core' |
ba7b7e57 | 6 | import { HooksService } from '@app/core/plugins/hooks.service' |
d92d070c | 7 | import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' |
67ed6552 | 8 | import { UserRegister } from '@shared/models' |
40360c17 | 9 | import { ServerConfig } from '@shared/models/server' |
9589907c | 10 | import { SignupService } from '../shared/signup.service' |
a184c71b C |
11 | |
12 | @Component({ | |
b247a132 C |
13 | selector: 'my-register', |
14 | templateUrl: './register.component.html', | |
15 | styleUrls: [ './register.component.scss' ] | |
a184c71b | 16 | }) |
421d935d | 17 | export class RegisterComponent implements OnInit { |
6f03f944 | 18 | @ViewChild('lastStep') lastStep: CdkStep |
ed22eaab | 19 | @ViewChild('instanceAboutAccordion') instanceAboutAccordion: InstanceAboutAccordionComponent |
6f03f944 C |
20 | |
21 | signupError: string | |
22 | signupSuccess = false | |
a184c71b | 23 | |
09c55770 | 24 | videoUploadDisabled: boolean |
6f03f944 | 25 | videoQuota: number |
09c55770 | 26 | |
40360c17 | 27 | formStepTerms: FormGroup |
1d5342ab C |
28 | formStepUser: FormGroup |
29 | formStepChannel: FormGroup | |
30 | ||
40360c17 K |
31 | aboutHtml = { |
32 | codeOfConduct: '' | |
33 | } | |
34 | ||
35 | instanceInformationPanels = { | |
36 | codeOfConduct: true, | |
37 | terms: true, | |
38 | administrators: false, | |
39 | features: false, | |
40 | moderation: false | |
41 | } | |
42 | ||
4c8a0991 C |
43 | defaultPreviousStepButtonLabel = $localize`Go to the previous step` |
44 | defaultNextStepButtonLabel = $localize`Go to the next step` | |
40360c17 K |
45 | stepUserButtonLabel = this.defaultNextStepButtonLabel |
46 | ||
bd898dd7 C |
47 | signupDisabled = false |
48 | ||
ba430d75 C |
49 | private serverConfig: ServerConfig |
50 | ||
df98563e | 51 | constructor ( |
ba430d75 | 52 | private route: ActivatedRoute, |
43e9d2af | 53 | private authService: AuthService, |
9589907c | 54 | private signupService: SignupService, |
66357162 | 55 | private hooks: HooksService |
9df52d66 | 56 | ) { } |
8a19bee1 | 57 | |
d9eaee39 | 58 | get requiresEmailVerification () { |
ba430d75 | 59 | return this.serverConfig.signup.requiresEmailVerification |
d9eaee39 JM |
60 | } |
61 | ||
9589907c C |
62 | get requiresApproval () { |
63 | return this.serverConfig.signup.requiresApproval | |
64 | } | |
65 | ||
1f256e7d P |
66 | get minimumAge () { |
67 | return this.serverConfig.signup.minimumAge | |
68 | } | |
69 | ||
6f03f944 C |
70 | get instanceName () { |
71 | return this.serverConfig.instance.name | |
72 | } | |
73 | ||
74 | ngOnInit () { | |
ba430d75 C |
75 | this.serverConfig = this.route.snapshot.data.serverConfig |
76 | ||
bd898dd7 C |
77 | if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) { |
78 | this.signupDisabled = true | |
79 | return | |
80 | } | |
81 | ||
6f03f944 C |
82 | this.videoQuota = this.serverConfig.user.videoQuota |
83 | this.videoUploadDisabled = this.videoQuota === 0 | |
84 | ||
40360c17 | 85 | this.stepUserButtonLabel = this.videoUploadDisabled |
e0fea785 | 86 | ? $localize`:Button on the registration form to finalize the account and channel creation:Signup` |
40360c17 | 87 | : this.defaultNextStepButtonLabel |
ba7b7e57 RK |
88 | |
89 | this.hooks.runAction('action:signup.register.init', 'signup') | |
40360c17 | 90 | |
421d935d C |
91 | } |
92 | ||
1d5342ab C |
93 | hasSameChannelAndAccountNames () { |
94 | return this.getUsername() === this.getChannelName() | |
95 | } | |
96 | ||
97 | getUsername () { | |
98 | if (!this.formStepUser) return undefined | |
99 | ||
100 | return this.formStepUser.value['username'] | |
101 | } | |
102 | ||
103 | getChannelName () { | |
104 | if (!this.formStepChannel) return undefined | |
105 | ||
106 | return this.formStepChannel.value['name'] | |
107 | } | |
108 | ||
40360c17 K |
109 | onTermsFormBuilt (form: FormGroup) { |
110 | this.formStepTerms = form | |
111 | } | |
112 | ||
1d5342ab C |
113 | onUserFormBuilt (form: FormGroup) { |
114 | this.formStepUser = form | |
115 | } | |
116 | ||
117 | onChannelFormBuilt (form: FormGroup) { | |
118 | this.formStepChannel = form | |
a184c71b C |
119 | } |
120 | ||
421d935d | 121 | onTermsClick () { |
ed22eaab | 122 | this.instanceAboutAccordion.expandTerms() |
421d935d C |
123 | } |
124 | ||
125 | onCodeOfConductClick () { | |
ed22eaab | 126 | this.instanceAboutAccordion.expandCodeOfConduct() |
421d935d C |
127 | } |
128 | ||
40360c17 | 129 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
40360c17 K |
130 | this.aboutHtml = instanceAboutAccordion.aboutHtml |
131 | } | |
132 | ||
6f03f944 C |
133 | skipChannelCreation () { |
134 | this.formStepChannel.reset() | |
135 | this.lastStep.select() | |
9589907c | 136 | |
6f03f944 C |
137 | this.signup() |
138 | } | |
139 | ||
ba7b7e57 | 140 | async signup () { |
6f03f944 | 141 | this.signupError = undefined |
a184c71b | 142 | |
9589907c C |
143 | const termsForm = this.formStepTerms.value |
144 | const userForm = this.formStepUser.value | |
145 | const channelForm = this.formStepChannel?.value | |
146 | ||
147 | const channel = this.formStepChannel?.value?.name | |
148 | ? { name: channelForm?.name, displayName: channelForm?.displayName } | |
149 | : undefined | |
150 | ||
151 | const body = await this.hooks.wrapObject( | |
6f03f944 | 152 | { |
9589907c C |
153 | username: userForm.username, |
154 | password: userForm.password, | |
155 | email: userForm.email, | |
156 | displayName: userForm.displayName, | |
157 | ||
158 | registrationReason: termsForm.registrationReason, | |
6f03f944 | 159 | |
9589907c | 160 | channel |
6f03f944 | 161 | }, |
0912f1b4 | 162 | 'signup', |
ba7b7e57 RK |
163 | 'filter:api.signup.registration.create.params' |
164 | ) | |
a184c71b | 165 | |
9589907c C |
166 | const obs = this.requiresApproval |
167 | ? this.signupService.requestSignup(body) | |
168 | : this.signupService.directSignup(body) | |
169 | ||
170 | obs.subscribe({ | |
1378c0d3 | 171 | next: () => { |
9589907c | 172 | if (this.requiresEmailVerification || this.requiresApproval) { |
6f03f944 | 173 | this.signupSuccess = true |
d8c9996c | 174 | return |
d9eaee39 | 175 | } |
d8c9996c | 176 | |
43e9d2af | 177 | // Auto login |
9589907c | 178 | this.autoLogin(body) |
a184c71b C |
179 | }, |
180 | ||
9df52d66 | 181 | error: err => { |
6f03f944 | 182 | this.signupError = err.message |
9df52d66 | 183 | } |
1378c0d3 | 184 | }) |
a184c71b | 185 | } |
9589907c C |
186 | |
187 | private autoLogin (body: UserRegister) { | |
188 | this.authService.login({ username: body.username, password: body.password }) | |
189 | .subscribe({ | |
190 | next: () => { | |
191 | this.signupSuccess = true | |
192 | }, | |
193 | ||
194 | error: err => { | |
195 | this.signupError = err.message | |
196 | } | |
197 | }) | |
198 | } | |
a184c71b | 199 | } |