]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/account/account.component.ts
9b6b5fbf421123f282cc1e2e1a8d065d22d346a7
[github/Chocobozzz/PeerTube.git] / client / src / app / account / account.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { FormBuilder, FormGroup } from '@angular/forms';
3 import { Router } from '@angular/router';
4
5 import { NotificationsService } from 'angular2-notifications';
6
7 import { AccountService } from './account.service';
8 import { FormReactive, USER_PASSWORD } from '../shared';
9
10 @Component({
11 selector: 'my-account',
12 templateUrl: './account.component.html'
13 })
14
15 export class AccountComponent extends FormReactive implements OnInit {
16 error: string = null;
17
18 form: FormGroup;
19 formErrors = {
20 'new-password': '',
21 'new-confirmed-password': ''
22 };
23 validationMessages = {
24 'new-password': USER_PASSWORD.MESSAGES,
25 'new-confirmed-password': USER_PASSWORD.MESSAGES
26 };
27
28 constructor(
29 private formBuilder: FormBuilder,
30 private router: Router,
31 private notificationsService: NotificationsService,
32 private accountService: AccountService
33 ) {
34 super();
35 }
36
37 buildForm() {
38 this.form = this.formBuilder.group({
39 'new-password': [ '', USER_PASSWORD.VALIDATORS ],
40 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ],
41 });
42
43 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
44 }
45
46 ngOnInit() {
47 this.buildForm();
48 }
49
50 changePassword() {
51 const newPassword = this.form.value['new-password'];
52 const newConfirmedPassword = this.form.value['new-confirmed-password'];
53
54 this.error = null;
55
56 if (newPassword !== newConfirmedPassword) {
57 this.error = 'The new password and the confirmed password do not correspond.';
58 return;
59 }
60
61 this.accountService.changePassword(newPassword).subscribe(
62 () => this.notificationsService.success('Success', 'Password updated.'),
63
64 err => this.error = err
65 );
66 }
67 }