parent.form.ts 4 KB
Newer Older
1
import { Component, OnInit, OnDestroy, Injectable, ViewChild, ElementRef, Renderer } from '@angular/core';
2
import { Router } from '@angular/router';
3
import { BehaviorSubject, Subscription, Observable } from 'rxjs/Rx';
4
import { VALID_EMAIL_PATTERN, VALID_UCASE_NAMES_PATTERN } from '../../constants';
5
import { HelperDataService } from '../../services/helper-data-service';
6
import { LoginInfoActions } from '../../actions/logininfo.actions'
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
    private showLoader: BehaviorSubject<boolean>;
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
31
    public isModalShown: BehaviorSubject<boolean>;
32 33 34

       constructor(private fb: FormBuilder,
                private router: Router,
35
                private hds: HelperDataService,
36
                private _prfa: LoginInfoActions,
37
                private rd: Renderer) {
38
            this.isModalShown = new BehaviorSubject(false);
39
            this.formGroup = this.fb.group({
40 41 42 43
                 userName: ['', [Validators.pattern(VALID_UCASE_NAMES_PATTERN),Validators.required]],
                 userSurname: ['', [Validators.pattern(VALID_UCASE_NAMES_PATTERN),Validators.required]],
                 userFathername: ['', [Validators.pattern(VALID_UCASE_NAMES_PATTERN),Validators.required]],
                 userMothername: ['', [Validators.pattern(VALID_UCASE_NAMES_PATTERN),Validators.required]],
44
                 userEmail: ['', [Validators.pattern(VALID_EMAIL_PATTERN),Validators.required]],
45
                 });
46 47 48 49
            this.epalUserData$ = new BehaviorSubject(<any>{userEmail: '', userName: '', userSurname: '', userFathername: '', userMothername: ''});
            this.showLoader = new BehaviorSubject(false);
            this.modalTitle =  new BehaviorSubject("");
            this.modalText =  new BehaviorSubject("");
50
        }
51 52

    public showModal():void {
53
        (<any>$('#emailSentNotice')).modal('show');
54 55 56
    }

    public hideModal():void {
57
        (<any>$('#emailSentNotice')).modal('hide');
58 59 60 61 62 63
    }

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

64
    ngOnInit() {
65
        (<any>$('#emailSentNotice')).appendTo("body");
66
        this.showLoader.next(true);
67
        this.epalUserDataSub = this.hds.getEpalUserData().subscribe(x => {
68
            this.showLoader.next(false);
69 70 71 72 73 74 75
            this.epalUserData$.next(x);
            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);
        });
76 77 78
    }

    ngOnDestroy() {
79
        (<any>$('#emailSentNotice')).remove();
80 81 82
        if (this.epalUserDataSub) this.epalUserDataSub.unsubscribe();
    }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    saveProfileAndContinue() : void {
        if (!this.formGroup.valid) {
            this.modalTitle.next("Αποτυχία αποθήκευσης");
            this.modalText.next("Δεν συμπληρώσατε κάποιο πεδίο");
            this.showModal();
        } else {
            this.showLoader.next(true);
            this.hds.saveProfile(this.formGroup.value)
                .then(res => {
                    this._prfa.saveProfile(this.formGroup.value);
                    this.showLoader.next(false);
                    this.router.navigate(['/intro-statement']);})
                .catch(err => {
                    this.showLoader.next(false);
                    console.log(err)
                });
        }
100 101
    }

102
}