parent.form.ts 5.1 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
33
34

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

40
41
42
43
44
45
46
            this.userEmailEnabled = new BehaviorSubject(false);
            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]],
47
                 verificationCode: ['', []]
48
49
50
                 });
            this.epalUserData$ = new BehaviorSubject({});
        }
51
    ngOnInit() {
52
53
54
        this.epalUserDataSub = this.hds.getEpalUserData().subscribe(
            x => {
                this.epalUserData$.next(x);
55
56
57
58
59
                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);
60
61
62

                if (typeof(x.verificationCodeVerified) !== 'undefined' && x.verificationCodeVerified === "1") {
                    this.verificationCodeVerified.next(true);
63
64
                } else {
                    this.verificationCodeVerified.next(false);
65
66
67
68
                }

                if (typeof(x.userEmail) !== 'undefined' && x.userEmail.length > 0)
                    this.userEmailEnabled.next(false);
69
70
                else {
                    this.enableUserEmail();
71
                    this.userEmailEnabled.next(true);
72
73
                }

74
75
76
            }
        );

77
        this.userEmailSub = this.formGroup.get('userEmail').valueChanges.subscribe(
78
            x => {
79
                if (this.formGroup.get('userEmail').value === '') {
80
81
82
83
84
85
86
87
88
89
90
91
92
                    this.enableUserEmail();
                }
            }
        );
    }

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

    sendVerificationCode() {
        this.hds.sendVerificationCode(this.formGroup.value.userEmail)
93
94
95
96
            .then(res => {
                this.verificationCodeSent.next(true);
                this.verificationCodeVerified.next(false);
                this.disableUserEmail();})
97
98
99
100
101
            .catch(err => {console.log(err)});
    }

    verifyVerificationCode() {
        this.hds.verifyVerificationCode(this.formGroup.value.verificationCode)
102
103
104
            .then(res => {
                this.verificationCodeVerified.next((<any>res).verificationCodeVerified);
                this.formGroup.value.userEmail=(<any>res).userEmail;})
105
            .catch(err => {console.log(err)});
106
107
    }

108
109
110
111
112
    saveProfileAndContinue() {
        this.hds.saveProfile(this.formGroup.value)
            .then(res => {
                this.router.navigate(['/epal-class-select']);})
            .catch(err => {console.log(err)});
113
    }
114
115
116

    enableUserEmail() {
        this.userEmailEnabled.next(true);
117
        this.formGroup.get("userEmail").enable({emitEvent: false});
118
119
120
121
122
        this.rd.invokeElementMethod(this.userEmail.nativeElement,'focus');
    }

    disableUserEmail() {
        this.userEmailEnabled.next(false);
123
124
125
126
127
128
129
        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});
130
    }
131
}