]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { FormGroup } from '@angular/forms' | |
3 | import { ActivatedRoute } from '@angular/router' | |
4 | import { AuthService, UserService } from '@app/core' | |
5 | import { HooksService } from '@app/core/plugins/hooks.service' | |
6 | import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap' | |
7 | import { UserRegister } from '@shared/models' | |
8 | import { ServerConfig } from '@shared/models/server' | |
9 | import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' | |
10 | ||
11 | @Component({ | |
12 | selector: 'my-register', | |
13 | templateUrl: './register.component.html', | |
14 | styleUrls: [ './register.component.scss' ] | |
15 | }) | |
16 | export class RegisterComponent implements OnInit { | |
17 | accordion: NgbAccordion | |
18 | info: string = null | |
19 | error: string = null | |
20 | success: string = null | |
21 | signupDone = false | |
22 | ||
23 | videoUploadDisabled: boolean | |
24 | ||
25 | formStepTerms: FormGroup | |
26 | formStepUser: FormGroup | |
27 | formStepChannel: FormGroup | |
28 | ||
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 | 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` | |
43 | stepUserButtonLabel = this.defaultNextStepButtonLabel | |
44 | ||
45 | signupDisabled = false | |
46 | ||
47 | private serverConfig: ServerConfig | |
48 | ||
49 | constructor ( | |
50 | private route: ActivatedRoute, | |
51 | private authService: AuthService, | |
52 | private userService: UserService, | |
53 | private hooks: HooksService | |
54 | ) { } | |
55 | ||
56 | get requiresEmailVerification () { | |
57 | return this.serverConfig.signup.requiresEmailVerification | |
58 | } | |
59 | ||
60 | get minimumAge () { | |
61 | return this.serverConfig.signup.minimumAge | |
62 | } | |
63 | ||
64 | ngOnInit (): void { | |
65 | this.serverConfig = this.route.snapshot.data.serverConfig | |
66 | ||
67 | if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) { | |
68 | this.signupDisabled = true | |
69 | return | |
70 | } | |
71 | ||
72 | this.videoUploadDisabled = this.serverConfig.user.videoQuota === 0 | |
73 | this.stepUserButtonLabel = this.videoUploadDisabled | |
74 | ? $localize`:Button on the registration form to finalize the account and channel creation:Signup` | |
75 | : this.defaultNextStepButtonLabel | |
76 | ||
77 | this.hooks.runAction('action:signup.register.init', 'signup') | |
78 | ||
79 | } | |
80 | ||
81 | hasSameChannelAndAccountNames () { | |
82 | return this.getUsername() === this.getChannelName() | |
83 | } | |
84 | ||
85 | getUsername () { | |
86 | if (!this.formStepUser) return undefined | |
87 | ||
88 | return this.formStepUser.value['username'] | |
89 | } | |
90 | ||
91 | getChannelName () { | |
92 | if (!this.formStepChannel) return undefined | |
93 | ||
94 | return this.formStepChannel.value['name'] | |
95 | } | |
96 | ||
97 | onTermsFormBuilt (form: FormGroup) { | |
98 | this.formStepTerms = form | |
99 | } | |
100 | ||
101 | onUserFormBuilt (form: FormGroup) { | |
102 | this.formStepUser = form | |
103 | } | |
104 | ||
105 | onChannelFormBuilt (form: FormGroup) { | |
106 | this.formStepChannel = form | |
107 | } | |
108 | ||
109 | onTermsClick () { | |
110 | if (this.accordion) this.accordion.toggle('terms') | |
111 | } | |
112 | ||
113 | onCodeOfConductClick () { | |
114 | if (this.accordion) this.accordion.toggle('code-of-conduct') | |
115 | } | |
116 | ||
117 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { | |
118 | this.accordion = instanceAboutAccordion.accordion | |
119 | this.aboutHtml = instanceAboutAccordion.aboutHtml | |
120 | } | |
121 | ||
122 | async signup () { | |
123 | this.error = null | |
124 | ||
125 | const body: UserRegister = await this.hooks.wrapObject( | |
126 | Object.assign(this.formStepUser.value, { channel: this.videoUploadDisabled ? undefined : this.formStepChannel.value }), | |
127 | 'signup', | |
128 | 'filter:api.signup.registration.create.params' | |
129 | ) | |
130 | ||
131 | this.userService.signup(body).subscribe({ | |
132 | next: () => { | |
133 | this.signupDone = true | |
134 | ||
135 | if (this.requiresEmailVerification) { | |
136 | this.info = $localize`Now please check your emails to verify your account and complete signup.` | |
137 | return | |
138 | } | |
139 | ||
140 | // Auto login | |
141 | this.authService.login(body.username, body.password) | |
142 | .subscribe({ | |
143 | next: () => { | |
144 | this.success = $localize`You are now logged in as ${body.username}!` | |
145 | }, | |
146 | ||
147 | error: err => { | |
148 | this.error = err.message | |
149 | } | |
150 | }) | |
151 | }, | |
152 | ||
153 | error: err => { | |
154 | this.error = err.message | |
155 | } | |
156 | }) | |
157 | } | |
158 | } |