navbar.component.ts 2.33 KB
Newer Older
1
import {Component, OnInit, OnDestroy} from '@angular/core';
2
import {Router} from '@angular/router';
Open Source Developer's avatar
Open Source Developer committed
3
import { Injectable } from "@angular/core";
Open Source Developer's avatar
Open Source Developer committed
4

5
import { BehaviorSubject } from 'rxjs/Rx';
6
import { NgRedux, select } from '@angular-redux/store';
7 8
import { IAppState } from '../../store/store';
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
Open Source Developer's avatar
Open Source Developer committed
9
import { LoginInfoActions } from '../../actions/logininfo.actions';
10
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
11 12 13 14 15

@Component({
  selector: 'reg-navbar',
  templateUrl: 'navbar.component.html',
})
Open Source Developer's avatar
Open Source Developer committed
16

17
@Injectable() export default class NavbarComponent implements OnInit, OnDestroy{
18 19
    private authToken: string;
    private authRole: string;
20 21
    private lockCapacity: BehaviorSubject<boolean>;
    private lockStudents: BehaviorSubject<boolean>;
22 23
    private cuName: string;
    private loginInfo$: BehaviorSubject<ILoginInfo>;
24
 	public cuser :any;
Open Source Developer's avatar
Open Source Developer committed
25

26
    constructor( private _ngRedux: NgRedux<IAppState>
27 28 29 30
                ) {

                        this.authToken = '';
                        this.authRole = '';
31 32
                        this.lockCapacity = new BehaviorSubject(true);
                        this.lockStudents = new BehaviorSubject(true);
33 34
                        this.cuName = '';
                        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
Open Source Developer's avatar
Open Source Developer committed
35 36 37 38

        };

    ngOnInit() {
39
        this._ngRedux.select(state => {
40 41
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
42 43
                    this.authToken = loginInfoToken.auth_token;
                    this.authRole = loginInfoToken.auth_role;
44 45 46 47 48 49 50 51
                    if (loginInfoToken.lock_capacity === 1)
                        this.lockCapacity.next(true);
                    else
                        this.lockCapacity.next(false);
                    if (loginInfoToken.lock_students === 1)
                        this.lockStudents.next(true);
                    else
                        this.lockStudents.next(false);
52
                    this.cuName = loginInfoToken.cu_name;
53
                    return loginInfoToken;
54
                }, {})
55
            }
56

57
            return state.loginInfo;
58 59 60
        }).subscribe(this.loginInfo$);

    }
61

62 63
    ngOnDestroy() {
        this.loginInfo$.unsubscribe();
64

Open Source Developer's avatar
Open Source Developer committed
65 66
    }

67
}