sign-up.component.ts 2.92 KB
Newer Older
Steven Zou's avatar
Steven Zou committed
1 2 3 4 5 6 7 8 9 10
import { Component, Output, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';

import { NewUserFormComponent } from '../../shared/new-user-form/new-user-form.component';
import { User } from '../../user/user';

import { SessionService } from '../../shared/session.service';
import { UserService } from '../../user/user.service';
import { errorHandler } from '../../shared/shared.utils';
import { InlineAlertComponent } from '../../shared/inline-alert/inline-alert.component';
kunw's avatar
kunw committed
11

12 13
import { Modal } from 'clarity-angular';

kunw's avatar
kunw committed
14 15 16 17 18
@Component({
    selector: 'sign-up',
    templateUrl: "sign-up.component.html"
})
export class SignUpComponent {
Steven Zou's avatar
Steven Zou committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    opened: boolean = false;
    staticBackdrop: boolean = true;
    private error: any;
    private onGoing: boolean = false;
    private formValueChanged: boolean = false;

    constructor(
        private session: SessionService,
        private userService: UserService) { }

    @ViewChild(NewUserFormComponent)
    private newUserForm: NewUserFormComponent;

    @ViewChild(InlineAlertComponent)
    private inlienAlert: InlineAlertComponent;

35 36 37
    @ViewChild(Modal)
    private modal: Modal;

Steven Zou's avatar
Steven Zou committed
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
    private getNewUser(): User {
        return this.newUserForm.getData();
    }

    public get inProgress(): boolean {
        return this.onGoing;
    }

    public get isValid(): boolean {
        return this.newUserForm.isValid && this.error == null;
    }

    formValueChange(flag: boolean): void {
        if (flag) {
            this.formValueChanged = true;
        }
        if (this.error != null) {
            this.error = null;//clear error
        }
        this.inlienAlert.close();//Close alert if being shown
    }

    open(): void {
        this.newUserForm.reset();//Reset form
        this.formValueChanged = false;
63
        this.modal.open();
Steven Zou's avatar
Steven Zou committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }

    close(): void {
        if (this.formValueChanged) {
            if (this.newUserForm.isEmpty()) {
                this.opened = false;
            } else {
                //Need user confirmation
                this.inlienAlert.showInlineConfirmation({
                    message: "ALERT.FORM_CHANGE_CONFIRMATION"
                });
            }
        } else {
            this.opened = false;
        }
    }

    confirmCancel(): void {
82
        this.modal.close();
Steven Zou's avatar
Steven Zou committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    }

    //Create new user
    create(): void {
        //Double confirm everything is ok
        //Form is valid
        if (!this.isValid) {
            return;
        }

        //We have new user data
        let u = this.getNewUser();
        if (!u) {
            return;
        }

        //Start process
        this.onGoing = true;

        this.userService.addUser(u)
            .then(() => {
                this.onGoing = false;
105
                this.modal.close();
Steven Zou's avatar
Steven Zou committed
106 107 108 109 110 111 112
            })
            .catch(error => {
                this.onGoing = false;
                this.error = error;
                this.inlienAlert.showInlineError(error);
            });
    }
kunw's avatar
kunw committed
113
}