header.component.ts 6.62 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;
41
    private loginInfoSub: Subscription;
Open Source Developer's avatar
counter  
Open Source Developer committed
42

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

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

67
    };
68 69

    ngOnInit() {
70
        (<any>$("#headerNotice")).appendTo("body");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        this.loginInfoSub = this._ngRedux.select('loginInfo')
            .map(loginInfo => <ILoginInfo>loginInfo)
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
                    loginInfo.reduce(({}, loginInfoToken) => {
                        this.authToken = loginInfoToken.auth_token;
                        this.authRole = loginInfoToken.auth_role;
                        this.cuName = loginInfoToken.cu_name;
                        return loginInfoToken;
                    }, {});

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

96
                }
Open Source Developer's avatar
counter  
Open Source Developer committed
97

98
                }
99

100 101
                this.loginInfo$.next(loginInfo);
            });
102

Open Source Developer's avatar
counter  
Open Source Developer committed
103 104 105



106 107 108
    }

    ngOnDestroy() {
109
        (<any>$("#headerNotice")).remove();
110 111 112 113
        if (this.loginInfoSub) {
            this.loginInfoSub.unsubscribe();
        }
//        this.loginInfo$.unsubscribe();
114 115 116 117 118 119 120 121

    }

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

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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"]);
        }
    }

178 179
    gohelpDesk()
    {
180
       this.router.navigate(['/help-desk']);
181 182
    }

183 184
    public showModal(): void {
        (<any>$("#headerNotice")).modal("show");
185 186
    }

187 188
    public hideModal(): void {
        (<any>$("#headerNotice")).modal("hide");
189 190
    }

191
}