]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-change-email/my-account-change-email.component.ts
Reorganize client shared modules
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-change-email / my-account-change-email.component.ts
1 import { forkJoin } from 'rxjs'
2 import { tap } from 'rxjs/operators'
3 import { Component, OnInit } from '@angular/core'
4 import { AuthService, ServerService, UserService } from '@app/core'
5 import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { User } from '@shared/models'
8
9 @Component({
10 selector: 'my-account-change-email',
11 templateUrl: './my-account-change-email.component.html',
12 styleUrls: [ './my-account-change-email.component.scss' ]
13 })
14 export class MyAccountChangeEmailComponent extends FormReactive implements OnInit {
15 error: string = null
16 success: string = null
17 user: User = null
18
19 constructor (
20 protected formValidatorService: FormValidatorService,
21 private userValidatorsService: UserValidatorsService,
22 private authService: AuthService,
23 private userService: UserService,
24 private serverService: ServerService,
25 private i18n: I18n
26 ) {
27 super()
28 }
29
30 ngOnInit () {
31 this.buildForm({
32 'new-email': this.userValidatorsService.USER_EMAIL,
33 'password': this.userValidatorsService.USER_PASSWORD
34 })
35
36 this.user = this.authService.getUser()
37 }
38
39 changeEmail () {
40 this.error = null
41 this.success = null
42
43 const password = this.form.value[ 'password' ]
44 const email = this.form.value[ 'new-email' ]
45
46 forkJoin([
47 this.serverService.getConfig(),
48 this.userService.changeEmail(password, email)
49 ]).pipe(tap(() => this.authService.refreshUserInformation()))
50 .subscribe(
51 ([ config ]) => {
52 this.form.reset()
53
54 if (config.signup.requiresEmailVerification) {
55 this.success = this.i18n('Please check your emails to verify your new email.')
56 } else {
57 this.success = this.i18n('Email updated.')
58 }
59 },
60
61 err => {
62 if (err.status === 401) {
63 this.error = this.i18n('You current password is invalid.')
64 return
65 }
66
67 this.error = err.message
68 }
69 )
70 }
71 }