home.ts 3.57 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, 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
commit  
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
Open Source Developer's avatar
commit  
Open Source Developer committed
32 33
                </button>
            </div>
34
            </div>
35

36
     </form>
37 38 39
  </div>
  `
})
Open Source Developer's avatar
Open Source Developer committed
40

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

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

66
    ngOnInit() {
67

68 69 70 71 72
        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;
73 74 75 76 77 78 79
                    if (this.authToken && this.authToken.length > 0 && this.authRole && this.authRole === STUDENT_ROLE) {
                        if (loginInfoToken.lock_application === 1)
                            this.router.navigate(['/info']);
                        else {
                            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
}