home.ts 3.67 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';
10
import { STUDENT_ROLE } from '../constants';
Open Source Developer's avatar
commit  
Open Source Developer committed
11 12 13 14 15 16
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
17 18

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

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

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

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

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

64
    ngOnInit() {
65 66 67 68 69 70 71 72
        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');
        }

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

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

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

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

97 98 99 100 101
        });  */
    }

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

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

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

110
    }
111
}