region.schools.select.ts 10.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 === 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;" *ngIf="(regions$ | async).size > 0">
70
        <div class="col-md-6">
71
            <button type="button" class="btn-primary btn-lg pull-left" (click)="navigateBack()" >
72
73
74
75
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
76
            <button type="button" class="btn-primary btn-lg pull-right" (click)="navigateToApplication()" [disabled] = "selectionLimitOptional === false && numSelected < selectionLimit "  >
Open Source Developer's avatar
Open Source Developer committed
77
          <i class="fa fa-forward"></i>
78
79
80
81
82
83
            </button>
        </div>
        </div>
    </form>
  `
})
84
85
86
87
88
89
90
91
92
93
@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;

94
95
    private formGroup: FormGroup;
    private rss = new FormArray([]);
96
    private classActive = "-1";
97
    private regionActive = <number>-1;
98
    private regionActiveId = <number>-1;
99
    private courseActive = <number>-1;
100
    private numSelected = <number>0;
101
102
103
    private selectionLimit = <number>3;
    private regionSizeLimit = <number>3;
    private selectionLimitOptional = <boolean>false;
104
    //private schoolArray: Array<boolean> = new Array();
105

106
107
    constructor(private fb: FormBuilder,
                private _rsa: RegionSchoolsActions,
108
                private _rsb: SectorCoursesActions,
109
                private _ngRedux: NgRedux<IAppState>,
110
                private router: Router
111

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

120
        });
Open Source Developer's avatar
Open Source Developer committed
121

122
123
124
    };

    ngOnInit() {
125

126
127
128
129
130
131
        this.selectEpalClasses();

        this.selectRegionSchools();
    }

    ngOnDestroy() {
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        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();
148
149
150
151
    }

    selectEpalClasses() {
        this.epalclassesSub = this._ngRedux.select(state => {
152
          if (state.epalclasses.size > 0) {
153
154
155
              state.epalclasses.reduce(({}, epalclass, i) => {
                  this.setClassActive(epalclass.name);
                  this.getAppropriateSchools(epalclass.name);
156
157
158
159
                  return epalclass;
              }, {});
          }
          return state.epalclasses;
160
161
162
163
164
165
      }).subscribe(this.epalclasses$);
    }

    selectRegionSchools() {

        this.regionsSub = this._ngRedux.select(state => {
166
            let numsel = 0;
167
168
169
            let numreg = 0;   //count reduced regions in order to set activeRegion when user comes back to his choices
            this.selectionLimitOptional = false;

170
            state.regions.reduce((prevRegion, region) =>{
171
                numreg++;
172
173
                region.epals.reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.selected, []));
174
                    if (epal.selected === true) {
175
                      numsel++;
176
177
178
179
180
181
182
183
184
                      if ( epal.epal_special_case === "1") {
                        this.selectionLimitOptional = true;
                      }
                      this.regionActiveId = Number(region.region_id);
                      this.regionActive = numreg - 1;
                    }
                    if (Number(region.region_id) ===  this.regionActiveId)  {
                      if (region.epals.length < this.regionSizeLimit)
                        this.selectionLimitOptional = true;
185
                    }
186
187
                    return epal;
                }, {});
188

189
190
                return region;
            }, {});
191
            this.numSelected = numsel;
192
            return state.regions;
193
        }).subscribe(this.regions$);
194
195
    }

196
197
198
    setClassActive(className) {
        this.classActive = className;
    }
199

200
    getAppropriateSchools(epalClass) {
201

202
203
204
205
        if (epalClass === "Α' Λυκείου")  {
            this._rsa.getRegionSchools(1,"-1", false);
        }
        else if (epalClass === "Β' Λυκείου") {
206
            this.sectorFieldsSub = this._ngRedux.select(state => {
207
208
209
210
211
212
213
214
                state.sectorFields.reduce(({}, sectorField) =>{
                    if (sectorField.selected === true) {
                        this.courseActive = sectorField.id;
                        this._rsa.getRegionSchools(2,this.courseActive, false);
                    }
                    return sectorField;
                }, {});
                return state.sectorFields;
215
            }).subscribe(this.sectorFields$);
216
217
        }
        else if (epalClass === "Γ' Λυκείου")  {
218
            this.sectorsSub = this._ngRedux.select(state => {
219
220
221
222
223
224
225
226
227
228
229
230
231
                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);
                                  this._rsa.getRegionSchools(3,this.courseActive, false);
                              }
                              return course;
                          }, {});
                      }
                    return sector;
                }, {});
                return state.sectors;
232
            }).subscribe(this.sectors$);
233
        }
234
    }
235

236
237
238
239
240
241
242
243
244
245
246
    navigateBack() {
//        this.router.navigate(['/epal-class-select']);
        if (this.classActive === "Α' Λυκείου")  {
            this.router.navigate(['/epal-class-select']);
        }
        else if (this.classActive === "Β' Λυκείου") {
            this.router.navigate(['/sector-fields-select']);
        }
        else if (this.classActive === "Γ' Λυκείου")  {
            this.router.navigate(['/sectorcourses-fields-select']);
        }
247
248
249
    }

    setActiveRegion(ind) {
250
251
252
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
253
254
    }

255
256
    saveSelected(checked,i,j) {
        this._rsa.saveRegionSchoolsSelected(checked, i, j);
257
258
259
    }

    navigateToApplication() {
260
        this.router.navigate(['/schools-order-select']);
261
262
    }

263
}