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

Open Source Developer's avatar
counter  
Open Source Developer committed
5
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
6
import { NgRedux, select } from "@angular-redux/store";
7 8 9 10 11 12 13 14 15 16 17 18
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

Open Source Developer's avatar
counter  
Open Source Developer committed
20

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

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

42 43 44 45 46 47 48 49 50 51 52
    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
    ) {
53

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

66
    };
67 68

    ngOnInit() {
69
        (<any>$("#headerNotice")).appendTo("body");
70 71 72 73 74 75 76
        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;
77
                }, {});
Open Source Developer's avatar
counter  
Open Source Developer committed
78

Open Source Developer's avatar
Open Source Developer committed
79 80
            if (this.hasvalue == false)
            {
Open Source Developer's avatar
counter  
Open Source Developer committed
81 82 83 84
            this.showLoader.next(true);
            this.TotalStudentsSub = this._hds.findTotalStudents().subscribe(x => {
            this.TotalStudents$.next(x);
            this.showLoader.next(false);
85
            this.hasvalue = true;
Open Source Developer's avatar
Open Source Developer committed
86
            },
Open Source Developer's avatar
counter  
Open Source Developer committed
87 88 89 90 91 92
            error => {
                this.TotalStudents$.next([{}]);
                console.log("Error Getting courses perSchool");
                this.showLoader.next(false);
            });

Open Source Developer's avatar
Open Source Developer committed
93
            }
Open Source Developer's avatar
counter  
Open Source Developer committed
94

95 96 97 98 99
            }

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

Open Source Developer's avatar
counter  
Open Source Developer committed
100 101 102



103 104 105
    }

    ngOnDestroy() {
106
        (<any>$("#headerNotice")).remove();
107 108 109 110 111 112 113 114 115
        this.loginInfo$.unsubscribe();

    }

    signOut() {
        this.showLoader$.next(true);
        this._hds.signOut().then(data => {
            this._ata.initLoginInfo();
            if (this.authRole === SCHOOL_ROLE) {
116
                // this.router.navigate(['/school']);
117 118
                this.authToken = "";
                this.authRole = "";
119
                window.location.assign((<any>data).next);
120 121
            }
            else if (this.authRole === PDE_ROLE) {
122 123 124
                // this.router.navigate(["/school"]);
                this.authToken = "";
                this.authRole = "";
125
                window.location.assign((<any>data).next);
126 127
            }
            else if (this.authRole === DIDE_ROLE) {
128 129 130
                // this.router.navigate(["/school"]);
                this.authToken = "";
                this.authRole = "";
131
                window.location.assign((<any>data).next);
132 133 134 135 136 137 138 139
            }
            else if (this.authRole === STUDENT_ROLE) {
                this._eca.initEpalClasses();
                this._sfa.initSectorFields();
                this._rsa.initRegionSchools();
                this._csa.initSectorCourses();
                this._sdfa.initStudentDataFields();
                this._cria.initCriteria();
140
                this.router.navigate([""]);
141 142
            }
            else if (this.authRole === MINISTRY_ROLE) {
143
                this.router.navigate(["/ministry"]);
144
            }
145 146
            this.authToken = "";
            this.authRole = "";
147 148 149
            this.showLoader$.next(false);
        }).catch(err => {
            this.showLoader$.next(false);
150
            console.log(err);
151 152 153
        });
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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"]);
        }
    }

172 173
    gohelpDesk()
    {
174
       this.router.navigate(['/help-desk']);
175 176
    }

177 178
    public showModal(): void {
        (<any>$("#headerNotice")).modal("show");
179 180
    }

181 182
    public hideModal(): void {
        (<any>$("#headerNotice")).modal("hide");
183 184
    }

185
}