]>
Commit | Line | Data |
---|---|---|
40360c17 | 1 | import { Component, OnInit } from '@angular/core' |
1d5342ab | 2 | import { FormGroup } from '@angular/forms' |
67ed6552 | 3 | import { ActivatedRoute } from '@angular/router' |
d92d070c | 4 | import { AuthService } from '@app/core' |
ba7b7e57 | 5 | import { HooksService } from '@app/core/plugins/hooks.service' |
d92d070c C |
6 | import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' |
7 | import { UserSignupService } from '@app/shared/shared-users' | |
421d935d | 8 | import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap' |
67ed6552 | 9 | import { UserRegister } from '@shared/models' |
40360c17 | 10 | import { ServerConfig } from '@shared/models/server' |
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 { |
40360c17 | 18 | accordion: NgbAccordion |
d8c9996c | 19 | info: string = null |
df98563e | 20 | error: string = null |
1d5342ab | 21 | success: string = null |
d8c9996c | 22 | signupDone = false |
a184c71b | 23 | |
09c55770 | 24 | videoUploadDisabled: boolean |
25 | ||
40360c17 | 26 | formStepTerms: FormGroup |
1d5342ab C |
27 | formStepUser: FormGroup |
28 | formStepChannel: FormGroup | |
29 | ||
40360c17 K |
30 | aboutHtml = { |
31 | codeOfConduct: '' | |
32 | } | |
33 | ||
34 | instanceInformationPanels = { | |
35 | codeOfConduct: true, | |
36 | terms: true, | |
37 | administrators: false, | |
38 | features: false, | |
39 | moderation: false | |
40 | } | |
41 | ||
e0fea785 RK |
42 | defaultPreviousStepButtonLabel = $localize`:Button on the registration form to go to the previous step:Back` |
43 | defaultNextStepButtonLabel = $localize`:Button on the registration form to go to the previous step:Next` | |
40360c17 K |
44 | stepUserButtonLabel = this.defaultNextStepButtonLabel |
45 | ||
bd898dd7 C |
46 | signupDisabled = false |
47 | ||
ba430d75 C |
48 | private serverConfig: ServerConfig |
49 | ||
df98563e | 50 | constructor ( |
ba430d75 | 51 | private route: ActivatedRoute, |
43e9d2af | 52 | private authService: AuthService, |
d92d070c | 53 | private userSignupService: UserSignupService, |
66357162 | 54 | private hooks: HooksService |
9df52d66 | 55 | ) { } |
8a19bee1 | 56 | |
d9eaee39 | 57 | get requiresEmailVerification () { |
ba430d75 | 58 | return this.serverConfig.signup.requiresEmailVerification |
d9eaee39 JM |
59 | } |
60 | ||
1f256e7d P |
61 | get minimumAge () { |
62 | return this.serverConfig.signup.minimumAge | |
63 | } | |
64 | ||
421d935d | 65 | ngOnInit (): void { |
ba430d75 C |
66 | this.serverConfig = this.route.snapshot.data.serverConfig |
67 | ||
bd898dd7 C |
68 | if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) { |
69 | this.signupDisabled = true | |
70 | return | |
71 | } | |
72 | ||
09c55770 | 73 | this.videoUploadDisabled = this.serverConfig.user.videoQuota === 0 |
40360c17 | 74 | this.stepUserButtonLabel = this.videoUploadDisabled |
e0fea785 | 75 | ? $localize`:Button on the registration form to finalize the account and channel creation:Signup` |
40360c17 | 76 | : this.defaultNextStepButtonLabel |
ba7b7e57 RK |
77 | |
78 | this.hooks.runAction('action:signup.register.init', 'signup') | |
40360c17 | 79 | |
421d935d C |
80 | } |
81 | ||
1d5342ab C |
82 | hasSameChannelAndAccountNames () { |
83 | return this.getUsername() === this.getChannelName() | |
84 | } | |
85 | ||
86 | getUsername () { | |
87 | if (!this.formStepUser) return undefined | |
88 | ||
89 | return this.formStepUser.value['username'] | |
90 | } | |
91 | ||
92 | getChannelName () { | |
93 | if (!this.formStepChannel) return undefined | |
94 | ||
95 | return this.formStepChannel.value['name'] | |
96 | } | |
97 | ||
40360c17 K |
98 | onTermsFormBuilt (form: FormGroup) { |
99 | this.formStepTerms = form | |
100 | } | |
101 | ||
1d5342ab C |
102 | onUserFormBuilt (form: FormGroup) { |
103 | this.formStepUser = form | |
104 | } | |
105 | ||
106 | onChannelFormBuilt (form: FormGroup) { | |
107 | this.formStepChannel = form | |
a184c71b C |
108 | } |
109 | ||
421d935d C |
110 | onTermsClick () { |
111 | if (this.accordion) this.accordion.toggle('terms') | |
112 | } | |
113 | ||
114 | onCodeOfConductClick () { | |
115 | if (this.accordion) this.accordion.toggle('code-of-conduct') | |
116 | } | |
117 | ||
40360c17 K |
118 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
119 | this.accordion = instanceAboutAccordion.accordion | |
120 | this.aboutHtml = instanceAboutAccordion.aboutHtml | |
121 | } | |
122 | ||
ba7b7e57 | 123 | async signup () { |
df98563e | 124 | this.error = null |
a184c71b | 125 | |
ba7b7e57 | 126 | const body: UserRegister = await this.hooks.wrapObject( |
09c55770 | 127 | Object.assign(this.formStepUser.value, { channel: this.videoUploadDisabled ? undefined : this.formStepChannel.value }), |
0912f1b4 | 128 | 'signup', |
ba7b7e57 RK |
129 | 'filter:api.signup.registration.create.params' |
130 | ) | |
a184c71b | 131 | |
d92d070c | 132 | this.userSignupService.signup(body).subscribe({ |
1378c0d3 | 133 | next: () => { |
d8c9996c C |
134 | this.signupDone = true |
135 | ||
d9eaee39 | 136 | if (this.requiresEmailVerification) { |
66357162 | 137 | this.info = $localize`Now please check your emails to verify your account and complete signup.` |
d8c9996c | 138 | return |
d9eaee39 | 139 | } |
d8c9996c | 140 | |
43e9d2af | 141 | // Auto login |
1d5342ab | 142 | this.authService.login(body.username, body.password) |
1378c0d3 C |
143 | .subscribe({ |
144 | next: () => { | |
145 | this.success = $localize`You are now logged in as ${body.username}!` | |
146 | }, | |
43e9d2af | 147 | |
9df52d66 C |
148 | error: err => { |
149 | this.error = err.message | |
150 | } | |
1378c0d3 | 151 | }) |
a184c71b C |
152 | }, |
153 | ||
9df52d66 C |
154 | error: err => { |
155 | this.error = err.message | |
156 | } | |
1378c0d3 | 157 | }) |
a184c71b C |
158 | } |
159 | } |