header.component.ts 6.37 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
import { BehaviorSubject, Subscription } from "rxjs/Rx";
6
import { NgRedux, select } from "@angular-redux/store";
7
import { IAppState } from "../../store/store";
8
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
9
10
11
12
13
14
15
16
17
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 { StudentDataFieldsActions } from "../../actions/studentdatafields.actions";
18
19

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

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

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

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

64
    };
65
66

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

79
80
81
82
83
84
85
86
87
88
89
90
91
                    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);
                            });
                    }
92
93
94
                }
                this.loginInfo$.next(loginInfo);
            });
95
96
97
98

    }

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

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

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

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

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

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

174
}