account-settings-modal.component.ts 4.18 KB
Newer Older
1 2 3 4 5
import { Component, OnInit, ViewChild, AfterViewChecked } from '@angular/core';
import { NgForm } from '@angular/forms';

import { SessionUser } from '../../shared/session-user';
import { SessionService } from '../../shared/session.service';
Steven Zou's avatar
Steven Zou committed
6 7 8 9
import { MessageService } from '../../global-message/message.service';
import { AlertType, httpStatusCode } from '../../shared/shared.const';
import { errorHandler, accessErrorHandler } from '../../shared/shared.utils';
import { InlineAlertComponent } from '../../shared/inline-alert/inline-alert.component';
10 11 12 13 14 15 16 17 18 19

@Component({
    selector: "account-settings-modal",
    templateUrl: "account-settings-modal.component.html"
})

export class AccountSettingsModalComponent implements OnInit, AfterViewChecked {
    opened: boolean = false;
    staticBackdrop: boolean = true;
    account: SessionUser;
Steven Zou's avatar
Steven Zou committed
20 21
    error: any = null;
    originalStaticData: SessionUser;
22 23 24 25 26 27

    private isOnCalling: boolean = false;
    private formValueChanged: boolean = false;

    accountFormRef: NgForm;
    @ViewChild("accountSettingsFrom") accountForm: NgForm;
Steven Zou's avatar
Steven Zou committed
28 29
    @ViewChild(InlineAlertComponent)
    private inlineAlert: InlineAlertComponent;
30

Steven Zou's avatar
Steven Zou committed
31 32 33
    constructor(
        private session: SessionService,
        private msgService: MessageService) { }
34 35 36 37 38 39

    ngOnInit(): void {
        //Value copy
        this.account = Object.assign({}, this.session.getCurrentUser());
    }

Steven Zou's avatar
Steven Zou committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    private isUserDataChange(): boolean {
        if (!this.originalStaticData || !this.account) {
            return false;
        }

        for (var prop in this.originalStaticData) {
            if (this.originalStaticData[prop]) {
                if (this.account[prop]) {
                    if (this.originalStaticData[prop] != this.account[prop]) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

58
    public get isValid(): boolean {
Steven Zou's avatar
Steven Zou committed
59
        return this.accountForm && this.accountForm.valid && this.error === null;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }

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

    ngAfterViewChecked(): void {
        if (this.accountFormRef != this.accountForm) {
            this.accountFormRef = this.accountForm;
            if (this.accountFormRef) {
                this.accountFormRef.valueChanges.subscribe(data => {
                    if (this.error) {
                        this.error = null;
                    }
                    this.formValueChanged = true;
Steven Zou's avatar
Steven Zou committed
75
                    this.inlineAlert.close();
76 77 78 79 80 81
                });
            }
        }
    }

    open() {
Steven Zou's avatar
Steven Zou committed
82 83
        //Keep the initial data for future diff
        this.originalStaticData = Object.assign({}, this.session.getCurrentUser());
84 85 86 87 88 89 90
        this.account = Object.assign({}, this.session.getCurrentUser());
        this.formValueChanged = false;

        this.opened = true;
    }

    close() {
Steven Zou's avatar
Steven Zou committed
91 92 93 94 95 96 97 98 99 100 101 102
        if (this.formValueChanged) {
            if (!this.isUserDataChange()) {
                this.opened = false;
            } else {
                //Need user confirmation
                this.inlineAlert.showInlineConfirmation({
                    message: "ALERT.FORM_CHANGE_CONFIRMATION"
                });
            }
        } else {
            this.opened = false;
        }
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }

    submit() {
        if (!this.isValid || this.isOnCalling) {
            return;
        }

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

        this.isOnCalling = true;

        this.session.updateAccountSettings(this.account)
            .then(() => {
                this.isOnCalling = false;
121
                this.opened = false;
Steven Zou's avatar
Steven Zou committed
122
                this.msgService.announceMessage(200, "PROFILE.SAVE_SUCCESS", AlertType.SUCCESS);
123 124 125
            })
            .catch(error => {
                this.isOnCalling = false;
kunw's avatar
kunw committed
126
                this.error = error;
Steven Zou's avatar
Steven Zou committed
127 128 129 130 131
                if(accessErrorHandler(error, this.msgService)){
                    this.opened = false;
                }else{
                    this.inlineAlert.showInlineError(error);
                }
132 133 134
            });
    }

Steven Zou's avatar
Steven Zou committed
135 136 137 138 139
    confirmCancel(): void {
        this.inlineAlert.close();
        this.opened = false;
    }

140
}