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