]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+signup/+register/register.component.ts
Merge branch 'signup-hooks' into 'develop'
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / +register / register.component.ts
CommitLineData
421d935d 1import { Component, OnInit, ViewChild } from '@angular/core'
f8b2c1b4 2import { AuthService, Notifier, RedirectService, ServerService } from '@app/core'
b247a132 3import { UserService, UserValidatorsService } from '@app/shared'
b1d40cff 4import { I18n } from '@ngx-translate/i18n-polyfill'
1d5342ab
C
5import { UserRegister } from '@shared/models/users/user-register.model'
6import { FormGroup } from '@angular/forms'
421d935d
C
7import { About } from '@shared/models/server'
8import { InstanceService } from '@app/shared/instance/instance.service'
ba7b7e57 9import { HooksService } from '@app/core/plugins/hooks.service'
421d935d 10import { NgbAccordion } from '@ng-bootstrap/ng-bootstrap'
a184c71b
C
11
12@Component({
b247a132
C
13 selector: 'my-register',
14 templateUrl: './register.component.html',
15 styleUrls: [ './register.component.scss' ]
a184c71b 16})
421d935d
C
17export class RegisterComponent implements OnInit {
18 @ViewChild('accordion', { static: true }) accordion: NgbAccordion
19
d8c9996c 20 info: string = null
df98563e 21 error: string = null
1d5342ab 22 success: string = null
d8c9996c 23 signupDone = false
a184c71b 24
421d935d
C
25 about: About
26 aboutHtml = {
27 description: '',
28 terms: '',
29 codeOfConduct: '',
30 moderationInformation: '',
31 administrator: ''
32 }
33
1d5342ab
C
34 formStepUser: FormGroup
35 formStepChannel: FormGroup
36
df98563e 37 constructor (
43e9d2af 38 private authService: AuthService,
e309822b 39 private userValidatorsService: UserValidatorsService,
f8b2c1b4 40 private notifier: Notifier,
5afdd0a5 41 private userService: UserService,
d9eaee39 42 private serverService: ServerService,
b1d40cff 43 private redirectService: RedirectService,
421d935d 44 private instanceService: InstanceService,
ba7b7e57 45 private hooks: HooksService,
b1d40cff 46 private i18n: I18n
a184c71b 47 ) {
8a19bee1
C
48 }
49
d9eaee39
JM
50 get requiresEmailVerification () {
51 return this.serverService.getConfig().signup.requiresEmailVerification
52 }
53
421d935d
C
54 ngOnInit (): void {
55 this.instanceService.getAbout()
56 .subscribe(
57 async about => {
58 this.about = about
59
60 this.aboutHtml = await this.instanceService.buildHtml(about)
61 },
62
63 err => this.notifier.error(err.message)
64 )
ba7b7e57
RK
65
66 this.hooks.runAction('action:signup.register.init', 'signup')
421d935d
C
67 }
68
1d5342ab
C
69 hasSameChannelAndAccountNames () {
70 return this.getUsername() === this.getChannelName()
71 }
72
73 getUsername () {
74 if (!this.formStepUser) return undefined
75
76 return this.formStepUser.value['username']
77 }
78
79 getChannelName () {
80 if (!this.formStepChannel) return undefined
81
82 return this.formStepChannel.value['name']
83 }
84
85 onUserFormBuilt (form: FormGroup) {
86 this.formStepUser = form
87 }
88
89 onChannelFormBuilt (form: FormGroup) {
90 this.formStepChannel = form
a184c71b
C
91 }
92
421d935d
C
93 onTermsClick () {
94 if (this.accordion) this.accordion.toggle('terms')
95 }
96
97 onCodeOfConductClick () {
98 if (this.accordion) this.accordion.toggle('code-of-conduct')
99 }
100
ba7b7e57 101 async signup () {
df98563e 102 this.error = null
a184c71b 103
ba7b7e57
RK
104 const body: UserRegister = await this.hooks.wrapObject(
105 Object.assign(this.formStepUser.value, { channel: this.formStepChannel.value }),
106 'filter:api.signup.registration.create.params'
107 )
a184c71b 108
1d5342ab 109 this.userService.signup(body).subscribe(
a184c71b 110 () => {
d8c9996c
C
111 this.signupDone = true
112
d9eaee39 113 if (this.requiresEmailVerification) {
1d5342ab 114 this.info = this.i18n('Now please check your emails to verify your account and complete signup.')
d8c9996c 115 return
d9eaee39 116 }
d8c9996c 117
43e9d2af 118 // Auto login
1d5342ab 119 this.authService.login(body.username, body.password)
43e9d2af
C
120 .subscribe(
121 () => {
1d5342ab 122 this.success = this.i18n('You are now logged in as {{username}}!', { username: body.username })
43e9d2af
C
123 },
124
125 err => this.error = err.message
126 )
a184c71b
C
127 },
128
f7354483 129 err => this.error = err.message
df98563e 130 )
a184c71b
C
131 }
132}