sector.fields.select.ts 4.13 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 8
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
import { SECTOR_FIELDS_INITIAL_STATE } from '../../store/sectorfields/sectorfields.initial-state';
10

11
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
12

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

@Component({
    selector: 'sector-fields-select',
    template: `
Open Source Developer's avatar
trans  
Open Source Developer committed
24 25 26
    <div class="row">
             <breadcrubs></breadcrubs>
    </div>
27
     <div class = "loading" *ngIf="(sectorFields$ | async).size === 0">
28
    </div>
29 30
       <form [formGroup]="formGroup">
        <div formArrayName="formArray">
31 32 33
            <ul class="list-group main-view">
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index; let isOdd=odd; let isEven=even">
                <li class="list-group-item  isclickable" (click)="setActiveSectorAndSave(i)" [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="sectorActive === i">
34 35 36
                    <h5>{{sectorField$.name}}</h5>
                </li>
            </div>
37
            </ul>
38

39
        </div>
40

41
        <div class="row" style="margin-top: 20px;" *ngIf="(sectorFields$ | async).size > 0">
42
        <div class="col-md-6">
43
            <button type="button" class="btn-primary btn-lg pull-left" (click)="router.navigate(['/epal-class-select']);" >
44 45 46 47
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
48
            <button type="button" class="btn-primary btn-lg pull-right" (click)="navigateToSchools()" [disabled]="sectorActive === -1"  >
49
          <i class="fa fa-forward"></i>
50 51 52
            </button>
        </div>
        </div>
53

54 55 56 57
      </form>
  `

})
58
@Injectable() export default class SectorFieldsSelect implements OnInit, OnDestroy {
59 60
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private sectorFieldsSub: Subscription;
61 62 63 64 65 66
    public formGroup: FormGroup;
    public cfs = new FormArray([]);
    private sectorActive = <number>-1;

    constructor(private fb: FormBuilder,
                private _cfa: SectorFieldsActions,
67
                private _rsa: RegionSchoolsActions,
68 69
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
70 71
        this.sectorFields$ = new BehaviorSubject(SECTOR_FIELDS_INITIAL_STATE);

72 73 74 75 76 77
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
78 79
        this._cfa.getSectorFields(false);
        this.sectorFieldsSub = this._ngRedux.select(state => {
80 81
            state.sectorFields.reduce(({}, sectorField) =>{
                this.cfs.push(new FormControl(sectorField.selected, []));
82
                //in case we want to retrieve last check when we return to the form
83

84 85 86
                if (sectorField.selected === true) {
                  this.sectorActive = sectorField.id - 1;
                }
87

88 89 90
                return sectorField;
            }, {});
            return state.sectorFields;
91 92 93
        }).subscribe(this.sectorFields$);

    }
94

95 96
    ngOnDestroy() {
        if (this.sectorFieldsSub) this.sectorFieldsSub.unsubscribe();
97
        this.sectorFields$.unsubscribe();
98 99
    }

100
    navigateToSchools() {
101 102 103
        this.router.navigate(['/region-schools-select']);
    }

104 105 106 107 108 109 110
    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);
111

112
      this._rsa.initRegionSchools();
113 114 115 116 117
    }

    setActiveSectorAndSave(ind) {
        if (ind === this.sectorActive)
          ind = -1;
118
        this.sectorActive = ind;
119
        this.saveSelected();
120 121 122
    }

}