sector.courses.select.ts 5.67 KB
Newer Older
1
2
3
4
5
6
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
7
8
9
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { IRegions } from '../../store/regionschools/regionschools.types';
import { NgRedux, select } from 'ng2-redux';
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { IAppState } from '../../store/store';

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'sectorcourses-fields-select',
    template: `
    <div class="row equal">
     <div class="col-md-8">
      <form [formGroup]="formGroup">
        <div formArrayName="formArray">
            <ul class="list-group">
            <div *ngFor="let sector$ of sectors$ | async; let i=index">
                <li class="list-group-item" (click)="setActiveSector(i)" [style.background-color]="toggleBackgroundColor(i)">
                    <h5>{{sector$.sector_name}}</h5>
                </li>
                <div *ngFor="let course$ of sector$.courses; let j=index;" [hidden]="i !== sectorActive">
                    <li class="list-group-item" >
                        <div class="row">
                            <div class="col-md-2">
                                <input #cb type="checkbox" formControlName="{{ course$.globalIndex }}"
                                (change)="updateCheckedOptions(course$.globalIndex, cb)"
38
39
                                [checked] = " course$.globalIndex === idx "
                                >
40
41
42
43
44
45
46
47
48
49
50
51
52
                            </div>
                            <div class="col-md-10">
                                {{course$.course_name}}
                            </div>
                        </div>
                    </li>
                </div>
              </div>
            </ul>
        </div>

        <div class="row">
        <div class="col-md-2 col-md-offset-5">
53
            <button type="button" class="btn-primary btn-lg pull-center" (click)="navigateToSchools()" [disabled]="idx === -1"	 >
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
            </button>
        </div>
        </div>
     </form>
   </div>

   <div class="col-md-4">
     <application-preview-select></application-preview-select>
   </div>
   </div>
  `
})
@Injectable() export default class SectorCoursesSelect implements OnInit {
    private sectors$: Observable<ISectors>;
69
    private regions$: Observable<IRegions>;
70
71
72
73
    private formGroup: FormGroup;
    private rss = new FormArray([]);
    private sectorActive = <number>-1;
    private idx = <number>-1;
74
    private sectorsList: Array<boolean> = new Array();
75
76
77

    constructor(private fb: FormBuilder,
                private _rsa: SectorCoursesActions,
78
                private _rsr: RegionSchoolsActions,
79
80
81
82
83
84
85
86
87
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

    ngOnInit() {
88
89
        //re-initialize schools-redux-state
        this.getAllSchools();
90

91
92
        this._rsa.getSectorCourses(true);
        let ids = 0;
93
94
        this.sectors$ = this._ngRedux.select(state => {
            state.sectors.reduce((prevSector, sector) =>{
95
96
97
98
99
                this.sectorsList[ids] = sector.sector_selected;
                ids++;
                //In case we want to preserve last checked option when we revisit the form
                //if (sector.sector_selected === true)
                  //this.sectorActive = ids-1;
100
101
                sector.courses.reduce((prevCourse, course) =>{
                    this.rss.push( new FormControl(course.selected, []));
102
103
104
105
106
                    //this.retrieveCheck();
                    if (course.selected === true) {
                      //In case we want to preserve last checked option when we revisit the form
                      //this.idx = course.globalIndex;
                    }
107
108
109
110
                    return course;
                }, {});
                return sector;
            }, {});
111
            ids = 0;
112
113
114
115
116
117
            return state.sectors;
        });

    }

    setActiveSector(ind) {
118
119
120
      if (ind === this.sectorActive)
        ind = -1;
      this.sectorActive = ind;
121
122
123
124
125
126
127
    }

    toggleBackgroundColor(ind) {
        return ((this.sectorActive === ind) ? "cyan" : "#eeeeee");
    }

    saveSelected() {
128
129
130
131
        this._rsa.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
    }

    navigateToSchools() {
132
133
134
135
136
137
138
139
140
141
        this.router.navigate(['/region-schools-select']);
    }

    updateCheckedOptions(globalIndex, cb){
      this.idx = globalIndex;
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      this.formGroup.value.formArray[globalIndex] = cb.checked;
      if (cb.checked === false)
        this.idx = -1;
142
143
144
145
146
147

      for (let i = 0; i < this.sectorsList.length; i++)
          this.sectorsList[i] = false;
      this.sectorsList[this.sectorActive] = true;

      this.saveSelected();
148
149
}

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
getAllSchools() {
  //store in Redux the whole schools
  this._rsr.getRegionSchools("-1", true);
  this.regions$ = this._ngRedux.select(state => {
      let numsel = 0;
      state.regions.reduce((prevRegion, region) =>{
          region.epals.reduce((prevEpal, epal) =>{
              this.rss.push( new FormControl(epal.selected, []));
              return epal;
          }, {});
          return region;
      }, {});
      return state.regions;
  });

165
}
166

167
168

}