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