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