]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+signup/+register/register.component.ts
Refactoring login component style
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / +register / register.component.ts
CommitLineData
6f03f944
C
1import { CdkStep } from '@angular/cdk/stepper'
2import { Component, OnInit, ViewChild } from '@angular/core'
1d5342ab 3import { FormGroup } from '@angular/forms'
67ed6552 4import { ActivatedRoute } from '@angular/router'
d92d070c 5import { AuthService } from '@app/core'
ba7b7e57 6import { HooksService } from '@app/core/plugins/hooks.service'
d92d070c 7import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
421d935d 8import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
67ed6552 9import { UserRegister } from '@shared/models'
40360c17 10import { ServerConfig } from '@shared/models/server'
9589907c 11import { SignupService } from '../shared/signup.service'
a184c71b
C
12
13@Component({
b247a132
C
14 selector: 'my-register',
15 templateUrl: './register.component.html',
16 styleUrls: [ './register.component.scss' ]
a184c71b 17})
421d935d 18export 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,
9589907c 56 private signupService: SignupService,
66357162 57 private hooks: HooksService
9df52d66 58 ) { }
8a19bee1 59
d9eaee39 60 get requiresEmailVerification () {
ba430d75 61 return this.serverConfig.signup.requiresEmailVerification
d9eaee39
JM
62 }
63
9589907c
C
64 get requiresApproval () {
65 return this.serverConfig.signup.requiresApproval
66 }
67
1f256e7d
P
68 get minimumAge () {
69 return this.serverConfig.signup.minimumAge
70 }
71
6f03f944
C
72 get instanceName () {
73 return this.serverConfig.instance.name
74 }
75
76 ngOnInit () {
ba430d75
C
77 this.serverConfig = this.route.snapshot.data.serverConfig
78
bd898dd7
C
79 if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) {
80 this.signupDisabled = true
81 return
82 }
83
6f03f944
C
84 this.videoQuota = this.serverConfig.user.videoQuota
85 this.videoUploadDisabled = this.videoQuota === 0
86
40360c17 87 this.stepUserButtonLabel = this.videoUploadDisabled
e0fea785 88 ? $localize`:Button on the registration form to finalize the account and channel creation:Signup`
40360c17 89 : this.defaultNextStepButtonLabel
ba7b7e57
RK
90
91 this.hooks.runAction('action:signup.register.init', 'signup')
40360c17 92
421d935d
C
93 }
94
1d5342ab
C
95 hasSameChannelAndAccountNames () {
96 return this.getUsername() === this.getChannelName()
97 }
98
99 getUsername () {
100 if (!this.formStepUser) return undefined
101
102 return this.formStepUser.value['username']
103 }
104
105 getChannelName () {
106 if (!this.formStepChannel) return undefined
107
108 return this.formStepChannel.value['name']
109 }
110
40360c17
K
111 onTermsFormBuilt (form: FormGroup) {
112 this.formStepTerms = form
113 }
114
1d5342ab
C
115 onUserFormBuilt (form: FormGroup) {
116 this.formStepUser = form
117 }
118
119 onChannelFormBuilt (form: FormGroup) {
120 this.formStepChannel = form
a184c71b
C
121 }
122
421d935d
C
123 onTermsClick () {
124 if (this.accordion) this.accordion.toggle('terms')
125 }
126
127 onCodeOfConductClick () {
128 if (this.accordion) this.accordion.toggle('code-of-conduct')
129 }
130
40360c17
K
131 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
132 this.accordion = instanceAboutAccordion.accordion
133 this.aboutHtml = instanceAboutAccordion.aboutHtml
134 }
135
6f03f944
C
136 skipChannelCreation () {
137 this.formStepChannel.reset()
138 this.lastStep.select()
9589907c 139
6f03f944
C
140 this.signup()
141 }
142
ba7b7e57 143 async signup () {
6f03f944 144 this.signupError = undefined
a184c71b 145
9589907c
C
146 const termsForm = this.formStepTerms.value
147 const userForm = this.formStepUser.value
148 const channelForm = this.formStepChannel?.value
149
150 const channel = this.formStepChannel?.value?.name
151 ? { name: channelForm?.name, displayName: channelForm?.displayName }
152 : undefined
153
154 const body = await this.hooks.wrapObject(
6f03f944 155 {
9589907c
C
156 username: userForm.username,
157 password: userForm.password,
158 email: userForm.email,
159 displayName: userForm.displayName,
160
161 registrationReason: termsForm.registrationReason,
6f03f944 162
9589907c 163 channel
6f03f944 164 },
0912f1b4 165 'signup',
ba7b7e57
RK
166 'filter:api.signup.registration.create.params'
167 )
a184c71b 168
9589907c
C
169 const obs = this.requiresApproval
170 ? this.signupService.requestSignup(body)
171 : this.signupService.directSignup(body)
172
173 obs.subscribe({
1378c0d3 174 next: () => {
9589907c 175 if (this.requiresEmailVerification || this.requiresApproval) {
6f03f944 176 this.signupSuccess = true
d8c9996c 177 return
d9eaee39 178 }
d8c9996c 179
43e9d2af 180 // Auto login
9589907c 181 this.autoLogin(body)
a184c71b
C
182 },
183
9df52d66 184 error: err => {
6f03f944 185 this.signupError = err.message
9df52d66 186 }
1378c0d3 187 })
a184c71b 188 }
9589907c
C
189
190 private autoLogin (body: UserRegister) {
191 this.authService.login({ username: body.username, password: body.password })
192 .subscribe({
193 next: () => {
194 this.signupSuccess = true
195 },
196
197 error: err => {
198 this.signupError = err.message
199 }
200 })
201 }
a184c71b 202}