minister-informstudents.ts 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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';
import { Observable} from "rxjs/Observable";
import { Http, Headers, RequestOptions} from '@angular/http';
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
import { Router, ActivatedRoute, Params} from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';


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

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

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

28 29 30 31
    <div
      class = "loading" *ngIf="successSending == -2" >
    </div>
    <div class="alert alert-success" *ngIf="successSending == 1 ">
32 33 34
      Έγινε αποστολή {{numSuccessMails}} e-mails!
    </div>
    <div class="alert alert-warning" *ngIf="successSending == 0">
35 36 37 38 39
      Αποτυχία αποστολής e-mails!
    </div>
    <div class="alert alert-warning" *ngIf="numFailMails != 0">
      Κάποια e-mail δεν έχουν σταλεί.
      Δεν ήταν δυνατή η αποστολή {{numFailMails}} e-mails!
40 41 42 43 44 45 46 47
    </div>

    <div class="col-md-8 offset-md-4">
      <button type="submit" class="btn-primary btn-md"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="informUnlocatedStudents()" >
          Μαζική αποστολή e-mail στους μαθητές που δεν τοποθετήθηκαν<span class="glyphicon glyphicon-menu-right"></span>
      </button>
    </div>

48 49 50 51 52 53 54 55 56 57 58


   `
})

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

    public formGroup: FormGroup;
    //private loginInfo$: Observable<ILoginInfo>;
    loginInfo$: BehaviorSubject<ILoginInfo>;
    loginInfoSub: Subscription;
59
    private numSuccessMails:number;
60
    private numFailMails:number;
61
    private successSending:number;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;

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

          this.formGroup = this.fb.group({

          });

          this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);

    }

    ngOnDestroy() {

      if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
      this.loginInfo$.unsubscribe();

    }

    ngOnInit() {

      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$);

101
      this.numSuccessMails = 0;
102
      this.numFailMails = 0;
103 104 105 106 107 108 109 110 111 112 113 114 115 116
      this.successSending = -1;

    }

    informUnlocatedStudents() {

      /*
      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword)
      .catch(err => {console.log(err);   })
      .then(msg => {
          console.log("Success");
      });
      */

117 118 119 120
      this.successSending = -2;
      this.numSuccessMails = 0;
      this.numFailMails = 0;

121 122 123 124
      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
          //this.data = data;
          //this.successSending = 0;
          this.numSuccessMails = data.num_success_mail;
125 126 127
          this.numFailMails = data.num_fail_mail;
          //console.log("HERE!");
          //console.log(this.numSuccessMails);
128 129 130 131 132 133 134 135 136 137 138 139
      },
        error => {
          console.log("Error");
          this.successSending = 0;
        },
        () => {
          console.log("Success");
          this.successSending = 1;
          //this.validCreator = true;
        }
      )

140 141 142 143
    }


}