header.component.ts 5.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import {Component, OnInit, OnDestroy} from '@angular/core';
import {Router} from '@angular/router';
import { Injectable } from "@angular/core";

import { BehaviorSubject } from 'rxjs/Rx';
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { HelperDataService } from '../../services/helper-data-service';
import { LoginInfoActions } from '../../actions/logininfo.actions';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import { SCHOOL_ROLE, STUDENT_ROLE, PDE_ROLE, DIDE_ROLE, MINISTRY_ROLE } from '../../constants';
import { EpalClassesActions } from '../../actions/epalclass.actions';
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { CriteriaActions } from '../../actions/criteria.actions';
import { StudentDataFieldsActions } from '../../actions/studentdatafields.actions';
19 20 21 22 23

@Component({
  selector: 'reg-header',
  templateUrl: 'header.component.html'
})
24 25 26 27 28 29 30
export default class HeaderComponent implements OnInit, OnDestroy {
    private authToken: string;
    private authRole: string;
    private cuName: string;
    private loginInfo$: BehaviorSubject<ILoginInfo>;
    public cuser :any;
    private showLoader$: BehaviorSubject<boolean>;
31 32 33
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    constructor( private _ata: LoginInfoActions,
                private _hds: HelperDataService,
                private _csa: SectorCoursesActions,
                private _sfa: SectorFieldsActions,
                private _rsa: RegionSchoolsActions,
                private _eca: EpalClassesActions,
                private _sdfa: StudentDataFieldsActions,
                private _cria: CriteriaActions,
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
                ) {

                        this.authToken = '';
                        this.authRole = '';
                        this.cuName = '';
                        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
                        this.showLoader$ = new BehaviorSubject(false);
52 53 54
                        this.modalTitle =  new BehaviorSubject("");
                        this.modalText =  new BehaviorSubject("");
                        this.modalHeader =  new BehaviorSubject("");
55 56 57 58

        };

    ngOnInit() {
59
        (<any>$('#headerNotice')).appendTo("body");
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    this.authToken = loginInfoToken.auth_token;
                    this.authRole = loginInfoToken.auth_role;
                    this.cuName = loginInfoToken.cu_name;
                    return loginInfoToken;
                }, {})
            }

            return state.loginInfo;
        }).subscribe(this.loginInfo$);

    }

    ngOnDestroy() {
76
        (<any>$('#headerNotice')).remove();
77 78 79 80 81 82 83 84 85
        this.loginInfo$.unsubscribe();

    }

    signOut() {
        this.showLoader$.next(true);
        this._hds.signOut().then(data => {
            this._ata.initLoginInfo();
            if (this.authRole === SCHOOL_ROLE) {
86 87 88 89
                // this.router.navigate(['/school']);
                this.authToken = '';
                this.authRole = '';
                window.location.assign((<any> data).next);
90 91
            }
            else if (this.authRole === PDE_ROLE) {
92 93 94 95
                // this.router.navigate(['/school']);
                this.authToken = '';
                this.authRole = '';
                window.location.assign((<any> data).next);
96 97
            }
            else if (this.authRole === DIDE_ROLE) {
98 99 100 101
                // this.router.navigate(['/school']);
                this.authToken = '';
                this.authRole = '';
                window.location.assign((<any> data).next);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            }
            else if (this.authRole === STUDENT_ROLE) {
                this._eca.initEpalClasses();
                this._sfa.initSectorFields();
                this._rsa.initRegionSchools();
                this._csa.initSectorCourses();
                this._sdfa.initStudentDataFields();
                this._cria.initCriteria();
                this.router.navigate(['']);
            }
            else if (this.authRole === MINISTRY_ROLE) {
                this.router.navigate(['/ministry']);
            }
            this.authToken = '';
            this.authRole = '';
            this.showLoader$.next(false);
        }).catch(err => {
            this.showLoader$.next(false);
            console.log(err)
        });
    }

124 125 126 127 128 129 130 131
    public showModal():void {
        (<any>$('#headerNotice')).modal('show');
    }

    public hideModal():void {
        (<any>$('#headerNotice')).modal('hide');
    }

132
}