minister-informstudents.ts 5.87 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 45
    <br><br>
    <div class="col-md-12">
      <button type="submit" class="btn btn-lg btn-block"  *ngIf="(loginInfo$ | async).size !== 0"  (click)="informUnlocatedStudents()" >
46 47 48 49
          Μαζική αποστολή e-mail στους μαθητές που δεν τοποθετήθηκαν<span class="glyphicon glyphicon-menu-right"></span>
      </button>
    </div>

50 51 52 53 54 55
   `
})

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

    loginInfo$: BehaviorSubject<ILoginInfo>;
56 57
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
58 59
    private modalHeader: BehaviorSubject<string>;
    //public modalHeader: string;
60
    loginInfoSub: Subscription;
61
    private numSuccessMails:number;
62
    private numFailMails:number;
63
    private successSending:number;
64 65 66 67
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;

68
    constructor(
69 70 71 72 73 74
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {

          this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
75 76 77
          //this.isModalShown = new BehaviorSubject(false);
          this.modalTitle =  new BehaviorSubject("");
          this.modalText =  new BehaviorSubject("");
78
          this.modalHeader =  new BehaviorSubject("");
79 80 81 82 83

    }

    ngOnInit() {

84
      (<any>$('#emaiSentNotice')).appendTo("body");
85

86 87 88 89 90 91 92 93 94 95 96
      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$);

97
      this.numSuccessMails = 0;
98
      this.numFailMails = 0;
99 100 101 102
      this.successSending = -1;

    }

103 104
    ngOnDestroy() {

105 106 107 108
      (<any>$('#emaiSentNotice')).remove();

      if (this.loginInfoSub)
        this.loginInfoSub.unsubscribe();
109 110 111 112


    }

113 114

    public showModal():void {
115
        console.log("about to show modal");
116
        (<any>$('#emaiSentNotice')).modal('show');
117 118
    }

119 120
    public hideModal():void {
        (<any>$('#emaiSentNotice')).modal('hide');
121 122
    }

123 124
    public onHidden():void {
        //this.isModalShown.next(false);
125 126
    }

127 128
    informUnlocatedStudents() {

129 130 131 132
      this.successSending = -2;
      this.numSuccessMails = 0;
      this.numFailMails = 0;

133 134
      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
          this.numSuccessMails = data.num_success_mail;
135 136 137
          this.numFailMails = data.num_fail_mail;
          //console.log("HERE!");
          //console.log(this.numSuccessMails);
138 139 140 141
      },
        error => {
          console.log("Error");
          this.successSending = 0;
142 143 144

          this.modalTitle.next("Κατανομή Μαθητών");
          this.modalText.next("Αποτυχία αποστολής e-mails!");
145 146
          //this.modalHeader = "modal-header-warning";
          this.modalHeader.next("modal-header-warning");
147
          this.showModal();
148 149 150 151
        },
        () => {
          console.log("Success");
          this.successSending = 1;
152

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

167 168 169 170
    }


}