header.component.ts 5.47 KB
Newer Older
1 2
import {Component, OnInit, OnDestroy} from "@angular/core";
import {Router} from "@angular/router";
3 4
import { Injectable } from "@angular/core";

5 6 7 8 9 10 11 12 13 14 15 16 17 18
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

@Component({
21 22
    selector: "reg-header",
    templateUrl: "header.component.html"
23
})
24 25
export default class HeaderComponent implements OnInit, OnDestroy {
    private authToken: string;
26
    private studentRole = STUDENT_ROLE;
27 28 29
    private authRole: string;
    private cuName: string;
    private loginInfo$: BehaviorSubject<ILoginInfo>;
30
    public cuser: any;
31
    private showLoader$: BehaviorSubject<boolean>;
32 33 34
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
35

36 37 38 39 40 41 42 43 44 45 46
    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
    ) {
47

48 49 50 51 52 53 54 55
        this.authToken = "";
        this.authRole = "";
        this.cuName = "";
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.showLoader$ = new BehaviorSubject(false);
        this.modalTitle = new BehaviorSubject("");
        this.modalText = new BehaviorSubject("");
        this.modalHeader = new BehaviorSubject("");
56

57
    };
58 59

    ngOnInit() {
60
        (<any>$("#headerNotice")).appendTo("body");
61 62 63 64 65 66 67
        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;
68
                }, {});
69 70 71 72 73 74 75 76
            }

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

    }

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

    }

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

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    goHome() {
        if (this.authRole === SCHOOL_ROLE) {
            this.router.navigate(['/school']);
        }
        else if (this.authRole === PDE_ROLE) {
            this.router.navigate(["/school"]);
        }
        else if (this.authRole === DIDE_ROLE) {
            this.router.navigate(["/school"]);
        }
        else if (this.authRole === STUDENT_ROLE) {
            this.router.navigate([""]);
        }
        else if (this.authRole === MINISTRY_ROLE) {
            this.router.navigate(["/ministry"]);
        }
    }

143 144
    gohelpDesk()
    {
145
       this.router.navigate(['/help-desk']);
146 147
    }

148 149
    public showModal(): void {
        (<any>$("#headerNotice")).modal("show");
150 151
    }

152 153
    public hideModal(): void {
        (<any>$("#headerNotice")).modal("hide");
154 155
    }

156
}