school.home.ts 4.5 KB
Newer Older
1
import {Router, ActivatedRoute, Params} from '@angular/router';
2
import {OnInit, OnDestroy, Component} from '@angular/core';
3 4
import { LoginInfoActions } from '../actions/logininfo.actions';
import { ILoginInfo } from '../store/logininfo/logininfo.types';
5
import { LOGININFO_INITIAL_STATE } from '../store/logininfo/logininfo.initial-state';
6
import { NgRedux, select } from 'ng2-redux';
7
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
8 9
import { IAppState } from '../store/store';
import { HelperDataService } from '../services/helper-data-service';
10
import { CookieService } from 'ngx-cookie';
11 12 13 14 15 16 17
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';

18
import { API_ENDPOINT, API_ENDPOINT_PARAMS } from '../app.settings';
19 20 21 22
@Component({
    selector: 'school-home',
    template: `
  <div>
23
       <form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/cas/login{{apiEndPointParams}}" #form>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<!--            <input type="hidden" name="X-oauth-enabled" value="true"> -->

            <div *ngFor="let loginInfoToken$ of loginInfo$ | async; let i=index"></div>
            <div class="row" style="min-height: 300px; margin-top: 100px;">

            <div *ngIf="!authToken" class="col-md-8 offset-md-4">
                <button type="submit" class="btn-primary btn-lg" (click)="form.submit()">
                Είσοδος μέσω Π.Σ.Δ<span class="glyphicon glyphicon-menu-right"></span>
                </button>
            </div>
            </div>
     </form>
  </div>
  `
})

40
export default class SchoolHome implements OnInit, OnDestroy {
41 42 43 44 45
    public formGroup: FormGroup;
    private authToken: string;
    private authRole: string;
    private name: any;
    private xcsrftoken: any;
46 47
    private loginInfo$: BehaviorSubject<ILoginInfo>;
    private loginInfoSub: Subscription;
48
    private apiEndPoint = API_ENDPOINT;
49
    private apiEndPointParams = API_ENDPOINT_PARAMS;
50 51 52 53 54 55

    constructor(private fb: FormBuilder,
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
        private activatedRoute: ActivatedRoute,
        private _hds: HelperDataService,
56 57
        private router: Router,
        private _cookieService:CookieService
58 59 60 61
    ) {
        this.authToken = '';
        this.authRole = '';
        this.name = '';
62
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
63 64 65 66
        this.formGroup = this.fb.group({
        });
    };

67 68 69 70 71 72
    ngOnDestroy() {
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        this.loginInfo$.unsubscribe();
    };

73
    ngOnInit() {
74
/*        this.authToken = this.getCookie('auth_token');
75 76 77 78 79
        this.authRole = this.getCookie('auth_role');
        if (this.authToken && this.authRole) {
            this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
            this.removeCookie('auth_token');
            this.removeCookie('auth_role');
80
        } */
81

82
        this.loginInfoSub = this._ngRedux.select(state => {
83 84 85 86
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    this.authToken = loginInfoToken.auth_token;
                    this.authRole = loginInfoToken.auth_role;
87 88
                    if (this.authToken && this.authToken.length > 0) {
                        if (this.authRole === 'director') {
Open Source Developer's avatar
trans  
Open Source Developer committed
89
                            this.router.navigate(['/school/director-buttons']);
90 91 92
                        }
                        else if (this.authRole === 'pde')
                            this.router.navigate(['/school/perfecture-view']);
Open Source Developer's avatar
Open Source Developer committed
93 94 95
                        else if (this.authRole === 'dide')
                            this.router.navigate(['/school/eduadmin-view']);
                        
96
                    }
97 98 99 100 101
                    return loginInfoToken;
                }, {});
            }

            return state.loginInfo;
102
        }).subscribe(this.loginInfo$);
103 104 105 106 107 108 109 110 111 112 113 114 115


        // subscribe to router event
        this.activatedRoute.queryParams.subscribe((params: Params) => {
            if (params) {
                this.authToken = params['auth_token'];
                this.authRole = params['auth_role'];
            }

            if (this.authToken && this.authRole)
                this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });

        });
116
    }
117

118 119 120
    getCookie(key: string){
        return this._cookieService.get(key);
    }
121

122 123
    removeCookie(key: string){
        return this._cookieService.remove(key);
124
    }
125

126 127 128 129
    checkvalidation() {

    }
}