]>
Commit | Line | Data |
---|---|---|
0ba5f5ba C |
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' | |
a80e84f0 | 8 | import { tap } from 'rxjs/operators' |
ba430d75 | 9 | import { forkJoin } from 'rxjs' |
0ba5f5ba C |
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 | ||
ba430d75 C |
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() | |
0ba5f5ba | 56 | |
ba430d75 C |
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 | }, | |
0ba5f5ba | 63 | |
ba430d75 C |
64 | err => { |
65 | if (err.status === 401) { | |
66 | this.error = this.i18n('You current password is invalid.') | |
67 | return | |
0ba5f5ba | 68 | } |
ba430d75 C |
69 | |
70 | this.error = err.message | |
71 | } | |
72 | ) | |
0ba5f5ba C |
73 | } |
74 | } |