school.home.ts 3.18 KB
Newer Older
1 2 3 4 5 6 7 8
import {Router, ActivatedRoute, Params} from '@angular/router';
import {OnInit, Component} from '@angular/core';
import { LoginInfoActions } from '../actions/logininfo.actions';
import { ILoginInfo } from '../store/logininfo/logininfo.types';
import { NgRedux, select } from 'ng2-redux';
import { Observable } from 'rxjs/Rx';
import { IAppState } from '../store/store';
import { HelperDataService } from '../services/helper-data-service';
9
import { CookieService } from 'ngx-cookie';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';

import { API_ENDPOINT } from '../app.settings';
@Component({
    selector: 'school-home',
    template: `
  <div>
       <form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/cas/login" #form>
<!--            <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>
  `
})

export default class SchoolHome implements OnInit {
    public formGroup: FormGroup;
    private authToken: string;
    private authRole: string;
    private name: any;
    private xcsrftoken: any;
    private loginInfo$: Observable<ILoginInfo>;
    private apiEndPoint = API_ENDPOINT;

    constructor(private fb: FormBuilder,
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
        private activatedRoute: ActivatedRoute,
        private _hds: HelperDataService,
53 54
        private router: Router,
        private _cookieService:CookieService
55 56 57 58 59 60 61 62 63
    ) {
        this.authToken = '';
        this.authRole = '';
        this.name = '';
        this.formGroup = this.fb.group({
        });
    };

    ngOnInit() {
64 65 66 67 68 69 70 71
        this.authToken = this.getCookie('auth_token');
        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');
        }

72 73 74 75 76 77
        this.loginInfo$ = this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    this.authToken = loginInfoToken.auth_token;
                    this.authRole = loginInfoToken.auth_role;
                    if (this.authToken && this.authToken.length > 0)
78
                        this.router.navigate(['/school/director-view']);
79 80 81 82 83 84
                    return loginInfoToken;
                }, {});
            }

            return state.loginInfo;
        });
85
    }
86

87 88 89
    getCookie(key: string){
        return this._cookieService.get(key);
    }
90

91 92
    removeCookie(key: string){
        return this._cookieService.remove(key);
93
    }
94

95 96 97 98
    checkvalidation() {

    }
}