parent.form.ts 5.69 KB
Newer Older
1
import { Component, OnInit, OnDestroy, Injectable, ViewChild, ElementRef, Renderer } from '@angular/core';
2
3
import { Router } from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
4
import { VALID_EMAIL_PATTERN, VALID_NAMES_PATTERN } from '../../constants';
5
import { HelperDataService } from '../../services/helper-data-service';
6
import { ModalDirective } from 'ng2-bootstrap/modal';
7
8
9
10
11
12
13
14
15
16
17
18

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators,
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'parent-form',
19
    templateUrl: 'parent.form.html'
20
21
})

22
@Injectable() export default class ParentForm implements OnInit, OnDestroy {
23
24
25
26
27

    public formGroup: FormGroup;
    private respond: any;
    private epalUserData$: BehaviorSubject<any>;
    private epalUserDataSub: Subscription;
28
29
30
31
32
    private userEmailSub: Subscription;
    private verificationCodeSent: BehaviorSubject<boolean>;
    private verificationCodeVerified: BehaviorSubject<boolean>;
    private userEmailEnabled: BehaviorSubject<boolean>;
    @ViewChild('userEmail') userEmail: ElementRef;
33
34
    @ViewChild('autoShownModal') public autoShownModal: ModalDirective;
    public isModalShown: BehaviorSubject<boolean>;
35
36
37

       constructor(private fb: FormBuilder,
                private router: Router,
38
39
40
41
                private hds: HelperDataService,
                private rd: Renderer) {
            this.verificationCodeSent = new BehaviorSubject(false);
            this.verificationCodeVerified = new BehaviorSubject(false);
42

43
            this.userEmailEnabled = new BehaviorSubject(false);
44
            this.isModalShown = new BehaviorSubject(false);
45
46
47
48
49
50
            this.formGroup = this.fb.group({
                 userName: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
                 userSurname: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
                 userFathername: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
                 userMothername: ['', [Validators.pattern(VALID_NAMES_PATTERN),Validators.required]],
                 userEmail: [{value: '', disabled: true}, [Validators.pattern(VALID_EMAIL_PATTERN),Validators.required]],
51
                 verificationCode: ['', []]
52
53
54
                 });
            this.epalUserData$ = new BehaviorSubject({});
        }
55
56
57
58
59
60
61
62
63
64
65
66
67
68

    public showModal():void {
        console.log("about to show modal");
        this.isModalShown.next(true);
    }

    public hideModal():void {
        this.autoShownModal.hide();
    }

    public onHidden():void {
        this.isModalShown.next(false);
    }

69
    ngOnInit() {
70
71
72
        this.epalUserDataSub = this.hds.getEpalUserData().subscribe(
            x => {
                this.epalUserData$.next(x);
73
74
75
76
77
                this.formGroup.get('userEmail').setValue(x.userEmail);
                this.formGroup.get('userName').setValue(x.userName);
                this.formGroup.get('userSurname').setValue(x.userSurname);
                this.formGroup.get('userFathername').setValue(x.userFathername);
                this.formGroup.get('userMothername').setValue(x.userMothername);
78
79
80

                if (typeof(x.verificationCodeVerified) !== 'undefined' && x.verificationCodeVerified === "1") {
                    this.verificationCodeVerified.next(true);
81
82
                } else {
                    this.verificationCodeVerified.next(false);
83
84
85
86
                }

                if (typeof(x.userEmail) !== 'undefined' && x.userEmail.length > 0)
                    this.userEmailEnabled.next(false);
87
88
                else {
                    this.enableUserEmail();
89
                    this.userEmailEnabled.next(true);
90
91
                }

92
93
94
            }
        );

95
        this.userEmailSub = this.formGroup.get('userEmail').valueChanges.subscribe(
96
            x => {
97
                if (this.formGroup.get('userEmail').value === '') {
98
99
100
101
102
103
104
105
106
107
108
109
110
                    this.enableUserEmail();
                }
            }
        );
    }

    ngOnDestroy() {
        if (this.epalUserDataSub) this.epalUserDataSub.unsubscribe();
        if (this.userEmailSub) this.epalUserDataSub.unsubscribe();
    }

    sendVerificationCode() {
        this.hds.sendVerificationCode(this.formGroup.value.userEmail)
111
112
113
            .then(res => {
                this.verificationCodeSent.next(true);
                this.verificationCodeVerified.next(false);
114
                this.disableUserEmail();
115
                //this.showModal();
116
            })
117
118
119
120
121
            .catch(err => {console.log(err)});
    }

    verifyVerificationCode() {
        this.hds.verifyVerificationCode(this.formGroup.value.verificationCode)
122
123
            .then(res => {
                this.verificationCodeVerified.next((<any>res).verificationCodeVerified);
124
                this.formGroup.value.userEmail=(<any>res).userEmail;
125
                //this.showModal();
126
            })
127
            .catch(err => {console.log(err)});
128
129
    }

130
131
132
133
134
    saveProfileAndContinue() {
        this.hds.saveProfile(this.formGroup.value)
            .then(res => {
                this.router.navigate(['/epal-class-select']);})
            .catch(err => {console.log(err)});
135
    }
136
137
138

    enableUserEmail() {
        this.userEmailEnabled.next(true);
139
        this.formGroup.get("userEmail").enable({emitEvent: false});
140
141
142
143
144
        this.rd.invokeElementMethod(this.userEmail.nativeElement,'focus');
    }

    disableUserEmail() {
        this.userEmailEnabled.next(false);
145
146
147
148
149
150
151
        this.formGroup.get("userEmail").disable({emitEvent: false});
    }

    resetUserEmail() {
        this.userEmailEnabled.next(false);
        this.formGroup.get("userEmail").setValue(this.epalUserData$.getValue().userEmail);
        this.formGroup.get("userEmail").disable({emitEvent: false});
152
    }
153
}