minister-informstudents.ts 7.38 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
    <div
      class = "loading" *ngIf="successSending == -2" >
    </div>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    <div id="mailSuccessNotice" (onHidden)="onHidden('#mailSuccessNotice')" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title pull-left">Κατανομή μαθητών</h4>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal('#mailSuccessNotice')">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Έγινε αποστολή {{numSuccessMails}} e-mails!</p>
          </div>
        </div>
      </div>
    </div>

    <div id="mailFailureNotice" (onHidden)="onHidden('#mailFailureNotice')" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title pull-left">Κατανομή μαθητών</h4>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal('#mailFailureNotice')">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Αποτυχία αποστολής e-mails!</p>
          </div>
        </div>
      </div>
    </div>

    <div id="mailNonSendingNotice" (onHidden)="onHidden('#mailNonSendingNotice')" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title pull-left">Κατανομή μαθητών</h4>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal('#mailNonSendingNotice')">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Κάποια e-mail δεν έχουν σταλεί.
            Δεν ήταν δυνατή η αποστολή {{numFailMails}} e-mails!</p>
          </div>
        </div>
      </div>
    </div>

    <!--
82
    <div class="alert alert-success" *ngIf="successSending == 1 ">
83 84 85
      Έγινε αποστολή {{numSuccessMails}} e-mails!
    </div>
    <div class="alert alert-warning" *ngIf="successSending == 0">
86 87 88 89 90
      Αποτυχία αποστολής e-mails!
    </div>
    <div class="alert alert-warning" *ngIf="numFailMails != 0">
      Κάποια e-mail δεν έχουν σταλεί.
      Δεν ήταν δυνατή η αποστολή {{numFailMails}} e-mails!
91
    </div>
92
    -->
93

94 95 96
    <br><br>
    <div class="col-md-12">
      <button type="submit" class="btn btn-lg btn-block"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="informUnlocatedStudents()" >
97 98 99 100
          Μαζική αποστολή e-mail στους μαθητές που δεν τοποθετήθηκαν<span class="glyphicon glyphicon-menu-right"></span>
      </button>
    </div>

101 102


103

104 105 106 107 108 109 110 111 112
   `
})

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

    public formGroup: FormGroup;
    //private loginInfo$: Observable<ILoginInfo>;
    loginInfo$: BehaviorSubject<ILoginInfo>;
    loginInfoSub: Subscription;
113
    private numSuccessMails:number;
114
    private numFailMails:number;
115
    private successSending:number;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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);

    }

    ngOnInit() {

137 138 139 140
      (<any>$('#mailSuccessNotice')).appendTo("body");
      (<any>$('#mailFailureNotice')).appendTo("body");
      (<any>$('#mailNonSendingNotice')).appendTo("body");

141 142 143 144 145 146 147 148 149 150 151
      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$);

152
      this.numSuccessMails = 0;
153
      this.numFailMails = 0;
154 155 156 157
      this.successSending = -1;

    }

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    ngOnDestroy() {

      (<any>$('#mailSuccessNotice')).remove();
      (<any>$('#mailFailureNotice')).remove();
      (<any>$('#mailNonSendingNotice')).remove();

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

    }

    public showModal(popupMsgId):void {
        console.log("about to show modal");
        //(<any>$('#distributionWaitingNotice')).modal('show');
        (<any>$(popupMsgId)).modal('show');
    }

    public hideModal(popupMsgId):void {
        //(<any>$('#distributionWaitingNotice')).modal('hide');
        (<any>$(popupMsgId)).modal('hide');
    }

    public onHidden(popupMsgId):void {

    }

184 185 186 187 188 189 190 191 192 193
    informUnlocatedStudents() {

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

194 195 196 197
      this.successSending = -2;
      this.numSuccessMails = 0;
      this.numFailMails = 0;

198 199 200 201
      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
          //this.data = data;
          //this.successSending = 0;
          this.numSuccessMails = data.num_success_mail;
202 203 204
          this.numFailMails = data.num_fail_mail;
          //console.log("HERE!");
          //console.log(this.numSuccessMails);
205 206 207 208
      },
        error => {
          console.log("Error");
          this.successSending = 0;
209
          this.showModal("#mailFailureNotice");
210 211 212 213
        },
        () => {
          console.log("Success");
          this.successSending = 1;
214 215 216
          this.showModal("#mailSuccessNotice");
          if (this.numFailMails != 0)
            this.showModal("#mailNonSendingNotice");
217 218 219
        }
      )

220 221 222 223
    }


}