]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { forkJoin } from 'rxjs' |
2 | import { tap } from 'rxjs/operators' | |
0ba5f5ba | 3 | import { Component, OnInit } from '@angular/core' |
67ed6552 C |
4 | import { AuthService, ServerService, UserService } from '@app/core' |
5 | import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' | |
0ba5f5ba | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 7 | import { User } from '@shared/models' |
0ba5f5ba C |
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, | |
0ba5f5ba C |
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 | ||
ba430d75 C |
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() | |
0ba5f5ba | 53 | |
ba430d75 C |
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 | }, | |
0ba5f5ba | 60 | |
ba430d75 C |
61 | err => { |
62 | if (err.status === 401) { | |
63 | this.error = this.i18n('You current password is invalid.') | |
64 | return | |
0ba5f5ba | 65 | } |
ba430d75 C |
66 | |
67 | this.error = err.message | |
68 | } | |
69 | ) | |
0ba5f5ba C |
70 | } |
71 | } |