region.schools.select.ts 11.8 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
38
<!--     <div class="row equal">
      <div class="col-md-12"> -->
Open Source Developer's avatar
trans    
Open Source Developer committed
39
      <h4> Επιλογή Σχολείου</h4>
40
       <form [formGroup]="formGroup">
41
        <div formArrayName="formArray">
42
        <p style="margin-top: 20px; line-height: 2em;"> Παρακαλώ επιλέξτε <strong>τρία </strong> ΕΠΑΛ στα οποία θα ήθελε να φοιτήσει ο μαθητής. Επιλέξτε πρώτα την Περιφερειακή Διεύθυνση που ανήκει το κάθε σχολείο της επιλογής σας, επιλέξτε τα σχολεία και στη συνέχεια επιλέξτε <i>Συνέχεια</i>.
Open Source Developer's avatar
trans    
Open Source Developer committed
43
        Μπορείτε να επιλέξετε σχολεία που ανήκουν σε περισσότερες απο μια Περιφερειακές Διευθύνσεις. <strong> Προσοχή!</strong> Σε ειδικές περιπτώσεις σχολείων μπορείτε να επιλέξετε και λιγότερα απο τρία σχολεία</p>
44
45
46
            <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">
47
48
                    <h5>{{region$.region_name}}</h5>
                </li>
49

50
                <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">
51

52
                        <div class="row">
53
                            <div class="col-md-2 col-md-offset-1">
54
                                <input #cb type="checkbox" formControlName="{{ epal$.globalIndex }}"
55
                                (change)="saveSelected(cb.checked,i,j)"
56
                                [hidden] = "(numSelected | async) === 3 && cb.checked === false"
57
                                >
58
                             </div>
59
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
Open Source Developer committed
60
                                {{epal$.epal_name | removeSpaces}}
61
62
                            </div>
                        </div>
63

64
                </div>
65

66
67
68
            </div>
            </ul>
        </div>
69
        <div class="row" style="margin-top: 20px; margin-bottom: 20px;" *ngIf="(regions$ | async).size > 0">
70
        <div class="col-md-6">
71
            <button type="button" class="btn-primary btn-lg pull-left isclickable" (click)="navigateBack()" >
72
73
74
75
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
76
77
78
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToApplication()"
              [disabled] = " ( (selectionLimitOptional  | async) === false && (classNight  | async) === false  && (numSelected | async) < (selectionLimit | async) )
                                || ( (numSelected | async) === 0)">
79
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
80
81
82
83
84
85
            </button>
        </div>
        </div>
    </form>
  `
})
86
87
88
89
90
91
92
93
94
95
@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;

96
97
    private formGroup: FormGroup;
    private rss = new FormArray([]);
98
    private classActive = "-1";
99
    private regionActive = <number>-1;
100
    private regionActiveId = <number>-1;
101
    private courseActive = <number>-1;
102
103
104
    private numSelected: BehaviorSubject<number>;
    private selectionLimit: BehaviorSubject<number>;
    private selectionLimitOptional: BehaviorSubject<boolean>;
105
    private regionSizeLimit = <number>3;
106
107
    private classNight: BehaviorSubject<boolean>;

108

109
110
    constructor(private fb: FormBuilder,
                private _rsa: RegionSchoolsActions,
111
                private _rsb: SectorCoursesActions,
112
                private _ngRedux: NgRedux<IAppState>,
113
                private router: Router
114

115
            ) {
116
117
118
119
        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);
120
121
        this.formGroup = this.fb.group({
            formArray: this.rss
122

123
        });
Open Source Developer's avatar
Open Source Developer committed
124

125
126
127
        this.numSelected = new BehaviorSubject(0);
        this.selectionLimit = new BehaviorSubject(3);
        this.selectionLimitOptional = new BehaviorSubject(false);
128
        this.classNight = new BehaviorSubject(false);
129

130
131
132
    };

    ngOnInit() {
133

134
135
136
137
138
139
        this.selectEpalClasses();

        this.selectRegionSchools();
    }

    ngOnDestroy() {
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        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();
156
157
158
159
    }

    selectEpalClasses() {
        this.epalclassesSub = this._ngRedux.select(state => {
160
          if (state.epalclasses.size > 0) {
161
162
              state.epalclasses.reduce(({}, epalclass, i) => {
                  this.setClassActive(epalclass.name);
163
164
165
166
167
168
169
                  console.log("My class:");
                  console.log(epalclass.name);
                  if (epalclass.name === "4") {
                    //this.selectionLimitOptional.next(true);
                    this.classNight.next(true);
                    console.log("Mphka!");
                  }
170
                  this.getAppropriateSchools(epalclass.name);
171
172
173
174
                  return epalclass;
              }, {});
          }
          return state.epalclasses;
175
176
177
178
179
180
      }).subscribe(this.epalclasses$);
    }

    selectRegionSchools() {

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

185
            state.regions.reduce((prevRegion, region) =>{
186
                numreg++;
187
188
                region.epals.reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.selected, []));
189
                    if (epal.selected === true) {
190
                      numsel++;
191
                      if ( epal.epal_special_case === "1") {
192
                        this.selectionLimitOptional.next(true);
193
194
195
196
197
198
                      }
                      this.regionActiveId = Number(region.region_id);
                      this.regionActive = numreg - 1;
                    }
                    if (Number(region.region_id) ===  this.regionActiveId)  {
                      if (region.epals.length < this.regionSizeLimit)
199
                        this.selectionLimitOptional.next(true);
200
                    }
201
202
                    return epal;
                }, {});
203

204
205
                return region;
            }, {});
206
207
            this.numSelected.next(numsel);
            console.log("numselected=" + this.numSelected.getValue());
208
            return state.regions;
209
        }).subscribe(this.regions$);
210
211
    }

212
213
214
    setClassActive(className) {
        this.classActive = className;
    }
215

216
    getAppropriateSchools(epalClass) {
217

Open Source Developer's avatar
trans    
Open Source Developer committed
218
        if (epalClass === "1")  {
219
220
            this._rsa.getRegionSchools(1,"-1", false);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
221
        else if (epalClass === "2") {
222
            this.sectorFieldsSub = this._ngRedux.select(state => {
223
224
225
226
227
228
229
230
                state.sectorFields.reduce(({}, sectorField) =>{
                    if (sectorField.selected === true) {
                        this.courseActive = sectorField.id;
                        this._rsa.getRegionSchools(2,this.courseActive, false);
                    }
                    return sectorField;
                }, {});
                return state.sectorFields;
231
            }).subscribe(this.sectorFields$);
232
        }
233
        else if (epalClass === "3" || epalClass === "4")  {
234
            this.sectorsSub = this._ngRedux.select(state => {
235
236
237
238
239
                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);
240
241
                                  //this._rsa.getRegionSchools(3,this.courseActive, false);
                                  this._rsa.getRegionSchools(Number(epalClass),this.courseActive, false);
242
243
244
245
246
247
248
                              }
                              return course;
                          }, {});
                      }
                    return sector;
                }, {});
                return state.sectors;
249
            }).subscribe(this.sectors$);
250
        }
251
    }
252

253
254
    navigateBack() {
//        this.router.navigate(['/epal-class-select']);
Open Source Developer's avatar
trans    
Open Source Developer committed
255
        if (this.classActive === "1")  {
256
257
            this.router.navigate(['/epal-class-select']);
        }
Open Source Developer's avatar
trans    
Open Source Developer committed
258
        else if (this.classActive === "2") {
259
260
            this.router.navigate(['/sector-fields-select']);
        }
261
        else if (this.classActive === "3" || this.classActive === "4")  {
262
263
            this.router.navigate(['/sectorcourses-fields-select']);
        }
264
265
266
    }

    setActiveRegion(ind) {
267
268
269
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
270
271
    }

272
273
    saveSelected(checked,i,j) {
        this._rsa.saveRegionSchoolsSelected(checked, i, j);
274
275
276
    }

    navigateToApplication() {
277
        this.router.navigate(['/schools-order-select']);
278
279
    }

280
}