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