header.component.ts 6.32 KB
Newer Older
1 2 3
import { NgRedux } from "@angular-redux/store";
import { Component, OnDestroy, OnInit } from "@angular/core";
import { Router } from "@angular/router";
4
import { BehaviorSubject, Subscription } from "rxjs/Rx";
5

6
import { EpalClassesActions } from "../../actions/epalclass.actions";
7
import { LoginInfoActions } from "../../actions/logininfo.actions";
8 9
import { RegionSchoolsActions } from "../../actions/regionschools.actions";
import { SectorCoursesActions } from "../../actions/sectorcourses.actions";
10
import { SectorFieldsActions } from "../../actions/sectorfields.actions";
11
import { StudentDataFieldsActions } from "../../actions/studentdatafields.actions";
12 13 14 15 16
import { DIDE_ROLE, MINISTRY_ROLE, PDE_ROLE, SCHOOL_ROLE, STUDENT_ROLE } from "../../constants";
import { HelperDataService } from "../../services/helper-data-service";
import { LOGININFO_INITIAL_STATE } from "../../store/logininfo/logininfo.initial-state";
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
import { IAppState } from "../../store/store";
17 18

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

Open Source Developer's avatar
counter  
Open Source Developer committed
34 35 36
    private TotalStudents$: BehaviorSubject<any>;
    private TotalStudentsSub: Subscription;
    private showLoader: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
37
    private hasvalue: boolean;
38
    private loginInfoSub: Subscription;
Open Source Developer's avatar
counter  
Open Source Developer committed
39

40 41 42 43 44 45 46 47 48 49
    constructor(private _ata: LoginInfoActions,
        private _hds: HelperDataService,
        private _csa: SectorCoursesActions,
        private _sfa: SectorFieldsActions,
        private _rsa: RegionSchoolsActions,
        private _eca: EpalClassesActions,
        private _sdfa: StudentDataFieldsActions,
        private _ngRedux: NgRedux<IAppState>,
        private router: Router
    ) {
50

51 52 53 54 55 56 57 58
        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("");
Open Source Developer's avatar
counter  
Open Source Developer committed
59 60
        this.TotalStudents$ = new BehaviorSubject([{}]);
        this.showLoader = new BehaviorSubject(false);
Open Source Developer's avatar
Open Source Developer committed
61
        this.hasvalue = false;
62

63
    };
64 65

    ngOnInit() {
66
        (<any>$("#headerNotice")).appendTo("body");
67
        this.loginInfoSub = this._ngRedux.select("loginInfo")
68
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
69 70
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
71 72 73 74 75
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.authToken = loginInfoObj.auth_token;
                        this.authRole = loginInfoObj.auth_role;
                        this.cuName = loginInfoObj.cu_name;
                        return loginInfoObj;
76 77
                    }, {});

78 79 80 81 82 83 84 85 86 87 88 89 90
                    if (this.hasvalue === false) {
                        this.showLoader.next(true);
                        this.TotalStudentsSub = this._hds.findTotalStudents().subscribe(x => {
                            this.TotalStudents$.next(x);
                            this.showLoader.next(false);
                            this.hasvalue = true;
                        },
                            error => {
                                this.TotalStudents$.next([{}]);
                                console.log("Error Getting courses perSchool");
                                this.showLoader.next(false);
                            });
                    }
91 92 93
                }
                this.loginInfo$.next(loginInfo);
            });
94 95 96 97

    }

    ngOnDestroy() {
98
        (<any>$("#headerNotice")).remove();
99 100 101
        if (this.loginInfoSub) {
            this.loginInfoSub.unsubscribe();
        }
102 103 104 105 106 107 108
    }

    signOut() {
        this.showLoader$.next(true);
        this._hds.signOut().then(data => {
            this._ata.initLoginInfo();
            if (this.authRole === SCHOOL_ROLE) {
109 110
                this.authToken = "";
                this.authRole = "";
111
                window.location.assign((<any>data).next);
112 113
            }
            else if (this.authRole === PDE_ROLE) {
114 115
                this.authToken = "";
                this.authRole = "";
116
                window.location.assign((<any>data).next);
117 118
            }
            else if (this.authRole === DIDE_ROLE) {
119 120
                this.authToken = "";
                this.authRole = "";
121
                window.location.assign((<any>data).next);
122 123 124 125 126 127 128
            }
            else if (this.authRole === STUDENT_ROLE) {
                this._eca.initEpalClasses();
                this._sfa.initSectorFields();
                this._rsa.initRegionSchools();
                this._csa.initSectorCourses();
                this._sdfa.initStudentDataFields();
129
                this.router.navigate([""]);
130 131
            }
            else if (this.authRole === MINISTRY_ROLE) {
132
                this.router.navigate(["/ministry"]);
133
            }
134 135
            this.authToken = "";
            this.authRole = "";
136 137 138
            this.showLoader$.next(false);
        }).catch(err => {
            this.showLoader$.next(false);
139
            console.log(err);
140 141 142
        });
    }

143 144
    goHome() {
        if (this.authRole === SCHOOL_ROLE) {
145
            this.router.navigate(["/school"]);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
        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"]);
        }
    }

161 162
    gohelpDesk() {
        this.router.navigate(["/help-desk"]);
163 164
    }

165 166
    public showModal(): void {
        (<any>$("#headerNotice")).modal("show");
167 168
    }

169 170
    public hideModal(): void {
        (<any>$("#headerNotice")).modal("hide");
171 172
    }

173
}