sector.fields.select.ts 4.83 KB
Newer Older
1
2
3
4
5
6
7
8
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { NgRedux, select } from 'ng2-redux';
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { IAppState } from '../../store/store';
9
10
11
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { IRegions } from '../../store/regionschools/regionschools.types';

12
13
14
15
16
17
18
19
20
21
22
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'sector-fields-select',
    template: `
23

24
    <div class="row equal">
25

Open Source Developer's avatar
UI    
Open Source Developer committed
26
     <div class="col-md-12">
27
28
       <form [formGroup]="formGroup">
        <div formArrayName="formArray">
29

30
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index">
31
                <li class="list-group-item" (click)="setActiveSectorAndSave(i)" [style.background-color]="toggleBackgroundColor(i)">
32
33
34
                    <h5>{{sectorField$.name}}</h5>
                </li>
            </div>
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

            <!--CHECK BOXES USING BOTSTRAP
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index">
                <div class="[ form-group ]">
                  <input type="checkbox" name="{{i}}" id="{{i}}" autocomplete="off" />
                  <div class="[ btn-group ]">
                      <label for="{{i}}" class="[ btn btn-primary ]">
                          <span class="[ glyphicon glyphicon-ok ]"></span>
                          <span> </span>
                      </label>
                      <label for="{{i}}" class="[ btn btn-default active ]">
                          {{sectorField$.name}}
                      </label>
                  </div>
                </div>
            </div>
            -->
52
        </div>
53

54
        <div class="row">
Open Source Developer's avatar
UI    
Open Source Developer committed
55
56
        <div class="col-md-12 col-md-offset-5">
            <button type="button" class="btn-primary btn-lg pull-right" (click)="navigateToSchools()" [disabled]="sectorActive === -1"	>
57
58
59
60
61
62
            Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
            </button>
        </div>
        </div>
      </form>
    </div>
Open Source Developer's avatar
UI    
Open Source Developer committed
63
  
64
65
66
67
68
69
  </div>
  `

})
@Injectable() export default class SectorFieldsSelect implements OnInit {
    private sectorFields$: Observable<ISectorFields>;
70
    private regions$: Observable<IRegions>;
71
72
73
74
75
76
    public formGroup: FormGroup;
    public cfs = new FormArray([]);
    private sectorActive = <number>-1;

    constructor(private fb: FormBuilder,
                private _cfa: SectorFieldsActions,
77
                private _rsr: RegionSchoolsActions,
78
79
80
81
82
83
84
85
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

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

89
        this._cfa.getSectorFields(true);
90
91
92
        this.sectorFields$ = this._ngRedux.select(state => {
            state.sectorFields.reduce(({}, sectorField) =>{
                this.cfs.push(new FormControl(sectorField.selected, []));
93
94
                //in case we want to retrieve last check when we return to the form
                /*
95
96
97
                if (sectorField.selected === true) {
                  this.sectorActive = sectorField.id - 1;
                }
98
                */
99
100
101
102
103
104
105
                return sectorField;
            }, {});
            return state.sectorFields;
        });

    }

106
107
    navigateToSchools() {
        //this.saveSelected();
108
109
110
        this.router.navigate(['/region-schools-select']);
    }

111
112
113
114
115
116
117
118
119
120
121
122
    saveSelected() {
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      if (this.sectorActive != -1)
        this.formGroup.value.formArray[this.sectorActive] = true;

      this._cfa.saveSectorFieldsSelected(this.formGroup.value.formArray);
    }

    setActiveSectorAndSave(ind) {
        if (ind === this.sectorActive)
          ind = -1;
123
        this.sectorActive = ind;
124
        this.saveSelected();
125
126
127
    }

    toggleBackgroundColor(ind) {
Open Source Developer's avatar
UI    
Open Source Developer committed
128
        return ((this.sectorActive === ind) ? "#fd9665" : "white");
129
130
    }

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    getAllSchools() {
      //store in Redux the whole schools
      this._rsr.getRegionSchools(3,"-1", true);
      this.regions$ = this._ngRedux.select(state => {
          let numsel = 0;
          state.regions.reduce((prevRegion, region) =>{
              region.epals.reduce((prevEpal, epal) =>{
                  this.cfs.push( new FormControl(epal.selected, []));
                  return epal;
              }, {});
              return region;
          }, {});
          return state.regions;
      });

    }
147
148

}