]>
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 | ||
41 | defaultNextStepButtonLabel = $localize`Next` | |
42 | stepUserButtonLabel = this.defaultNextStepButtonLabel | |
43 | ||
ba430d75 C |
44 | private serverConfig: ServerConfig |
45 | ||
df98563e | 46 | constructor ( |
ba430d75 | 47 | private route: ActivatedRoute, |
43e9d2af | 48 | private authService: AuthService, |
5afdd0a5 | 49 | private userService: UserService, |
66357162 C |
50 | private hooks: HooksService |
51 | ) { | |
8a19bee1 C |
52 | } |
53 | ||
d9eaee39 | 54 | get requiresEmailVerification () { |
ba430d75 | 55 | return this.serverConfig.signup.requiresEmailVerification |
d9eaee39 JM |
56 | } |
57 | ||
421d935d | 58 | ngOnInit (): void { |
ba430d75 C |
59 | this.serverConfig = this.route.snapshot.data.serverConfig |
60 | ||
09c55770 | 61 | this.videoUploadDisabled = this.serverConfig.user.videoQuota === 0 |
40360c17 K |
62 | this.stepUserButtonLabel = this.videoUploadDisabled |
63 | ? $localize`Signup` | |
64 | : this.defaultNextStepButtonLabel | |
ba7b7e57 RK |
65 | |
66 | this.hooks.runAction('action:signup.register.init', 'signup') | |
40360c17 | 67 | |
421d935d C |
68 | } |
69 | ||
1d5342ab C |
70 | hasSameChannelAndAccountNames () { |
71 | return this.getUsername() === this.getChannelName() | |
72 | } | |
73 | ||
74 | getUsername () { | |
75 | if (!this.formStepUser) return undefined | |
76 | ||
77 | return this.formStepUser.value['username'] | |
78 | } | |
79 | ||
80 | getChannelName () { | |
81 | if (!this.formStepChannel) return undefined | |
82 | ||
83 | return this.formStepChannel.value['name'] | |
84 | } | |
85 | ||
40360c17 K |
86 | onTermsFormBuilt (form: FormGroup) { |
87 | this.formStepTerms = form | |
88 | } | |
89 | ||
1d5342ab C |
90 | onUserFormBuilt (form: FormGroup) { |
91 | this.formStepUser = form | |
92 | } | |
93 | ||
94 | onChannelFormBuilt (form: FormGroup) { | |
95 | this.formStepChannel = form | |
a184c71b C |
96 | } |
97 | ||
421d935d C |
98 | onTermsClick () { |
99 | if (this.accordion) this.accordion.toggle('terms') | |
100 | } | |
101 | ||
102 | onCodeOfConductClick () { | |
103 | if (this.accordion) this.accordion.toggle('code-of-conduct') | |
104 | } | |
105 | ||
40360c17 K |
106 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
107 | this.accordion = instanceAboutAccordion.accordion | |
108 | this.aboutHtml = instanceAboutAccordion.aboutHtml | |
109 | } | |
110 | ||
ba7b7e57 | 111 | async signup () { |
df98563e | 112 | this.error = null |
a184c71b | 113 | |
ba7b7e57 | 114 | const body: UserRegister = await this.hooks.wrapObject( |
09c55770 | 115 | Object.assign(this.formStepUser.value, { channel: this.videoUploadDisabled ? undefined : this.formStepChannel.value }), |
0912f1b4 | 116 | 'signup', |
ba7b7e57 RK |
117 | 'filter:api.signup.registration.create.params' |
118 | ) | |
a184c71b | 119 | |
1d5342ab | 120 | this.userService.signup(body).subscribe( |
a184c71b | 121 | () => { |
d8c9996c C |
122 | this.signupDone = true |
123 | ||
d9eaee39 | 124 | if (this.requiresEmailVerification) { |
66357162 | 125 | this.info = $localize`Now please check your emails to verify your account and complete signup.` |
d8c9996c | 126 | return |
d9eaee39 | 127 | } |
d8c9996c | 128 | |
43e9d2af | 129 | // Auto login |
1d5342ab | 130 | this.authService.login(body.username, body.password) |
43e9d2af C |
131 | .subscribe( |
132 | () => { | |
66357162 | 133 | this.success = $localize`You are now logged in as ${body.username}!` |
43e9d2af C |
134 | }, |
135 | ||
136 | err => this.error = err.message | |
137 | ) | |
a184c71b C |
138 | }, |
139 | ||
f7354483 | 140 | err => this.error = err.message |
df98563e | 141 | ) |
a184c71b C |
142 | } |
143 | } |