minister-informstudents.ts 6.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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 { API_ENDPOINT } from '../../app.settings';

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

19 20 21
    <div
      class = "loading" *ngIf="successSending == -2" >
    </div>
22

23 24
    <div id="emaiSentNotice" (onHidden)="onHidden()" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-lg">
25
        <div class="modal-content">
26
          <div class="modal-header {{modalHeader | async}}" >
27 28 29
              <h3 class="modal-title pull-left"><i class="fa fa-check-square-o"></i>&nbsp;&nbsp;{{ modalTitle | async }}</h3>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal()">
              <span aria-hidden="true"><i class="fa fa-times"></i></span>
30 31 32
            </button>
          </div>
          <div class="modal-body">
33
              <p>{{ modalText | async }}</p>
34
          </div>
35 36
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Κλείσιμο</button>
37 38 39 40 41
          </div>
        </div>
      </div>
    </div>

42
    <h5> >Αποστολή ειδοποιήσεων <br></h5>
43 44
    <br><br>
    <div class="col-md-12">
45 46 47 48 49 50 51 52
      <button type="submit" class="btn btn-lg btn-block"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="informUnlocatedStudents(true)" >
          Μαζική αποστολή e-mail στους μαθητές που ΔΕΝ τοποθετήθηκαν<span class="glyphicon glyphicon-menu-right"></span>
      </button>
    </div>
    <br>
    <div class="col-md-12">
      <button type="submit" class="btn btn-lg btn-block"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="informUnlocatedStudents(false)" >
          Μαζική αποστολή e-mail στους μαθητές που τοποθετήθηκαν<span class="glyphicon glyphicon-menu-right"></span>
53 54 55
      </button>
    </div>

56 57 58 59 60 61
   `
})

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

    loginInfo$: BehaviorSubject<ILoginInfo>;
62 63
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
64 65
    private modalHeader: BehaviorSubject<string>;
    //public modalHeader: string;
66
    loginInfoSub: Subscription;
67
    private numSuccessMails:number;
68
    private numFailMails:number;
69
    private successSending:number;
70 71 72 73
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;

74
    constructor(
75 76 77 78 79 80
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {

          this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
81 82 83
          //this.isModalShown = new BehaviorSubject(false);
          this.modalTitle =  new BehaviorSubject("");
          this.modalText =  new BehaviorSubject("");
84
          this.modalHeader =  new BehaviorSubject("");
85 86 87 88 89

    }

    ngOnInit() {

90
      (<any>$('#emaiSentNotice')).appendTo("body");
91

92 93 94 95 96 97 98 99 100 101 102
      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$);

103
      this.numSuccessMails = 0;
104
      this.numFailMails = 0;
105 106 107 108
      this.successSending = -1;

    }

109 110
    ngOnDestroy() {

111 112 113 114
      (<any>$('#emaiSentNotice')).remove();

      if (this.loginInfoSub)
        this.loginInfoSub.unsubscribe();
115 116 117 118


    }

119 120

    public showModal():void {
121
        console.log("about to show modal");
122
        (<any>$('#emaiSentNotice')).modal('show');
123 124
    }

125 126
    public hideModal():void {
        (<any>$('#emaiSentNotice')).modal('hide');
127 128
    }

129 130
    public onHidden():void {
        //this.isModalShown.next(false);
131 132
    }

133
    informUnlocatedStudents(unallocated) {
134

135 136 137 138
      this.successSending = -2;
      this.numSuccessMails = 0;
      this.numFailMails = 0;

139
      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword, unallocated).subscribe(data => {
140
          this.numSuccessMails = data.num_success_mail;
141 142 143
          this.numFailMails = data.num_fail_mail;
          //console.log("HERE!");
          //console.log(this.numSuccessMails);
144 145 146 147
      },
        error => {
          console.log("Error");
          this.successSending = 0;
148 149 150

          this.modalTitle.next("Κατανομή Μαθητών");
          this.modalText.next("Αποτυχία αποστολής e-mails!");
151
          this.modalHeader.next("modal-header-warning");
152
          this.showModal();
153 154
        },
        () => {
155
          console.log("Επιτυχής αποστολή e-mails!");
156
          this.successSending = 1;
157

158
          this.modalHeader.next("modal-header-success");
159 160 161
          this.modalTitle.next("Κατανομή Μαθητών");
          let txtModal = "Έγινε αποστολή " + this.numSuccessMails + " e-mails! ";
          if (this.numFailMails != 0) {
162
            this.modalHeader.next("modal-header-warning");
163 164 165 166
            txtModal += "Κάποια e-mail δεν έχουν σταλεί. Δεν ήταν δυνατή η αποστολή " + this.numFailMails + " e-mails!";
          }
          this.modalText.next(txtModal);
          this.showModal();
167 168 169
        }
      )

170 171 172 173
    }


}