region.schools.select.ts 14.7 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).size === 0">
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
70
                    <h5>{{region$.region_name}}</h5>
                </li>
71

72
                <div *ngFor="let epal$ of region$.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$.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">
Open Source Developer's avatar
Open Source Developer committed
82
                                {{epal$.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).size > 0">
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
    </div>
113
114
  `
})
115
116
117
118
119
120
121
122
123
124
@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;

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

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

141

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

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

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

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

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

167
168
169
    };

    ngOnInit() {
170

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

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

        this.selectRegionSchools();
    }

    ngOnDestroy() {
179
180
181

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

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        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();
198
199
    }

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

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

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

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

    selectRegionSchools() {

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

236
            state.regions.reduce((prevRegion, region) =>{
237
                numreg++;
238
239
                region.epals.reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.selected, []));
240
                    if (epal.selected === true) {
241
                      numsel++;
242
                      if ( epal.epal_special_case === "1") {
243
                        this.selectionLimitOptional.next(true);
244
245
246
247
248
249
                      }
                      this.regionActiveId = Number(region.region_id);
                      this.regionActive = numreg - 1;
                    }
                    if (Number(region.region_id) ===  this.regionActiveId)  {
                      if (region.epals.length < this.regionSizeLimit)
250
                        this.selectionLimitOptional.next(true);
251
                    }
252
253
                    return epal;
                }, {});
254

255
256
                return region;
            }, {});
257
            this.numSelected.next(numsel);
258
            return state.regions;
259
        }).subscribe(this.regions$);
260
261
    }

262
263
264
    setClassActive(className) {
        this.classActive = className;
    }
265

266
    getAppropriateSchools(epalClass) {
267

Open Source Developer's avatar
trans    
Open Source Developer committed
268
        if (epalClass === "1")  {
269
270
            this._rsa.getRegionSchools(1,"-1", false);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
271
        else if (epalClass === "2") {
272
            this.sectorFieldsSub = this._ngRedux.select(state => {
273
274
275
276
277
278
279
280
                state.sectorFields.reduce(({}, sectorField) =>{
                    if (sectorField.selected === true) {
                        this.courseActive = sectorField.id;
                        this._rsa.getRegionSchools(2,this.courseActive, false);
                    }
                    return sectorField;
                }, {});
                return state.sectorFields;
281
            }).subscribe(this.sectorFields$);
282
        }
283
        else if (epalClass === "3" || epalClass === "4")  {
284
            this.sectorsSub = this._ngRedux.select(state => {
285
286
287
288
289
                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);
290
291
                                  //this._rsa.getRegionSchools(3,this.courseActive, false);
                                  this._rsa.getRegionSchools(Number(epalClass),this.courseActive, false);
292
293
294
295
296
297
298
                              }
                              return course;
                          }, {});
                      }
                    return sector;
                }, {});
                return state.sectors;
299
            }).subscribe(this.sectors$);
300
        }
301
    }
302

303
304
    navigateBack() {
//        this.router.navigate(['/epal-class-select']);
Open Source Developer's avatar
trans    
Open Source Developer committed
305
        if (this.classActive === "1")  {
306
307
            this.router.navigate(['/epal-class-select']);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
308
        else if (this.classActive === "2") {
309
310
            this.router.navigate(['/sector-fields-select']);
        }
311
        else if (this.classActive === "3" || this.classActive === "4")  {
312
313
            this.router.navigate(['/sectorcourses-fields-select']);
        }
314
315
316
    }

    setActiveRegion(ind) {
317
318
319
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
320
321
    }

322
323
    saveSelected(checked,i,j) {
        this._rsa.saveRegionSchoolsSelected(checked, i, j);
324
325
326
    }

    navigateToApplication() {
327
328
329
330
      //  if ( (this.selectionLimitOptional.value === false /*&& this.classNight.value === false */  && this.numSelected.value < this.selectionLimit.value )
      //        || (this.numSelected.value === 0) )    {

      if ( this.numSelected.value === 0)     {
331
332

          //this.modalHeader = "modal-header-success";
333
          this.modalHeader.next("modal-header-danger");
334
335
336
          this.modalTitle.next("Επιλογή αριθμού σχολείων");
          if (this.numSelected.value === 0)
            this.modalText.next("Δεν έχετε επιλέξει κανένα σχολείο!");
337
          /*
338
339
340
341
          else
            this.modalText.next("Παρακαλώ επιλέξτε ΤΡΙΑ σχολεία. "
                              + "Μπορείτε να επιλέξετε ή να αφαιρέσετε μια επιλογή, κάνωντας κλικ στο αντίστοιχο κουτάκι που βρίσκεται μπροστά στο όνομα κάθε σχολείου. "
                              + "Θα έχετε τη δυνατότητα να καθορίσετε την επιθυμητή σειρά προτίμησής των επιλεγμένων σχολείων στην επόμενη οθόνη.");
342
343
            */

344
345
346
347
348
          this.showModal();
        }
        else
          this.router.navigate(['/schools-order-select']);

349
350
    }

351
}