minister-informstudents.ts 5.81 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 42 43 44
          </div>
        </div>
      </div>
    </div>

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

49 50 51 52 53 54
   `
})

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

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

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

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

    }

    ngOnInit() {

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

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

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

    }

102 103
    ngOnDestroy() {

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

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


    }

112 113

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

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

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

126 127
    informUnlocatedStudents() {

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

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

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

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

166 167 168 169
    }


}