]> 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
Migrate to $localize
[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 { User } from '@shared/models'
7
8 @Component({
9 selector: 'my-account-change-email',
10 templateUrl: './my-account-change-email.component.html',
11 styleUrls: [ './my-account-change-email.component.scss' ]
12 })
13 export class MyAccountChangeEmailComponent extends FormReactive implements OnInit {
14 error: string = null
15 success: string = null
16 user: User = null
17
18 constructor (
19 protected formValidatorService: FormValidatorService,
20 private userValidatorsService: UserValidatorsService,
21 private authService: AuthService,
22 private userService: UserService,
23 private serverService: ServerService
24 ) {
25 super()
26 }
27
28 ngOnInit () {
29 this.buildForm({
30 'new-email': this.userValidatorsService.USER_EMAIL,
31 'password': this.userValidatorsService.USER_PASSWORD
32 })
33
34 this.user = this.authService.getUser()
35 }
36
37 changeEmail () {
38 this.error = null
39 this.success = null
40
41 const password = this.form.value[ 'password' ]
42 const email = this.form.value[ 'new-email' ]
43
44 forkJoin([
45 this.serverService.getConfig(),
46 this.userService.changeEmail(password, email)
47 ]).pipe(tap(() => this.authService.refreshUserInformation()))
48 .subscribe(
49 ([ config ]) => {
50 this.form.reset()
51
52 if (config.signup.requiresEmailVerification) {
53 this.success = $localize`Please check your emails to verify your new email.`
54 } else {
55 this.success = $localize`Email updated.`
56 }
57 },
58
59 err => {
60 if (err.status === 401) {
61 this.error = $localize`You current password is invalid.`
62 return
63 }
64
65 this.error = err.message
66 }
67 )
68 }
69 }