]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+signup/+register/register.component.ts
Update code contributors
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / +register / register.component.ts
CommitLineData
40360c17 1import { Component, OnInit } from '@angular/core'
1d5342ab 2import { FormGroup } from '@angular/forms'
67ed6552 3import { ActivatedRoute } from '@angular/router'
40360c17 4import { AuthService, UserService } from '@app/core'
ba7b7e57 5import { HooksService } from '@app/core/plugins/hooks.service'
421d935d 6import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
67ed6552 7import { UserRegister } from '@shared/models'
40360c17
K
8import { ServerConfig } from '@shared/models/server'
9import { 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 16export 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
bd898dd7
C
45 signupDisabled = false
46
ba430d75
C
47 private serverConfig: ServerConfig
48
df98563e 49 constructor (
ba430d75 50 private route: ActivatedRoute,
43e9d2af 51 private authService: AuthService,
5afdd0a5 52 private userService: UserService,
66357162 53 private hooks: HooksService
9df52d66 54 ) { }
8a19bee1 55
d9eaee39 56 get requiresEmailVerification () {
ba430d75 57 return this.serverConfig.signup.requiresEmailVerification
d9eaee39
JM
58 }
59
1f256e7d
P
60 get minimumAge () {
61 return this.serverConfig.signup.minimumAge
62 }
63
421d935d 64 ngOnInit (): void {
ba430d75
C
65 this.serverConfig = this.route.snapshot.data.serverConfig
66
bd898dd7
C
67 if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) {
68 this.signupDisabled = true
69 return
70 }
71
09c55770 72 this.videoUploadDisabled = this.serverConfig.user.videoQuota === 0
40360c17 73 this.stepUserButtonLabel = this.videoUploadDisabled
e0fea785 74 ? $localize`:Button on the registration form to finalize the account and channel creation:Signup`
40360c17 75 : this.defaultNextStepButtonLabel
ba7b7e57
RK
76
77 this.hooks.runAction('action:signup.register.init', 'signup')
40360c17 78
421d935d
C
79 }
80
1d5342ab
C
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
40360c17
K
97 onTermsFormBuilt (form: FormGroup) {
98 this.formStepTerms = form
99 }
100
1d5342ab
C
101 onUserFormBuilt (form: FormGroup) {
102 this.formStepUser = form
103 }
104
105 onChannelFormBuilt (form: FormGroup) {
106 this.formStepChannel = form
a184c71b
C
107 }
108
421d935d
C
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
40360c17
K
117 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
118 this.accordion = instanceAboutAccordion.accordion
119 this.aboutHtml = instanceAboutAccordion.aboutHtml
120 }
121
ba7b7e57 122 async signup () {
df98563e 123 this.error = null
a184c71b 124
ba7b7e57 125 const body: UserRegister = await this.hooks.wrapObject(
09c55770 126 Object.assign(this.formStepUser.value, { channel: this.videoUploadDisabled ? undefined : this.formStepChannel.value }),
0912f1b4 127 'signup',
ba7b7e57
RK
128 'filter:api.signup.registration.create.params'
129 )
a184c71b 130
1378c0d3
C
131 this.userService.signup(body).subscribe({
132 next: () => {
d8c9996c
C
133 this.signupDone = true
134
d9eaee39 135 if (this.requiresEmailVerification) {
66357162 136 this.info = $localize`Now please check your emails to verify your account and complete signup.`
d8c9996c 137 return
d9eaee39 138 }
d8c9996c 139
43e9d2af 140 // Auto login
1d5342ab 141 this.authService.login(body.username, body.password)
1378c0d3
C
142 .subscribe({
143 next: () => {
144 this.success = $localize`You are now logged in as ${body.username}!`
145 },
43e9d2af 146
9df52d66
C
147 error: err => {
148 this.error = err.message
149 }
1378c0d3 150 })
a184c71b
C
151 },
152
9df52d66
C
153 error: err => {
154 this.error = err.message
155 }
1378c0d3 156 })
a184c71b
C
157 }
158}