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

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

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

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

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

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

61
    ngOnInit() {
62 63 64 65 66
        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;
67
                    if (this.authToken && this.authToken.length > 0 && this.authRole && this.authRole === 'student')
68
                        this.router.navigate(['/parent-form']);
69 70 71
                    return loginInfoToken;
                }, {});
            }
72

73 74 75 76
            return state.loginInfo;
        });

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

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

86 87 88
        });
    }
    checkvalidation() {
Open Source Developer's avatar
commit  
Open Source Developer committed
89

90
    }
91
}