parent.form.ts 5.75 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
6
7
8
9
10
11
12
13
14
15
16
17
import { HelperDataService } from '../../services/helper-data-service';

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

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

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

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

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

41
            this.userEmailEnabled = new BehaviorSubject(false);
42
            this.isModalShown = new BehaviorSubject(false);
43
44
45
46
47
48
            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]],
49
                 verificationCode: ['', []]
50
51
52
                 });
            this.epalUserData$ = new BehaviorSubject({});
        }
53
54
55

    public showModal():void {
        console.log("about to show modal");
56
        (<any>$('#emaiSentNotice')).modal('show');
57
58
59
    }

    public hideModal():void {
60
        (<any>$('#emaiSentNotice')).modal('hide');
61
62
63
64
65
66
    }

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

67
    ngOnInit() {
68
69
        (<any>$('#emaiSentNotice')).appendTo("body");

70
71
        this.epalUserDataSub = this.hds.getEpalUserData().subscribe(
            x => {
72

73
                this.epalUserData$.next(x);
74
75
76
77
78
                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);
79
80
81

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

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

93
94
95
            }
        );

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

    ngOnDestroy() {
106
        (<any>$('#emaiSentNotice')).remove();
107
108
109
110
111
112
        if (this.epalUserDataSub) this.epalUserDataSub.unsubscribe();
        if (this.userEmailSub) this.epalUserDataSub.unsubscribe();
    }

    sendVerificationCode() {
        this.hds.sendVerificationCode(this.formGroup.value.userEmail)
113
114
115
            .then(res => {
                this.verificationCodeSent.next(true);
                this.verificationCodeVerified.next(false);
116
                this.disableUserEmail();
117
118
                this.showModal();
//                (<any>$('#emaiSentNotice')).modal('show');
119
            })
120
121
122
123
124
            .catch(err => {console.log(err)});
    }

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

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

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

    disableUserEmail() {
        this.userEmailEnabled.next(false);
148
149
150
151
152
153
154
        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});
155
    }
156
}