application.submit.ts 13.1 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
2 3
import { Router } from '@angular/router';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
4
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
5 6 7
import { Injectable } from "@angular/core";
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
8 9 10 11 12 13 14
import { IStudentDataFields } from '../../store/studentdatafields/studentdatafields.types';
import { IRegions } from '../../store/regionschools/regionschools.types';
import { ICriter } from '../../store/criteria/criteria.types';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { IEpalClasses } from '../../store/epalclasses/epalclasses.types';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
15 16 17 18 19 20 21
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from '../../store/studentdatafields/studentdatafields.initial-state';
import { CRITERIA_INITIAL_STATE } from '../../store/criteria/criteria.initial-state';
import { REGION_SCHOOLS_INITIAL_STATE } from '../../store/regionschools/regionschools.initial-state';
import { EPALCLASSES_INITIAL_STATE } from '../../store/epalclasses/epalclasses.initial-state';
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
import { SECTOR_FIELDS_INITIAL_STATE } from '../../store/sectorfields/sectorfields.initial-state';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
22
import { Student, StudentEpalChosen, StudentCourseChosen, StudentSectorChosen, StudentCriteriaChosen } from '../students/student';
23 24 25 26 27
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'application-submit',
    template: `
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    <div class = "loading" *ngIf="(studentDataFields$ | async).size === 0 || (criteria$ | async).size === 0 || (regions$ | async).size === 0 || (epalclasses$ | async).size === 0 || (loginInfo$ | async).size === 0 || (showLoader | async) === true"></div>
    <div id="studentFormSentNotice" (onHidden)="onHidden()" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header modal-header-success">
              <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>
            </button>
          </div>
          <div class="modal-body">
              <p>{{ modalText | async }}</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Κλείσιμο</button>
          </div>
        </div>
      </div>
    </div>
Open Source Developer's avatar
commit  
Open Source Developer committed
47
        <div class="row">
48
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
commit  
Open Source Developer committed
49
        </div>
50

51 52
        <application-preview-select></application-preview-select>
        <button type="button button-lg pull-right" *ngIf="(studentDataFields$ | async).size > 0 && (criteria$ | async).size > 0 && (regions$ | async).size > 0 && (epalclasses$ | async).size > 0 && (loginInfo$ | async).size > 0" class="btn-primary btn-lg pull-center" (click)="submitNow()">Υποβολή</button>
53 54 55 56
  `
})

@Injectable() export default class ApplicationSubmit implements OnInit {
57 58 59 60 61 62 63 64 65

    private authToken;
    private student;
    private epalSelected: Array<number> = new Array();
    private epalSelectedOrder: Array<number> = new Array();
    private studentCriteria: Array<number> = new Array();
    private courseSelected;
    private sectorSelected;
    private classSelected;
66
    private totalPoints=  <number>0;
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private regions$: BehaviorSubject<IRegions>;
    private criteria$: BehaviorSubject<ICriter>;
    private sectors$: BehaviorSubject<ISectors>;
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private epalclasses$: BehaviorSubject<IEpalClasses>;
    private loginInfo$: BehaviorSubject<ILoginInfo>;
    private studentDataFieldsSub: Subscription;
    private regionsSub: Subscription;
    private criteriaSub: Subscription;
    private sectorsSub: Subscription;
    private sectorFieldsSub: Subscription;
    private epalclassesSub: Subscription;
    private loginInfoSub: Subscription;
81 82 83 84
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    public isModalShown: BehaviorSubject<boolean>;
    private showLoader: BehaviorSubject<boolean>;
85 86 87 88 89

    constructor(private _ngRedux: NgRedux<IAppState>,
                private router: Router,
                private http: Http
            ) {
90 91 92 93 94 95 96 97

                this.regions$ = new BehaviorSubject(REGION_SCHOOLS_INITIAL_STATE);
                this.epalclasses$ = new BehaviorSubject(EPALCLASSES_INITIAL_STATE);
                this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
                this.sectorFields$ = new BehaviorSubject(SECTOR_FIELDS_INITIAL_STATE);
                this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
                this.criteria$ = new BehaviorSubject(CRITERIA_INITIAL_STATE);
                this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
98 99 100 101 102

                this.modalTitle =  new BehaviorSubject("");
                this.modalText =  new BehaviorSubject("");
                this.isModalShown = new BehaviorSubject(false);
                this.showLoader = new BehaviorSubject(false);
Open Source Developer's avatar
trans  
Open Source Developer committed
103
            };
104 105

    ngOnInit() {
106
        (<any>$('#studentFormSentNotice')).appendTo("body");
107
      this.loginInfoSub = this._ngRedux.select(state => {
108 109 110 111 112 113 114
          if (state.loginInfo.size > 0) {
              state.loginInfo.reduce(({}, loginInfoToken) => {
                  this.authToken = loginInfoToken.auth_token;
                  return loginInfoToken;
              }, {});
          }
          return state.loginInfo;
115
      }).subscribe(this.loginInfo$);
116

117
      this.epalclassesSub = this._ngRedux.select(state => {
118 119 120 121 122 123 124
        if (state.epalclasses.size > 0) {
            state.epalclasses.reduce(({}, epalclass) => {
                this.classSelected = epalclass.name;
                return epalclass;
            }, {});
        }
        return state.epalclasses;
125
      }).subscribe(this.epalclasses$);
126

127
      this.studentDataFieldsSub = this._ngRedux.select(state => {
128 129 130
          if (state.studentDataFields.size > 0) {
              state.studentDataFields.reduce(({}, studentDataField) => {
                  this.student = studentDataField;
131 132
                  return studentDataField;
              }, {});
133 134
          }
          return state.studentDataFields;
135
      }).subscribe(this.studentDataFields$);
136

137
      this.regionsSub = this._ngRedux.select(state => {
138 139 140 141 142 143 144 145 146 147 148
          state.regions.reduce((prevRegion, region) =>{
              region.epals.reduce((prevEpal, epal) =>{
                  if (epal.selected === true) {
                    this.epalSelected.push(Number(epal.epal_id));
                    this.epalSelectedOrder.push(epal.order_id);
                  }
                  return epal;
              }, {});
              return region;
          }, {});
          return state.regions;
149
      }).subscribe(this.regions$);
150

151
      this.criteriaSub = this._ngRedux.select(state => {
152 153 154
          if (state.criter.size > 0) {
              state.criter.reduce(({}, criteria) => {
                //code to be replaced in next version
155 156
                  //if (criteria.selected === true && Number(criteria.id) !== 11)
                  if (criteria.selected === true )  {
157
                      this.studentCriteria.push(Number(criteria.id));
158 159
                      this.totalPoints = this.totalPoints + Number(criteria.points);
                    }
160 161 162 163 164

                  return criteria;
              }, {});
          }
          return state.criter;
165
      }).subscribe(this.criteria$);
166

167
      this.sectorsSub = this._ngRedux.select(state => {
168 169 170 171 172 173 174 175 176 177
          state.sectors.reduce((prevSector, sector) =>{
              sector.courses.reduce((prevCourse, course) =>{
                  if (course.selected === true) {
                    this.courseSelected = course.course_id
                  }
                  return course;
              }, {});
              return sector;
          }, {});
          return state.sectors;
178
      }).subscribe(this.sectors$);
179

180
      this.sectorFieldsSub = this._ngRedux.select(state => {
181 182 183
          state.sectorFields.reduce(({}, sectorField) =>{
            if (sectorField.selected === true) {
              this.sectorSelected = sectorField.id
184
            }
185 186 187
            return sectorField;
          }, {});
          return state.sectorFields;
188
      }).subscribe(this.sectorFields$);
189

190 191
    };

192
    ngOnDestroy() {
193
        (<any>$('#studentFormSentNotice')).remove();
194 195 196 197 198 199 200
        if (this.studentDataFieldsSub) this.studentDataFieldsSub.unsubscribe();
        if (this.criteriaSub) this.criteriaSub.unsubscribe();
        if (this.regionsSub) this.regionsSub.unsubscribe();
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
        if (this.sectorFieldsSub) this.sectorFieldsSub.unsubscribe();
        if (this.epalclassesSub) this.epalclassesSub.unsubscribe();
        if (this.loginInfoSub) this.loginInfoSub.unsubscribe();
201 202 203 204 205 206 207
        this.regions$.unsubscribe();
        this.epalclasses$.unsubscribe();
        this.sectors$.unsubscribe();
        this.sectorFields$.unsubscribe();
        this.studentDataFields$.unsubscribe();
        this.criteria$.unsubscribe();
        this.loginInfo$.unsubscribe();
208 209
    }

210 211 212 213 214 215 216
    submitNow() {
          //αποστολή στοιχείων μαθητή στο entity: epal_student
          let aitisiObj: Array<Student | StudentEpalChosen[] | StudentCriteriaChosen[] | StudentCourseChosen | StudentSectorChosen > = [];
          let epalObj: Array<StudentEpalChosen> = [];
          let criteriaObj: Array<StudentCriteriaChosen> = [];

          aitisiObj[0] = this.student;
217
          //console.log(aitisiObj[0]['studentbirthdate']);
218 219
          aitisiObj[0]['currentclass'] = this.classSelected;
          //aitisiObj[0]['studentamka'] = ...;
220
          aitisiObj[0]['points'] = this.totalPoints;
221 222 223 224 225 226 227 228 229

          for (let i=0; i < this.epalSelected.length; i++)
            epalObj[i] =new StudentEpalChosen(null, this.epalSelected[i] , this.epalSelectedOrder[i]);
          aitisiObj['1'] =   epalObj;

          for (let i=0; i < this.studentCriteria.length; i++)
            criteriaObj[i] =new StudentCriteriaChosen(null, null, this.studentCriteria[i]);
          aitisiObj['2'] = criteriaObj;

230 231
          //if (aitisiObj[0]['currentclass'] === "Β' Λυκείου" )
          if (aitisiObj[0]['currentclass'] === "2" )
232
            aitisiObj['3'] =  new StudentSectorChosen(null, this.sectorSelected);
233 234
          //else if (aitisiObj[0]['currentclass'] === "Γ' Λυκείου" )
          else if (aitisiObj[0]['currentclass'] === "3" ) {
235
            aitisiObj['3'] =  new StudentCourseChosen(null, this.courseSelected);
236
          }
237 238

          this.submitRecord(aitisiObj);
239 240 241
  }


242 243
  submitRecord(record) {
    let auth_str = this.authToken + ":" + this.authToken;
244
    let authTokenPost = this.authToken + ":" + this.authToken;
245 246

    let headers = new Headers({
247
       "Authorization": "Basic " + btoa(authTokenPost),
248 249 250 251 252 253 254
       "Accept": "*/*",
       "Access-Control-Allow-Credentials": "true",
       "Content-Type": "application/json",
    });

    let options = new RequestOptions({ headers: headers,  method: "post", withCredentials: true });
    let connectionString = `${AppSettings.API_ENDPOINT}/epal/appsubmit`;
255
    this.showLoader.next(true);
256 257
    this.http.post(connectionString, record, options)
      .map((res: Response) => res.json())
258
      .subscribe(
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      success => {
          this.modalTitle.next("Υποβολή Αίτησης Εγγραφής");
          this.modalText.next("Η υποβολή της αίτησής σας πραγματοποιήθηκε. Μπορείτε να την εκτυπώσετε από την επιλογή 'Εξαγωγή σε PDF'. Θα ειδοποιηθείτε στο e-mail που δηλώσατε για την εξέλιξη της αίτησής σας");
          this.showModal();
          console.log("success post"); },
      error => {
          this.modalTitle.next("Υποβολή Αίτησης Εγγραφής");
          this.modalText.next("Η υποβολή της αίτησής σας απέτυχε. Παρακαλούμε προσπαθήστε πάλι και αν το πρόβλημα συνεχίσει να υφίσταται, επικοινωνήστε με την ομάδα υποστήριξης");
          this.showModal();
          console.log("Error HTTP POST Service")},
      () => {
          console.log("write this message anyway");
          this.showLoader.next(false);
      },

      );
275

276
  }
277

278 279
  public showModal():void {
      (<any>$('#studentFormSentNotice')).modal('show');
280
  }
281

282 283 284
  public hideModal():void {
      (<any>$('#studentFormSentNotice')).modal('hide');
  }
285

286 287 288
  public onHidden():void {
      this.isModalShown.next(false);
  }
289

290
}