minister-view.ts 4.05 KB
Newer Older
1 2 3 4
import { Component, OnInit, OnDestroy, ElementRef, ViewChild} from "@angular/core";
import { Injectable } from "@angular/core";
import { AppSettings } from '../../app.settings';
import { HelperDataService } from '../../services/helper-data-service';
5 6
import { Observable} from "rxjs/Observable";
import { Http, Headers, RequestOptions} from '@angular/http';
7 8
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
9
import { Router, ActivatedRoute, Params} from '@angular/router';
10 11
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
12 13
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators,
} from '@angular/forms';

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

@Component({
    selector: 'minister-view',
    template: `

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    <div
      class = "loading" *ngIf=" distStatus === 'STARTED'" >
    </div>
    <div class="alert alert-info" *ngIf="distStatus === 'STARTED'">
      Παρακαλώ περιμένετε...Η εκτέλεση της κατανομής ενδέχεται να διαρκέσει μερικά λεπτά. Παρακαλώ μην εκτελείται οποιαδήποτε ενέργεια μετακίνησης στον φυλλομετρητή σας, μέχρι να ολοκληρωθεί η κατανομή.
    </div>
    <div class="alert alert-info" *ngIf="distStatus === 'FINISHED'">
      Η κατανομή ολοκληρώθηκε με επιτυχία!
    </div>
    <div class="alert alert-info" *ngIf="distStatus === 'ERROR'">
      Αποτυχία κατανομής!
    </div>

  <div>
      <!--
44
      <form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/epal/distribution" #form>
45 46 47 48 49
      -->
      <form [formGroup]="formGroup"  #form>
        <!--<div *ngFor="let loginInfoToken$ of loginInfo$ | async; let i=index"></div>-->
        <!--<button type="submit" class="btn-primary btn-md" (click)="form.submit()" >-->
        <button type="submit" class="btn-primary btn-md"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="runDistribution()" >
50 51 52 53 54 55 56 57 58 59 60
            Εκτέλεση Κατανομής Μαθητών
        </button>
      </form>
    </div>

   `
})

@Injectable() export default class MinisterView implements OnInit, OnDestroy {

    public formGroup: FormGroup;
61 62 63
    //private loginInfo$: Observable<ILoginInfo>;
    loginInfo$: BehaviorSubject<ILoginInfo>;
    loginInfoSub: Subscription;
64
    private apiEndPoint = API_ENDPOINT;
65 66 67
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
68 69

    constructor(private fb: FormBuilder,
70 71
      //  private _ata: LoginInfoActions,
        private _ngRedux: NgRedux<IAppState>,
72 73 74 75 76 77 78 79
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {

          this.formGroup = this.fb.group({

          });

80 81
          this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);

82 83 84 85
    }

    ngOnDestroy() {

86 87 88
      if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
      this.loginInfo$.unsubscribe();

89 90 91 92
    }

    ngOnInit() {

93 94 95 96 97 98 99 100 101 102 103
      this.loginInfoSub = this._ngRedux.select(state => {
          if (state.loginInfo.size > 0) {
              state.loginInfo.reduce(({}, loginInfoToken) => {
                this.minedu_userName = loginInfoToken.minedu_username;
                this.minedu_userPassword = loginInfoToken.minedu_userpassword;
                  return loginInfoToken;
              }, {});
          }
          return state.loginInfo;
      }).subscribe(this.loginInfo$);

104 105 106 107 108 109
    }




    runDistribution() {
110 111 112 113 114 115 116 117
      this.distStatus = "STARTED";
      this._hds.makeDistribution(this.minedu_userName, this.minedu_userPassword)
      .catch(err => {console.log(err); this.distStatus = "ERROR"; })
      .then(msg => {
          console.log("KATANOMH TELEIOSE");
          if (this.distStatus !== "ERROR")
            this.distStatus = "FINISHED";
      });
118 119 120 121 122
    }



}