home.ts 4.78 KB
Newer Older
1 2 3
import { NgRedux } from "@angular-redux/store";
import { Component, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, Params, Router } from "@angular/router";
4
import { CookieService } from "ngx-cookie";
5 6 7 8
import { BehaviorSubject, Subscription } from "rxjs/Rx";

import { LoginInfoActions } from "../actions/logininfo.actions";
import { API_ENDPOINT, API_ENDPOINT_PARAMS } from "../app.settings";
9
import { STUDENT_ROLE } from "../constants";
10
import { HelperDataService } from "../services/helper-data-service";
11
import { LOGININFO_INITIAL_STATE } from "../store/logininfo/logininfo.initial-state";
12 13
import { ILoginInfoRecords } from "../store/logininfo/logininfo.types";
import { IAppState } from "../store/store";
14

15
@Component({
16
    selector: "home",
17
    template: `
18
  <div>
Ασπασία Κατσή's avatar
Ασπασία Κατσή committed
19
       <form  method = "POST" action="{{apiEndPoint}}/oauth/login{{apiEndPointParams}}" #form>
Open Source Developer's avatar
news  
Open Source Developer committed
20 21 22 23
            <div class="bg-warning" style="padding: 2em;">
            <p>
            <strong>Ανακοίνωση:</strong> Παρακαλείσθε να μην καταχωρείτε δήλωση προτίμησης επιλέγοντας τα παρακάτω:</p>
            <ul>
24
                <li> 9ο ΕΠΑ.Λ. Πειραιά, Β τάξη, Τομέας Ναυτιλιακών Επαγγελμάτων, Γ τάξη-Ειδικότητες Πλοίαρχος Εμπορικού Ναυτικού, Μηχανικός Εμπορικού Ναυτικού
Open Source Developer's avatar
news  
Open Source Developer committed
25
                </li>
26
                <li> 1ο ΕΠΑ.Λ. Δάφνης, Γ τάξη, Ειδικότητα Βοηθός Νοσηλευτή
Open Source Developer's avatar
news  
Open Source Developer committed
27
                </li>
28
                <li> 1ο Ημερήσιο ΕΠΑ.Λ. Σιβιτανιδείου, Γ τάξη, Ειδικότητα Βοηθός Νοσηλευτή
Open Source Developer's avatar
news  
Open Source Developer committed
29 30 31 32
                </li>
             </ul>
             <p>Τα τμήματα έχουν πληρότητα από την 1η περίοδο κατανομής.
            </p>
33
            </div>
Open Source Developer's avatar
news  
Open Source Developer committed
34

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

38 39
            <div *ngIf="!authToken" class="col-md-8 offset-md-4">
                <button type="submit" class="btn-primary btn-lg" (click)="form.submit()">
40
                Είσοδος μέσω TaxisNet
Open Source Developer's avatar
commit  
Open Source Developer committed
41 42
                </button>
            </div>
43
            </div>
44

45
     </form>
46 47 48
  </div>
  `
})
Open Source Developer's avatar
Open Source Developer committed
49

50
export default class Home implements OnInit, OnDestroy {
51 52
    private authToken: string;
    private authRole: string;
53 54
    private name: any;
    private xcsrftoken: any;
55
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
56
    private apiEndPoint = API_ENDPOINT;
57
    private apiEndPointParams = API_ENDPOINT_PARAMS;
58
    private loginInfoSub: Subscription;
59

Ασπασία Κατσή's avatar
Ασπασία Κατσή committed
60
    constructor(
61 62
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
63
        private activatedRoute: ActivatedRoute,
64
        private _hds: HelperDataService,
65
        private router: Router,
66
        private _cookieService: CookieService
67
    ) {
68 69 70
        this.authToken = "";
        this.authRole = "";
        this.name = "";
71
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
72
    };
73
    ngOnInit() {
74 75 76
        this.loginInfoSub = this._ngRedux.select("loginInfo")
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
            .subscribe(linfo => {
77
            if (linfo.size > 0) {
78 79 80
                linfo.reduce(({}, loginInfoObj) => {
                    this.authToken = loginInfoObj.auth_token;
                    this.authRole = loginInfoObj.auth_role;
81
                    if (this.authToken && this.authToken.length > 0 && this.authRole && this.authRole === STUDENT_ROLE) {
82
                        if (loginInfoObj.lock_application === 1)
83
                            this.router.navigate(["/info"]);
84
                        else {
85
                            this.router.navigate(["/parent-form"]);
86 87
                        }
                    }
88
                    return loginInfoObj;
89 90
                }, {});
            }
91

92
            this.loginInfo$.next(linfo);
93
        }, error => { console.log("error selecting loginInfo"); });
94 95

        // subscribe to router event
96
        this.activatedRoute.queryParams.subscribe((params: Params) => {
97
            if (params) {
98 99
                this.authToken = params["auth_token"];
                this.authRole = params["auth_role"];
100
            }
101

102
            if (this.authToken && this.authRole)
103
                this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
104

105
        });
106 107
    }

108
    ngOnDestroy() {
109 110 111
        if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
    }

112
    getCookie(key: string) {
113
        return this._cookieService.get(key);
114
    }
115

116
    removeCookie(key: string) {
117 118 119
        return this._cookieService.remove(key);
    }

120
    checkvalidation() {
Open Source Developer's avatar
commit  
Open Source Developer committed
121

122
    }
123
}