]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { forkJoin } from 'rxjs' |
2 | import { tap } from 'rxjs/operators' | |
0ba5f5ba | 3 | import { Component, OnInit } from '@angular/core' |
cb0eda56 | 4 | import { AuthService, Notifier, ServerService, UserService } from '@app/core' |
7ed1edbb | 5 | import { USER_EMAIL_VALIDATOR, USER_PASSWORD_VALIDATOR } from '@app/shared/form-validators/user-validators' |
5c5bcea2 | 6 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' |
d12b40fb | 7 | import { HttpStatusCode, 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 ( | |
5c5bcea2 | 20 | protected formReactiveService: FormReactiveService, |
0ba5f5ba C |
21 | private authService: AuthService, |
22 | private userService: UserService, | |
cb0eda56 AG |
23 | private serverService: ServerService, |
24 | private notifier: Notifier | |
7ed1edbb | 25 | ) { |
0ba5f5ba C |
26 | super() |
27 | } | |
28 | ||
29 | ngOnInit () { | |
30 | this.buildForm({ | |
7ed1edbb | 31 | 'new-email': USER_EMAIL_VALIDATOR, |
9df52d66 | 32 | password: USER_PASSWORD_VALIDATOR |
0ba5f5ba C |
33 | }) |
34 | ||
35 | this.user = this.authService.getUser() | |
36 | } | |
37 | ||
38 | changeEmail () { | |
39 | this.error = null | |
40 | this.success = null | |
41 | ||
9df52d66 C |
42 | const password = this.form.value['password'] |
43 | const email = this.form.value['new-email'] | |
0ba5f5ba | 44 | |
ba430d75 C |
45 | forkJoin([ |
46 | this.serverService.getConfig(), | |
47 | this.userService.changeEmail(password, email) | |
48 | ]).pipe(tap(() => this.authService.refreshUserInformation())) | |
1378c0d3 C |
49 | .subscribe({ |
50 | next: ([ config ]) => { | |
ba430d75 | 51 | this.form.reset() |
0ba5f5ba | 52 | |
ba430d75 | 53 | if (config.signup.requiresEmailVerification) { |
66357162 | 54 | this.success = $localize`Please check your emails to verify your new email.` |
ba430d75 | 55 | } else { |
66357162 | 56 | this.success = $localize`Email updated.` |
ba430d75 C |
57 | } |
58 | }, | |
0ba5f5ba | 59 | |
1378c0d3 | 60 | error: err => { |
d12b40fb | 61 | if (err.status === HttpStatusCode.UNAUTHORIZED_401) { |
66357162 | 62 | this.error = $localize`You current password is invalid.` |
ba430d75 | 63 | return |
0ba5f5ba | 64 | } |
ba430d75 C |
65 | |
66 | this.error = err.message | |
67 | } | |
1378c0d3 | 68 | }) |
0ba5f5ba C |
69 | } |
70 | } |