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

18
import { API_ENDPOINT, API_ENDPOINT_PARAMS } from '../app.settings';
19
@Component({
20 21
    selector: 'home',
    template: `
22
  <div>
23
       <form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/oauth/login{{apiEndPointParams}}" #form>
Open Source Developer's avatar
Open Source Developer committed
24
<!--            <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
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
    private apiEndPoint = API_ENDPOINT;
48
    private apiEndPointParams = API_ENDPOINT_PARAMS;
49

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

65
    ngOnInit() {
66
/*        this.authToken = this.getCookie('auth_token');
67
        this.authRole = this.getCookie('auth_role');
68 69
        console.log(this.authToken);
        console.log(this.authRole);
70 71 72 73
        if (this.authToken && this.authRole) {
            this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
            this.removeCookie('auth_token');
            this.removeCookie('auth_role');
74
        } */
75

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

87 88 89 90
            return state.loginInfo;
        });

        // subscribe to router event
91
        this.activatedRoute.queryParams.subscribe((params: Params) => {
92 93 94
            if (params) {
                this.authToken = params['auth_token'];
                this.authRole = params['auth_role'];
95 96
//                console.log(this.authToken);
//                console.log(this.authRole);
97
            }
98

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

102
        });
103 104 105 106
    }

    getCookie(key: string){
        return this._cookieService.get(key);
107
    }
108 109 110 111 112

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

113
    checkvalidation() {
Open Source Developer's avatar
Open Source Developer committed
114

115
    }
116
}