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

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

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

Open Source Developer's avatar
news  
Open Source Developer committed
23 24 25 26
            <div class="bg-warning" style="padding: 2em;">
            <p>
            <strong>Ανακοίνωση:</strong> Παρακαλείσθε να μην καταχωρείτε δήλωση προτίμησης επιλέγοντας τα παρακάτω:</p>
            <ul>
27
                <li> 9ο ΕΠΑ.Λ. Πειραιά, Β τάξη, Τομέας Ναυτιλιακών Επαγγελμάτων, Γ τάξη-Ειδικότητες Πλοίαρχος Εμπορικού Ναυτικού, Μηχανικός Εμπορικού Ναυτικού
Open Source Developer's avatar
news  
Open Source Developer committed
28
                </li>
29
                <li> 1ο ΕΠΑ.Λ. Δάφνης, Γ τάξη, Ειδικότητα Βοηθός Νοσηλευτή
Open Source Developer's avatar
news  
Open Source Developer committed
30
                </li>
31
                <li> 1ο Ημερήσιο ΕΠΑ.Λ. Σιβιτανιδείου, Γ τάξη, Ειδικότητα Βοηθός Νοσηλευτή
Open Source Developer's avatar
news  
Open Source Developer committed
32 33 34 35
                </li>
             </ul>
             <p>Τα τμήματα έχουν πληρότητα από την 1η περίοδο κατανομής.
            </p>
36
            </div>
Open Source Developer's avatar
news  
Open Source Developer committed
37

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

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

48
     </form>
49 50 51
  </div>
  `
})
Open Source Developer's avatar
Open Source Developer committed
52

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

64 65 66
    constructor(private fb: FormBuilder,
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
67
        private activatedRoute: ActivatedRoute,
68
        private _hds: HelperDataService,
69
        private router: Router,
70
        private _cookieService: CookieService
71
    ) {
72 73 74
        this.authToken = "";
        this.authRole = "";
        this.name = "";
75 76
        this.formGroup = this.fb.group({
        });
77
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
78
    };
Open Source Developer's avatar
commit  
Open Source Developer committed
79

80
    ngOnInit() {
81

82 83 84
        this.loginInfoSub = this._ngRedux.select("loginInfo")
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
            .subscribe(linfo => {
85
            if (linfo.size > 0) {
86 87 88
                linfo.reduce(({}, loginInfoObj) => {
                    this.authToken = loginInfoObj.auth_token;
                    this.authRole = loginInfoObj.auth_role;
89
                    if (this.authToken && this.authToken.length > 0 && this.authRole && this.authRole === STUDENT_ROLE) {
90
                        if (loginInfoObj.lock_application === 1)
91
                            this.router.navigate(["/info"]);
92
                        else {
93
                            this.router.navigate(["/parent-form"]);
94 95
                        }
                    }
96
                    return loginInfoObj;
97 98
                }, {});
            }
99

100
            this.loginInfo$.next(linfo);
101
        }, error => { console.log("error selecting loginInfo"); });
102 103

        // subscribe to router event
104
        this.activatedRoute.queryParams.subscribe((params: Params) => {
105
            if (params) {
106 107
                this.authToken = params["auth_token"];
                this.authRole = params["auth_role"];
108
            }
109

110
            if (this.authToken && this.authRole)
111
                this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
112

113
        });
114 115
    }

116
    ngOnDestroy() {
117 118 119
        if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
    }

120
    getCookie(key: string) {
121
        return this._cookieService.get(key);
122
    }
123

124
    removeCookie(key: string) {
125 126 127
        return this._cookieService.remove(key);
    }

128
    checkvalidation() {
Open Source Developer's avatar
commit  
Open Source Developer committed
129

130
    }
131
}