application.submit.ts 14.8 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
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';
14 15 16 17 18 19
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';
20
import { Student, StudentEpalChosen, StudentCourseChosen, StudentSectorChosen, StudentCriteriaChosen } from '../students/student';
21
import {AppSettings} from '../../app.settings';
22 23 24 25 26 27 28 29 30
import { ILoginInfo, ILoginInfoToken } from '../../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
import { EpalClassesActions } from '../../actions/epalclass.actions';
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { CriteriaActions } from '../../actions/criteria.actions';
import { StudentDataFieldsActions } from '../../actions/studentdatafields.actions';
import { HelperDataService } from '../../services/helper-data-service';
31 32 33 34

@Component({
    selector: 'application-submit',
    template: `
35
    <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>
36
    <div id="studentFormSentNotice" (onHidden)="onHidden()" class="modal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" data-keyboard="false">
37 38 39 40 41 42 43 44 45 46 47 48
      <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">
49
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal" (click)="hideModal()">Κλείσιμο</button>
50 51 52 53
          </div>
        </div>
      </div>
    </div>
Open Source Developer's avatar
Open Source Developer committed
54
        <div class="row">
55
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
Open Source Developer committed
56
        </div>
57

58
        <application-preview-select></application-preview-select>
Open Source Developer's avatar
Open Source Developer committed
59
        <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-right" (click)="submitNow()">Υποβολή</button>
60 61 62 63
  `
})

@Injectable() export default class ApplicationSubmit implements OnInit {
64 65 66 67 68 69 70 71 72

    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;
73
    private totalPoints=  <number>0;
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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;
88 89 90 91
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    public isModalShown: BehaviorSubject<boolean>;
    private showLoader: BehaviorSubject<boolean>;
Open Source Developer's avatar
Open Source Developer committed
92
    public currentUrl: string;
93

94 95 96 97 98 99 100 101 102
    constructor(
                private _hds: HelperDataService,
                private _csa: SectorCoursesActions,
                private _sfa: SectorFieldsActions,
                private _rsa: RegionSchoolsActions,
                private _eca: EpalClassesActions,
                private _sdfa: StudentDataFieldsActions,
                private _cria: CriteriaActions,
                private _ngRedux: NgRedux<IAppState>,
103 104 105
                private router: Router,
                private http: Http
            ) {
106 107 108 109 110 111 112 113

                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);
114 115 116 117 118

                this.modalTitle =  new BehaviorSubject("");
                this.modalText =  new BehaviorSubject("");
                this.isModalShown = new BehaviorSubject(false);
                this.showLoader = new BehaviorSubject(false);
Open Source Developer's avatar
Open Source Developer committed
119
            };
120

Open Source Developer's avatar
Open Source Developer committed
121 122 123



124
    ngOnInit() {
Open Source Developer's avatar
Open Source Developer committed
125 126 127 128

     


129
        (<any>$('#studentFormSentNotice')).appendTo("body");
130
      this.loginInfoSub = this._ngRedux.select(state => {
131 132 133 134 135 136 137
          if (state.loginInfo.size > 0) {
              state.loginInfo.reduce(({}, loginInfoToken) => {
                  this.authToken = loginInfoToken.auth_token;
                  return loginInfoToken;
              }, {});
          }
          return state.loginInfo;
138
      }).subscribe(this.loginInfo$);
139

140
      this.epalclassesSub = this._ngRedux.select(state => {
141 142 143 144 145 146 147
        if (state.epalclasses.size > 0) {
            state.epalclasses.reduce(({}, epalclass) => {
                this.classSelected = epalclass.name;
                return epalclass;
            }, {});
        }
        return state.epalclasses;
148
      }).subscribe(this.epalclasses$);
149

150
      this.studentDataFieldsSub = this._ngRedux.select(state => {
151 152 153
          if (state.studentDataFields.size > 0) {
              state.studentDataFields.reduce(({}, studentDataField) => {
                  this.student = studentDataField;
154 155
                  return studentDataField;
              }, {});
156 157
          }
          return state.studentDataFields;
158
      }).subscribe(this.studentDataFields$);
159

160
      this.regionsSub = this._ngRedux.select(state => {
161 162 163 164 165 166 167 168 169 170 171
          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;
172
      }).subscribe(this.regions$);
173

174
      this.criteriaSub = this._ngRedux.select(state => {
175 176 177
          if (state.criter.size > 0) {
              state.criter.reduce(({}, criteria) => {
                //code to be replaced in next version
178 179
                  //if (criteria.selected === true && Number(criteria.id) !== 11)
                  if (criteria.selected === true )  {
180
                      this.studentCriteria.push(Number(criteria.id));
181 182
                      this.totalPoints = this.totalPoints + Number(criteria.points);
                    }
183 184 185 186 187

                  return criteria;
              }, {});
          }
          return state.criter;
188
      }).subscribe(this.criteria$);
189

190
      this.sectorsSub = this._ngRedux.select(state => {
191 192 193 194 195 196 197 198 199 200
          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;
201
      }).subscribe(this.sectors$);
202

203
      this.sectorFieldsSub = this._ngRedux.select(state => {
204 205 206
          state.sectorFields.reduce(({}, sectorField) =>{
            if (sectorField.selected === true) {
              this.sectorSelected = sectorField.id
207
            }
208 209 210
            return sectorField;
          }, {});
          return state.sectorFields;
211
      }).subscribe(this.sectorFields$);
212

213 214
    };

215
    ngOnDestroy() {
216
        (<any>$('#studentFormSentNotice')).remove();
217 218 219 220 221 222 223
        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();
224 225 226 227 228 229 230
        this.regions$.unsubscribe();
        this.epalclasses$.unsubscribe();
        this.sectors$.unsubscribe();
        this.sectorFields$.unsubscribe();
        this.studentDataFields$.unsubscribe();
        this.criteria$.unsubscribe();
        this.loginInfo$.unsubscribe();
231 232
    }

233 234 235 236 237 238 239
    submitNow() {
          //αποστολή στοιχείων μαθητή στο entity: epal_student
          let aitisiObj: Array<Student | StudentEpalChosen[] | StudentCriteriaChosen[] | StudentCourseChosen | StudentSectorChosen > = [];
          let epalObj: Array<StudentEpalChosen> = [];
          let criteriaObj: Array<StudentCriteriaChosen> = [];

          aitisiObj[0] = this.student;
240
          //console.log(aitisiObj[0]['studentbirthdate']);
241 242
          aitisiObj[0]['currentclass'] = this.classSelected;
          //aitisiObj[0]['studentamka'] = ...;
243
          aitisiObj[0]['points'] = this.totalPoints;
244 245 246 247 248 249 250 251 252

          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;

253 254
          //if (aitisiObj[0]['currentclass'] === "Β' Λυκείου" )
          if (aitisiObj[0]['currentclass'] === "2" )
255
            aitisiObj['3'] =  new StudentSectorChosen(null, this.sectorSelected);
256 257
          //else if (aitisiObj[0]['currentclass'] === "Γ' Λυκείου" )
          else if (aitisiObj[0]['currentclass'] === "3" ) {
258
            aitisiObj['3'] =  new StudentCourseChosen(null, this.courseSelected);
259
          }
260 261

          this.submitRecord(aitisiObj);
262 263 264
  }


265 266
  submitRecord(record) {
    let auth_str = this.authToken + ":" + this.authToken;
267
    let authTokenPost = this.authToken + ":" + this.authToken;
268 269

    let headers = new Headers({
270
       "Authorization": "Basic " + btoa(authTokenPost),
271 272 273 274 275 276 277
       "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`;
278
    this.showLoader.next(true);
279 280
    this.http.post(connectionString, record, options)
      .map((res: Response) => res.json())
281
      .subscribe(
282
      success => {
283 284
          (<any>$('.loading')).remove();
          this.showLoader.next(false);
285
          this.modalTitle.next("Υποβολή Αίτησης Εγγραφής");
286 287 288 289 290 291 292 293
          this.modalText.next("Η υποβολή της αίτησής σας πραγματοποιήθηκε. Μπορείτε να την εκτυπώσετε από την επιλογή 'Εμφάνιση - Εκτύπωση Αίτησης'. Θα ειδοποιηθείτε στο e-mail που δηλώσατε για την εξέλιξη της αίτησής σας");
          this._eca.initEpalClasses();
          this._sfa.initSectorFields();
          this._rsa.initRegionSchools();
          this._csa.initSectorCourses();
          this._sdfa.initStudentDataFields();
          this._cria.initCriteria();
          console.log("success post");
294
          this.showModal();
295
           },
296
      error => {
297 298
          (<any>$('.loading')).remove();
          this.showLoader.next(false);
299 300
          this.modalTitle.next("Υποβολή Αίτησης Εγγραφής");
          this.modalText.next("Η υποβολή της αίτησής σας απέτυχε. Παρακαλούμε προσπαθήστε πάλι και αν το πρόβλημα συνεχίσει να υφίσταται, επικοινωνήστε με την ομάδα υποστήριξης");
301
          this.showLoader.next(false);
302 303 304 305
          this.showModal();
          console.log("Error HTTP POST Service")},
      () => {
          console.log("write this message anyway");
306
          (<any>$('.loading')).remove();
307 308 309 310
          this.showLoader.next(false);
      },

      );
311

312
  }
313

314 315
  public showModal():void {
      (<any>$('#studentFormSentNotice')).modal('show');
316
  }
317

318
  public hideModal():void {
319
      console.log("going to post-submit from hide()");
320
      (<any>$('#studentFormSentNotice')).modal('hide');
321 322
      //(<any>$('.modal-backdrop')).remove();
      this.router.navigate(['/post-submit']);
323
  }
324

325 326
  public onHidden():void {
      this.isModalShown.next(false);
327 328
      console.log("going to post-submit");
      this.router.navigate(['/post-submit']);
329
  }
330

331
}