ministry.home.ts 5.07 KB
Newer Older
1 2
import { Router, Params} from '@angular/router';
import { OnInit, Component} from '@angular/core';
3 4
import { LoginInfoActions } from '../actions/logininfo.actions';
import { ILoginInfo } from '../store/logininfo/logininfo.types';
5
import { NgRedux, select } from '@angular-redux/store';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import { Observable } from 'rxjs/Rx';
import { IAppState } from '../store/store';
import { HelperDataService } from '../services/helper-data-service';
import {Http, Response, RequestOptions} from '@angular/http';
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators
} from '@angular/forms';

import { API_ENDPOINT } from '../app.settings';

@Component({
    selector: 'ministry-home',
    template: `
  <div>
       <form novalidate [formGroup]="userDataGroup"  #form>
25 26 27
            <div
              class = "loading" *ngIf="validLogin === -1" >
            </div>
28 29 30 31 32 33 34 35 36 37 38 39 40
            <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>
41 42 43
            <div class="alert alert-danger" *ngIf="!validLogin">
                Λάθος όνομα χρήστη / κωδικός. Παρακαλώ προσπαθήστε ξανά.
            </div>
44 45

            <div *ngFor="let loginInfoToken$ of loginInfo$ | async; let i=index"></div>
46 47 48 49 50 51 52
              <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>
53
            </div>
54

55 56 57 58 59 60 61 62 63
     </form>
  </div>
  `
})

export default class MinistryHome implements OnInit {
    public userDataGroup: FormGroup;
    private authRole: string;
    private mineduUsername: string;
64 65
    //private mineduPassword: string;
    //private cuName: string;
66
    private validLogin: number;
67 68 69 70 71 72 73 74 75 76 77 78
    private loginInfo$: Observable<ILoginInfo>;
    private apiEndPoint = API_ENDPOINT;

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

        this.mineduUsername = '';
79
        //this.mineduPassword = '';
80
        this.authRole = '';
81
        //this.cuName = '';
82 83 84
        //this.validLogin = true;
        this.validLogin = 1;

85 86

        this.userDataGroup = this.fb.group({
87 88
          minedu_username: ['', [Validators.required]],
          minedu_userpassword: ['', [Validators.required]],
89 90
          cu_name: [''],
          auth_role: [''],
91 92 93 94 95 96 97 98
        });
    };

    ngOnInit() {
        this.loginInfo$ = this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    this.mineduUsername = loginInfoToken.minedu_username;
99
                    //this.mineduPassword = loginInfoToken.minedu_userpassword;
100
                    if (this.mineduUsername && this.mineduUsername.length > 0)
101
                        this.router.navigate(['/ministry/minister-settings']);
102 103 104 105 106 107 108 109 110
                    return loginInfoToken;
                }, {});
            }
            return state.loginInfo;
        });

    }

    submitCredentials() {
111
        this.validLogin = -1;
112 113
        let success = true;
        this._hds.sendMinisrtyCredentials(this.userDataGroup.value['minedu_username'],this.userDataGroup.value['minedu_userpassword'])
114
          .catch(err => {console.log(err); success = false; this.validLogin = 0; })
115 116 117
          .then(msg => {
            if (success)  {
              this.authRole = 'supervisor';
118
              this._hds.setMineduCurrentUser(this.userDataGroup.value['minedu_username'], this.userDataGroup.value['minedu_userpassword'],   this.authRole);
119

120
              this.validLogin = 1;
121 122
              this.userDataGroup.value['cu_name'] = this.userDataGroup.value['minedu_username'];
              this.userDataGroup.value['auth_role'] = 'supervisor';
123 124 125 126 127 128 129 130
              this._ata.saveMinEduloginInfo([this.userDataGroup.value]);
          }
          });
        }



}