region.schools.select.ts 15.1 KB
Newer Older
1
import { Component, OnInit, OnDestroy} from '@angular/core';
2
import { Router } from '@angular/router';
3
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
4
5
6
7
import { Injectable } from "@angular/core";
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { NgRedux, select } from 'ng2-redux';
import { IRegions } from '../../store/regionschools/regionschools.types';
8
9
10
11
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';
12
13
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
14
import { IAppState } from '../../store/store';
15
16
17
import { RemoveSpaces } from '../../pipes/removespaces';
import { IEpalClasses } from '../../store/epalclasses/epalclasses.types';
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
18

19

20
21
22
23
24
25
26
27
28
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
29
    selector: 'region-schools-select',
30
    template: `
Open Source Developer's avatar
commit    
Open Source Developer committed
31
    <div class="row">
32
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
commit    
Open Source Developer committed
33
34
    </div>

35
    <div class = "loading" *ngIf="!(regions$ | async)">
Open Source Developer's avatar
Open Source Developer committed
36
    </div>
37
    <!-- <div class="row equal">
38
      <div class="col-md-12"> -->
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

      <div id="choiceSentNotice" (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 {{modalHeader | async}}" >
                <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>

59
<div style="min-height: 500px;">
Open Source Developer's avatar
trans    
Open Source Developer committed
60
      <h4> Επιλογή Σχολείου</h4>
61
       <form [formGroup]="formGroup">
62
        <div formArrayName="formArray">
Open Source Developer's avatar
bug    
Open Source Developer committed
63
64
65
        <p style="margin-top: 20px; line-height: 2em;"> Παρακαλώ επιλέξτε εως τρία ΕΠΑΛ στα οποία επιθυμεί να φοιτήσει ο μαθητής.
         Επιλέξτε πρώτα την Περιφερειακή Διεύθυνση στην οποία ανήκει το σχολείο της επιλογής σας,στη συνέχεια τα σχολεία και τέλος πατήστε <i>Συνέχεια</i>.
        Μπορείτε να επιλέξετε απο ένα εως τρία σχολεία που δύναται να ανήκουν σε περισσότερες απο μια Περιφερειακές Διευθύνσεις.</p>
66
67
68
            <ul class="list-group main-view">
            <div *ngFor="let region$ of regions$ | async; let i=index; let isOdd=odd; let isEven=even"  >
                <li class="list-group-item isclickable" (click)="setActiveRegion(i)" [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="regionActive === i">
69
                    <h5>{{region$.get("region_name")}}</h5>
70
                </li>
71

72
                <div *ngFor="let epal$ of region$.get('epals'); let j=index; let isOdd2=odd; let isEven2=even" [class.oddin]="isOdd2" [class.evenin]="isEven2" [hidden]="i !== regionActive">
73

74
                        <div class="row">
75
                            <div class="col-md-2 col-md-offset-1">
76
                                <input #cb type="checkbox" formControlName="{{ epal$.get('globalIndex') }}"
77
                                (change)="saveSelected(cb.checked,i,j)"
78
                                [hidden] = "(numSelected | async) === 3 && cb.checked === false"
79
                                >
80
                             </div>
81
                            <div class="col-md-8  col-md-offset-1 isclickable">
82
                                {{epal$.get("epal_name") | removeSpaces}}
83
84
                            </div>
                        </div>
85

86
                </div>
87

88
89
90
            </div>
            </ul>
        </div>
91
        <div class="row" style="margin-top: 20px; margin-bottom: 20px;" *ngIf="(regions$ | async)">
92
        <div class="col-md-6">
93
            <button type="button" class="btn-primary btn-lg pull-left isclickable" (click)="navigateBack()" >
94
95
96
97
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
98
            <!--
99
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToApplication()"
100

101
102
              [disabled] = " ( (selectionLimitOptional  | async) === false && (classNight  | async) === false  && (numSelected | async) < (selectionLimit | async) )
                                || ( (numSelected | async) === 0)">
103
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
104
            </button>
105
106
107
108
            -->
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToApplication()" >
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
            </button>
109
110
111
        </div>
        </div>
    </form>
112
<!--     <pre>{{formGroup.value | json}}</pre> -->
113
    </div>
114
115
  `
})
116
117
118
119
120
121
122
123
124
125
@Injectable() export default class RegionSchoolsSelect implements OnInit, OnDestroy {
    private epalclasses$: BehaviorSubject<IEpalClasses>;
    private regions$: BehaviorSubject<IRegions>;
    private sectors$: BehaviorSubject<ISectors>;
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private epalclassesSub: Subscription;
    private regionsSub: Subscription;
    private sectorsSub: Subscription;
    private sectorFieldsSub: Subscription;

126
127
    private formGroup: FormGroup;
    private rss = new FormArray([]);
128
    private classActive = "-1";
129
    private regionActive = <number>-1;
130
    private regionActiveId = <number>-1;
131
    private courseActive = <number>-1;
132
133
134
    private numSelected: BehaviorSubject<number>;
    private selectionLimit: BehaviorSubject<number>;
    private selectionLimitOptional: BehaviorSubject<boolean>;
135
    private regionSizeLimit = <number>3;
136
137
    private classNight: BehaviorSubject<boolean>;

138
139
140
141
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;

142

143
144
    constructor(private fb: FormBuilder,
                private _rsa: RegionSchoolsActions,
145
                private _rsb: SectorCoursesActions,
146
                private _ngRedux: NgRedux<IAppState>,
147
                private router: Router
148

149
            ) {
150
151
152
153
        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);
154
155
        this.formGroup = this.fb.group({
            formArray: this.rss
156

157
        });
Open Source Developer's avatar
Open Source Developer committed
158

159
160
161
        this.numSelected = new BehaviorSubject(0);
        this.selectionLimit = new BehaviorSubject(3);
        this.selectionLimitOptional = new BehaviorSubject(false);
162
        this.classNight = new BehaviorSubject(false);
163

164
165
166
167
        this.modalTitle =  new BehaviorSubject("");
        this.modalText =  new BehaviorSubject("");
        this.modalHeader =  new BehaviorSubject("");

168
169
170
    };

    ngOnInit() {
171

172
173
        (<any>$('#choiceSentNotice')).appendTo("body");

174
175
176
177
178
179
        this.selectEpalClasses();

        this.selectRegionSchools();
    }

    ngOnDestroy() {
180
181
182

        (<any>$('#choiceSentNotice')).remove();

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        if (this.epalclassesSub) {
            this.epalclassesSub.unsubscribe();
        }
        if (this.regionsSub) {
            this.regionsSub.unsubscribe();
        }
        if (this.sectorsSub) {
            this.sectorsSub.unsubscribe();
        }
        if (this.sectorFieldsSub) {
            this.sectorFieldsSub.unsubscribe();
        }
        if (this.sectorFields$) this.sectorFields$.unsubscribe();
        if (this.sectors$) this.sectors$.unsubscribe();
        if (this.regions$) this.regions$.unsubscribe();
        if (this.epalclasses$) this.epalclasses$.unsubscribe();
199
200
    }

201
202
203
204
205
206
207
208
209
210
211
212
    public showModal():void {
        (<any>$('#choiceSentNotice')).modal('show');
    }

    public hideModal():void {
        (<any>$('#choiceSentNotice')).modal('hide');
    }

    public onHidden():void {
        //this.isModalShown.next(false);
    }

213
214
    selectEpalClasses() {
        this.epalclassesSub = this._ngRedux.select(state => {
215
          if (state.epalclasses.size > 0) {
216
217
              state.epalclasses.reduce(({}, epalclass, i) => {
                  this.setClassActive(epalclass.name);
218
219
220
221
                  if (epalclass.name === "4") {
                    //this.selectionLimitOptional.next(true);
                    this.classNight.next(true);
                  }
222
                  this.getAppropriateSchools(epalclass.name);
223
224
225
226
                  return epalclass;
              }, {});
          }
          return state.epalclasses;
227
228
229
230
231
232
      }).subscribe(this.epalclasses$);
    }

    selectRegionSchools() {

        this.regionsSub = this._ngRedux.select(state => {
233
            let numsel = 0;
234
            let numreg = 0;   //count reduced regions in order to set activeRegion when user comes back to his choices
235
            this.selectionLimitOptional.next(false);
236

237
238
239
240
241
242
243
            console.log("numsel="+numsel);
            console.log(state.regions);
            console.log(state.regions.size);
            console.log(this.regions$.getValue());
            if (state.regions.size === 0)
                return;

244
            state.regions.reduce((prevRegion, region) =>{
245
                numreg++;
246
247
248
                region.get("epals").reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.get("selected"), []));
                    if (epal.get("selected") === true) {
249
                      numsel++;
250
251
                      console.log("numsel2="+numsel);
                      if ( epal.get("epal_special_case") === "1") {
252
                        this.selectionLimitOptional.next(true);
253
254
255
256
257
                      }
                      this.regionActiveId = Number(region.region_id);
                      this.regionActive = numreg - 1;
                    }
                    if (Number(region.region_id) ===  this.regionActiveId)  {
258
                      if (region.get("epals").length < this.regionSizeLimit)
259
                        this.selectionLimitOptional.next(true);
260
                    }
261
262
                    return epal;
                }, {});
263

264
265
                return region;
            }, {});
266
267

            console.log("numsel3="+numsel);
268
            this.numSelected.next(numsel);
269
            return state.regions;
270
        }).subscribe(this.regions$);
271

272
273
    }

274
275
276
    setClassActive(className) {
        this.classActive = className;
    }
277

278
    getAppropriateSchools(epalClass) {
279

Open Source Developer's avatar
trans    
Open Source Developer committed
280
        if (epalClass === "1")  {
281
282
            this._rsa.getRegionSchools(1,"-1", false);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
283
        else if (epalClass === "2") {
284
            this.sectorFieldsSub = this._ngRedux.select(state => {
285
286
287
288
289
290
291
292
                state.sectorFields.reduce(({}, sectorField) =>{
                    if (sectorField.selected === true) {
                        this.courseActive = sectorField.id;
                        this._rsa.getRegionSchools(2,this.courseActive, false);
                    }
                    return sectorField;
                }, {});
                return state.sectorFields;
293
            }).subscribe(this.sectorFields$);
294
        }
295
        else if (epalClass === "3" || epalClass === "4")  {
296
            this.sectorsSub = this._ngRedux.select(state => {
297
298
299
300
301
                state.sectors.reduce((prevSector, sector) =>{
                      if (sector.sector_selected === true) {
                          sector.courses.reduce((prevCourse, course) =>{
                              if (course.selected === true) {
                                  this.courseActive = parseInt(course.course_id);
302
303
                                  //this._rsa.getRegionSchools(3,this.courseActive, false);
                                  this._rsa.getRegionSchools(Number(epalClass),this.courseActive, false);
304
305
306
307
308
309
310
                              }
                              return course;
                          }, {});
                      }
                    return sector;
                }, {});
                return state.sectors;
311
            }).subscribe(this.sectors$);
312
        }
313
    }
314

315
316
    navigateBack() {
//        this.router.navigate(['/epal-class-select']);
Open Source Developer's avatar
trans    
Open Source Developer committed
317
        if (this.classActive === "1")  {
318
319
            this.router.navigate(['/epal-class-select']);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
320
        else if (this.classActive === "2") {
321
322
            this.router.navigate(['/sector-fields-select']);
        }
323
        else if (this.classActive === "3" || this.classActive === "4")  {
324
325
            this.router.navigate(['/sectorcourses-fields-select']);
        }
326
327
328
    }

    setActiveRegion(ind) {
329
330
331
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
332
333
    }

334
335
    saveSelected(checked,i,j) {
        this._rsa.saveRegionSchoolsSelected(checked, i, j);
336
337
338
    }

    navigateToApplication() {
339
340
341
      //  if ( (this.selectionLimitOptional.value === false /*&& this.classNight.value === false */  && this.numSelected.value < this.selectionLimit.value )
      //        || (this.numSelected.value === 0) )    {

342
      if ( this.numSelected.getValue() === 0)     {
343
344

          //this.modalHeader = "modal-header-success";
345
          this.modalHeader.next("modal-header-danger");
346
          this.modalTitle.next("Επιλογή αριθμού σχολείων");
347
          if (this.numSelected.getValue() === 0)
348
            this.modalText.next("Δεν έχετε επιλέξει κανένα σχολείο!");
349
          /*
350
351
352
353
          else
            this.modalText.next("Παρακαλώ επιλέξτε ΤΡΙΑ σχολεία. "
                              + "Μπορείτε να επιλέξετε ή να αφαιρέσετε μια επιλογή, κάνωντας κλικ στο αντίστοιχο κουτάκι που βρίσκεται μπροστά στο όνομα κάθε σχολείου. "
                              + "Θα έχετε τη δυνατότητα να καθορίσετε την επιθυμητή σειρά προτίμησής των επιλεγμένων σχολείων στην επόμενη οθόνη.");
354
355
            */

356
357
358
359
360
          this.showModal();
        }
        else
          this.router.navigate(['/schools-order-select']);

361
362
    }

363
}