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

import { API_ENDPOINT } from '../app.settings';
18
@Component({
19 20
    selector: 'home',
    template: `
21
  <div>
Open Source Developer's avatar
Open Source Developer committed
22 23
       <form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/oauth/login" #form>
<!--            <input type="hidden" name="X-oauth-enabled" value="true"> -->
24 25 26

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

28 29
            <div *ngIf="!authToken" class="col-md-8 offset-md-4">
                <button type="submit" class="btn-primary btn-lg" (click)="form.submit()">
30
                Είσοδος μέσω TaxisNet<span class="glyphicon glyphicon-menu-right"></span>
Open Source Developer's avatar
Open Source Developer committed
31 32
                </button>
            </div>
33
            </div>
34
     </form>
35 36 37
  </div>
  `
})
Open Source Developer's avatar
Open Source Developer committed
38

39 40 41 42
export default class Home implements OnInit {
    public formGroup: FormGroup;
    private authToken: string;
    private authRole: string;
43 44
    private name: any;
    private xcsrftoken: any;
45
    private loginInfo$: Observable<ILoginInfo>;
46 47
    private apiEndPoint = API_ENDPOINT;

48 49 50
    constructor(private fb: FormBuilder,
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
51
        private activatedRoute: ActivatedRoute,
52
        private _hds: HelperDataService,
53 54
        private router: Router,
        private _cookieService:CookieService
55 56 57 58
    ) {
        this.authToken = '';
        this.authRole = '';
        this.name = '';
59 60 61
        this.formGroup = this.fb.group({
        });
    };
Open Source Developer's avatar
Open Source Developer committed
62

63
    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
        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;
77
                    if (this.authToken && this.authToken.length > 0 && this.authRole && this.authRole === 'student')
78
                        this.router.navigate(['/parent-form']);
79 80 81
                    return loginInfoToken;
                }, {});
            }
82

83 84 85 86
            return state.loginInfo;
        });

        // subscribe to router event
87
/*        this.activatedRoute.queryParams.subscribe((params: Params) => {
88 89 90 91
            if (params) {
                this.authToken = params['auth_token'];
                this.authRole = params['auth_role'];
            }
92

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

96 97 98 99 100
        });  */
    }

    getCookie(key: string){
        return this._cookieService.get(key);
101
    }
102 103 104 105 106

    removeCookie(key: string){
        return this._cookieService.remove(key);
    }

107
    checkvalidation() {
Open Source Developer's avatar
Open Source Developer committed
108

109
    }
110
}