home.ts 5.09 KB
Newer Older
1 2 3
import {Router, ActivatedRoute, Params} from "@angular/router";
import {OnInit, OnDestroy, Component} from "@angular/core";
import { LoginInfoActions } from "../actions/logininfo.actions";
4
import { ILoginInfoRecords } from "../store/logininfo/logininfo.types";
5 6 7 8 9 10
import { NgRedux, select } from "@angular-redux/store";
import { BehaviorSubject, Subscription } from "rxjs/Rx";
import { IAppState } from "../store/store";
import { HelperDataService } from "../services/helper-data-service";
import { CookieService } from "ngx-cookie";
import { STUDENT_ROLE } from "../constants";
11
import { LOGININFO_INITIAL_STATE } from "../store/logininfo/logininfo.initial-state";
Open Source Developer's avatar
Open Source Developer committed
12 13 14 15 16
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
17
} from "@angular/forms";
18

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

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

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

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

52
     </form>
53 54 55
  </div>
  `
})
Open Source Developer's avatar
Open Source Developer committed
56

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

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

84
    ngOnInit() {
85

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

104
            this.loginInfo$.next(linfo);
105
        }, error => { console.log("error selecting loginInfo"); });
106 107

        // subscribe to router event
108
        this.activatedRoute.queryParams.subscribe((params: Params) => {
109
            if (params) {
110 111
                this.authToken = params["auth_token"];
                this.authRole = params["auth_role"];
112
            }
113

114
            if (this.authToken && this.authRole)
115
                this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
116

117
        });
118 119
    }

120
    ngOnDestroy() {
121 122 123
        if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
    }

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

128
    removeCookie(key: string) {
129 130 131
        return this._cookieService.remove(key);
    }

132
    checkvalidation() {
Open Source Developer's avatar
Open Source Developer committed
133

134
    }
135
}