minister-informstudents.ts 3.99 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 31 32 33 34 35 36 37 38 39 40
    <div class="alert alert-success" *ngIf="successSending == 1">
      Έγινε αποστολή {{numSuccessMails}} e-mails!
    </div>
    <div class="alert alert-warning" *ngIf="successSending == 0">
      Κάποια e-mail δεν έχουν σταλεί. Έγινε αποστολή {{numSuccessMails}} e-mails!
    </div>

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

41 42 43 44 45 46 47 48 49 50 51


   `
})

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

    public formGroup: FormGroup;
    //private loginInfo$: Observable<ILoginInfo>;
    loginInfo$: BehaviorSubject<ILoginInfo>;
    loginInfoSub: Subscription;
52 53
    private numSuccessMails:number;
    private successSending:number;
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 82 83 84 85 86 87 88 89 90 91 92
    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);

    }

    ngOnDestroy() {

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

    }

    ngOnInit() {

      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$);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      this.numSuccessMails = 0;
      this.successSending = -1;

    }

    informUnlocatedStudents() {

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

      this._hds.informUnlocatedStudents(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
          //this.data = data;
          //this.successSending = 0;
          this.numSuccessMails = data.num_success_mail;
          console.log("HERE!");
          console.log(this.numSuccessMails);
          //console.log(this.data[0].num_success_mail);
          //console.log(this.data.length);
      },
        error => {
          console.log("Error");
          this.successSending = 0;
        },
        () => {
          console.log("Success");
          this.successSending = 1;
          //this.validCreator = true;
        }
      )

128 129 130 131
    }


}