password-setting.component.ts 2.65 KB
Newer Older
kunw's avatar
kunw committed
1
import { Component, ViewChild, AfterViewChecked } from '@angular/core';
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
import { Router } from '@angular/router';
import { NgForm } from '@angular/forms';

import { PasswordSettingService } from './password-setting.service';
import { SessionService } from '../../shared/session.service';

@Component({
    selector: 'password-setting',
    templateUrl: "password-setting.component.html"
})
export class PasswordSettingComponent implements AfterViewChecked {
    opened: boolean = false;
    oldPwd: string = "";
    newPwd: string = "";
    reNewPwd: string = "";

    private formValueChanged: boolean = false;
    private onCalling: boolean = false;

    pwdFormRef: NgForm;
    @ViewChild("changepwdForm") pwdForm: NgForm;
    constructor(private passwordService: PasswordSettingService, private session: SessionService){}

    //If form is valid
    public get isValid(): boolean {
        if (this.pwdForm && this.pwdForm.form.get("newPassword")) {
            return this.pwdForm.valid &&
                this.pwdForm.form.get("newPassword").value === this.pwdForm.form.get("reNewPassword").value;
        }
        return false;
    }

    public get valueChanged(): boolean {
        return this.formValueChanged;
    }

    public get showProgress(): boolean {
        return this.onCalling;
    }

    ngAfterViewChecked() {
        if (this.pwdFormRef != this.pwdForm) {
            this.pwdFormRef = this.pwdForm;
            if (this.pwdFormRef) {
                this.pwdFormRef.valueChanges.subscribe(data => {
                    this.formValueChanged = true;
                });
            }
        }
    }

    //Open modal dialog
    open(): void {
        this.opened = true;
        this.pwdForm.reset();
    }

    //Close the moal dialog
    close(): void {
        this.opened = false;
    }

    //handle the ok action
    doOk(): void {
        if (this.onCalling) {
            return;//To avoid duplicate click events
        }

        if (!this.isValid) {
            return;//Double confirm
        }

        //Double confirm session is valid
        let cUser = this.session.getCurrentUser();
        if(!cUser){
            return;
        }

        //Call service
        this.onCalling = true;

        this.passwordService.changePassword(cUser.user_id, 
        {
            new_password: this.pwdForm.value.newPassword,
            old_password: this.pwdForm.value.oldPassword
        })
        .then(() => {
            this.onCalling = false;
            this.close();
        })
        .catch(error => {
            this.onCalling = false;
            console.error(error);//TODO:
        });
        //TODO:publish the successful message to general messae box
    }
}