minister-informstudents.ts 5.57 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 27 28 29
          <div class="modal-header {{modalHeader}}" >
              <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 57 58
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    //public isModalShown: BehaviorSubject<boolean>;
    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 78 79 80 81

    }

    ngOnInit() {

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

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

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

    }

101 102
    ngOnDestroy() {

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

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


    }

111 112

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

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

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

125 126
    informUnlocatedStudents() {

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

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

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

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

162 163 164 165
    }


}