sector.courses.select.ts 5.71 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
import { Injectable } from "@angular/core";
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
7
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
8
import { NgRedux, select } from 'ng2-redux';
9
import { IAppState } from '../../store/store';
Open Source Developer's avatar
UI  
Open Source Developer committed
10
import {RemoveSpaces} from '../../pipes/removespaces';
11

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

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

@Component({
    selector: 'sectorcourses-fields-select',
    template: `
25
    <div class = "loading" *ngIf="(sectors$ | async).size === 0">
26
   </div>
27 28 29
      <form [formGroup]="formGroup">
        <div formArrayName="formArray">
            <ul class="list-group">
30 31
            <div *ngFor="let sector$ of sectors$ | async; let i=index; let isOdd=odd; let isEven=even">
                <li class="list-group-item isclickable" (click)="setActiveSector(i)"  [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="sectorActive === i">
32 33
                    <h5>{{sector$.sector_name}}</h5>
                </li>
34
                <div *ngFor="let course$ of sector$.courses; let j=index; let isOdd2=odd; let isEven2=even" [class.oddin]="isOdd2" [class.evenin]="isEven2" [hidden]="i !== sectorActive">
Open Source Developer's avatar
Open Source Developer committed
35
                          <div class="row">
36
                           <div class="col-md-2 col-md-offset-1">
37 38
                                <input #cb type="checkbox" formControlName="{{ course$.globalIndex }}"
                                (change)="updateCheckedOptions(course$.globalIndex, cb)"
39 40
                                [checked] = " course$.globalIndex === idx "
                                >
41
                            </div>
42
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
UI  
Open Source Developer committed
43
                                {{course$.course_name | removeSpaces}}
44
                            </div>
Open Source Developer's avatar
Open Source Developer committed
45 46
                            </div>
                    </div>
47 48 49 50
              </div>
            </ul>
        </div>

51
        <div class="row" style="margin-top: 20px;" *ngIf="(sectors$ | async).size > 0">
52
        <div class="col-md-6">
53
            <button type="button" class="btn-primary btn-lg pull-left" (click)="router.navigate(['/epal-class-select']);" >
54 55 56 57 58
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
            <button type="button" class="btn-primary btn-lg pull-right" (click)="navigateToSchools()" [disabled]="idx === -1">
Open Source Developer's avatar
Open Source Developer committed
59
            <i class="fa fa-forward"></i>
60 61 62 63 64 65
            </button>
        </div>
        </div>
     </form>
  `
})
66 67 68
@Injectable() export default class SectorCoursesSelect implements OnInit, OnDestroy {
    private sectors$: BehaviorSubject<ISectors>;
    private sectorsSub: Subscription;
69 70 71 72
    private formGroup: FormGroup;
    private rss = new FormArray([]);
    private sectorActive = <number>-1;
    private idx = <number>-1;
73
    private sectorsList: Array<boolean> = new Array();
74 75

    constructor(private fb: FormBuilder,
76
                private _sca: SectorCoursesActions,
77
                private _rsa: RegionSchoolsActions,
78 79 80
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
81 82

        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
83 84 85 86 87 88
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

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

    }
114

115 116
    ngOnDestroy() {
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
117
        this.sectors$.unsubscribe();
118 119 120
    }

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

    saveSelected() {
127
        this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
128

129
        this._rsa.initRegionSchools();
130 131 132
    }

    navigateToSchools() {
133

134 135 136 137 138 139 140 141 142 143
        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;
144 145 146 147 148 149

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

      this.saveSelected();
150
  }
151 152

}