sector.courses.select.ts 5.78 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: `
Open Source Developer's avatar
commit  
Open Source Developer committed
25 26 27
    <div class="row">
             <breadcrubs></breadcrubs>
    </div>
28
    <div class = "loading" *ngIf="(sectors$ | async).size === 0">
29
   </div>
30 31 32
      <form [formGroup]="formGroup">
        <div formArrayName="formArray">
            <ul class="list-group">
33 34
            <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">
35 36
                    <h5>{{sector$.sector_name}}</h5>
                </li>
37
                <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
38
                          <div class="row">
39
                           <div class="col-md-2 col-md-offset-1">
40 41
                                <input #cb type="checkbox" formControlName="{{ course$.globalIndex }}"
                                (change)="updateCheckedOptions(course$.globalIndex, cb)"
42 43
                                [checked] = " course$.globalIndex === idx "
                                >
44
                            </div>
45
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
UI  
Open Source Developer committed
46
                                {{course$.course_name | removeSpaces}}
47
                            </div>
Open Source Developer's avatar
Open Source Developer committed
48 49
                            </div>
                    </div>
50 51 52 53
              </div>
            </ul>
        </div>

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

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

        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
86 87 88 89 90 91
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

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

    }
117

118 119
    ngOnDestroy() {
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
120
        this.sectors$.unsubscribe();
121 122 123
    }

    setActiveSector(ind) {
124 125 126
      if (ind === this.sectorActive)
        ind = -1;
      this.sectorActive = ind;
127 128 129
    }

    saveSelected() {
130
        this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
131

132
        this._rsa.initRegionSchools();
133 134 135
    }

    navigateToSchools() {
136

137 138 139 140 141 142 143 144 145 146
        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;
147 148 149 150 151 152

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

      this.saveSelected();
153
  }
154 155

}