1 import { CdkStep } from '@angular/cdk/stepper'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { FormGroup } from '@angular/forms'
4 import { ActivatedRoute } from '@angular/router'
5 import { AuthService } from '@app/core'
6 import { HooksService } from '@app/core/plugins/hooks.service'
7 import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance'
8 import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
9 import { UserRegister } from '@shared/models'
10 import { ServerConfig } from '@shared/models/server'
11 import { SignupService } from '../shared/signup.service'
14 selector: 'my-register',
15 templateUrl: './register.component.html',
16 styleUrls: [ './register.component.scss' ]
18 export class RegisterComponent implements OnInit {
19 @ViewChild('lastStep') lastStep: CdkStep
21 accordion: NgbAccordion
26 videoUploadDisabled: boolean
29 formStepTerms: FormGroup
30 formStepUser: FormGroup
31 formStepChannel: FormGroup
37 instanceInformationPanels = {
40 administrators: false,
45 defaultPreviousStepButtonLabel = $localize`Go to the previous step`
46 defaultNextStepButtonLabel = $localize`Go to the next step`
47 stepUserButtonLabel = this.defaultNextStepButtonLabel
49 signupDisabled = false
51 private serverConfig: ServerConfig
54 private route: ActivatedRoute,
55 private authService: AuthService,
56 private signupService: SignupService,
57 private hooks: HooksService
60 get requiresEmailVerification () {
61 return this.serverConfig.signup.requiresEmailVerification
64 get requiresApproval () {
65 return this.serverConfig.signup.requiresApproval
69 return this.serverConfig.signup.minimumAge
73 return this.serverConfig.instance.name
77 this.serverConfig = this.route.snapshot.data.serverConfig
79 if (this.serverConfig.signup.allowed === false || this.serverConfig.signup.allowedForCurrentIP === false) {
80 this.signupDisabled = true
84 this.videoQuota = this.serverConfig.user.videoQuota
85 this.videoUploadDisabled = this.videoQuota === 0
87 this.stepUserButtonLabel = this.videoUploadDisabled
88 ? $localize`:Button on the registration form to finalize the account and channel creation:Signup`
89 : this.defaultNextStepButtonLabel
91 this.hooks.runAction('action:signup.register.init', 'signup')
95 hasSameChannelAndAccountNames () {
96 return this.getUsername() === this.getChannelName()
100 if (!this.formStepUser) return undefined
102 return this.formStepUser.value['username']
106 if (!this.formStepChannel) return undefined
108 return this.formStepChannel.value['name']
111 onTermsFormBuilt (form: FormGroup) {
112 this.formStepTerms = form
115 onUserFormBuilt (form: FormGroup) {
116 this.formStepUser = form
119 onChannelFormBuilt (form: FormGroup) {
120 this.formStepChannel = form
124 if (this.accordion) this.accordion.toggle('terms')
127 onCodeOfConductClick () {
128 if (this.accordion) this.accordion.toggle('code-of-conduct')
131 onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) {
132 this.accordion = instanceAboutAccordion.accordion
133 this.aboutHtml = instanceAboutAccordion.aboutHtml
136 skipChannelCreation () {
137 this.formStepChannel.reset()
138 this.lastStep.select()
144 this.signupError = undefined
146 const termsForm = this.formStepTerms.value
147 const userForm = this.formStepUser.value
148 const channelForm = this.formStepChannel?.value
150 const channel = this.formStepChannel?.value?.name
151 ? { name: channelForm?.name, displayName: channelForm?.displayName }
154 const body = await this.hooks.wrapObject(
156 username: userForm.username,
157 password: userForm.password,
158 email: userForm.email,
159 displayName: userForm.displayName,
161 registrationReason: termsForm.registrationReason,
166 'filter:api.signup.registration.create.params'
169 const obs = this.requiresApproval
170 ? this.signupService.requestSignup(body)
171 : this.signupService.directSignup(body)
175 if (this.requiresEmailVerification || this.requiresApproval) {
176 this.signupSuccess = true
185 this.signupError = err.message
190 private autoLogin (body: UserRegister) {
191 this.authService.login({ username: body.username, password: body.password })
194 this.signupSuccess = true
198 this.signupError = err.message