ministry.home.ts 5.46 KB
Newer Older
1 2 3
import { Router, 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
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 {Http, Response, RequestOptions} from "@angular/http";
10
import { LOGININFO_INITIAL_STATE } from "../store/logininfo/logininfo.initial-state";
11 12 13 14 15 16
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators
17
} from "@angular/forms";
18

19
import { API_ENDPOINT } from "../app.settings";
20 21

@Component({
22
    selector: "ministry-home",
23 24 25
    template: `
  <div>
       <form novalidate [formGroup]="userDataGroup"  #form>
26 27 28
            <div
              class = "loading" *ngIf="validLogin === -1" >
            </div>
29 30 31 32 33 34 35 36 37 38 39 40 41
            <div class="form-group">
                <label for="minedu_username">Όνομα διαχειριστή</label><input class="form-control" type="text" formControlName="minedu_username">
            </div>
            <div class="alert alert-danger" *ngIf="userDataGroup.get('minedu_username').touched && userDataGroup.get('minedu_username').hasError('required')">
                Το πεδίο δεν μπορεί να αφεθεί κενό!
            </div>

            <div class="form-group">
                <label for="minedu_userpassword">Κωδικός πρόσβασης</label><input class="form-control" type="password" formControlName="minedu_userpassword">
            </div>
            <div class="alert alert-danger" *ngIf="userDataGroup.get('minedu_userpassword').touched && userDataGroup.get('minedu_userpassword').hasError('required')">
                Το πεδίο δεν μπορεί να αφεθεί κενό!
            </div>
42 43 44
            <div class="alert alert-danger" *ngIf="!validLogin">
                Λάθος όνομα χρήστη / κωδικός. Παρακαλώ προσπαθήστε ξανά.
            </div>
45 46

            <div *ngFor="let loginInfoToken$ of loginInfo$ | async; let i=index"></div>
47 48 49 50 51 52 53
              <div class="row" style="min-height: 300px; margin-top: 100px;">
              <!--<div *ngIf="!mineduUsername" class="col-md-8 offset-md-4">-->
              <div class="col-md-8 offset-md-4">
                    <button type="submit" class="btn-primary btn-lg" (click)="submitCredentials()" [disabled]="userDataGroup.invalid">
                    Είσοδος<span class="glyphicon glyphicon-menu-right"></span>
                    </button>
                </div>
54
            </div>
55

56 57 58 59 60
     </form>
  </div>
  `
})

61
export default class MinistryHome implements OnInit, OnDestroy {
62
    private userDataGroup: FormGroup;
63 64
    private authRole: string;
    private mineduUsername: string;
65 66
    // private mineduPassword: string;
    // private cuName: string;
67
    private validLogin: number;
68
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
69
    private loginInfoSub: Subscription;
70 71 72 73 74 75 76 77 78 79
    private apiEndPoint = API_ENDPOINT;

    constructor(private fb: FormBuilder,
        private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private http: Http,
        private router: Router
    ) {

80 81
        this.mineduUsername = "";
        this.authRole = "";
82 83
        this.validLogin = 1;

84 85

        this.userDataGroup = this.fb.group({
86 87 88 89
            minedu_username: ["", [Validators.required]],
            minedu_userpassword: ["", [Validators.required]],
            cu_name: [""],
            auth_role: [""],
90
        });
91
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
92 93 94
    };

    ngOnInit() {
95
        this.loginInfoSub = this._ngRedux.select("loginInfo")
96 97
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
            .subscribe(linfo => {
98
                if (linfo.size > 0) {
99 100
                    linfo.reduce(({}, loginInfoObj) => {
                        this.mineduUsername = loginInfoObj.minedu_username;
101
                        if (this.mineduUsername && this.mineduUsername.length > 0)
102
                            this.router.navigate(["/ministry/minister-settings"]);
103
                        return loginInfoObj;
104 105 106
                    }, {});
                }
                this.loginInfo$.next(linfo);
107
            }, error => { console.log("error selecting loginInfo"); });
108 109

    }
110

111 112
    ngOnDestroy() {
        if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
113 114 115
    }

    submitCredentials() {
116
        this.validLogin = -1;
117
        let success = true;
118 119 120 121 122 123 124 125 126 127 128 129 130 131
        this._hds.sendMinisrtyCredentials(this.userDataGroup.value["minedu_username"], this.userDataGroup.value["minedu_userpassword"])
            .catch(err => { console.log(err); success = false; this.validLogin = 0; })
            .then(msg => {
                if (success) {
                    this.authRole = "supervisor";
                    this._hds.setMineduCurrentUser(this.userDataGroup.value["minedu_username"], this.userDataGroup.value["minedu_userpassword"], this.authRole);

                    this.validLogin = 1;
                    this.userDataGroup.value["cu_name"] = this.userDataGroup.value["minedu_username"];
                    this.userDataGroup.value["auth_role"] = "supervisor";
                    this._ata.saveMinEduloginInfo([this.userDataGroup.value]);
                }
            });
    }
132 133

}