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

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

    constructor(private fb: FormBuilder,
74
                private _sca: SectorCoursesActions,
75 76 77
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
78 79

        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
80 81 82 83 84 85
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

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

    }
111

112 113
    ngOnDestroy() {
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
114 115 116
    }

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

    saveSelected() {
123
        this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
124 125 126
    }

    navigateToSchools() {
127 128 129 130 131 132 133 134 135 136
        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;
137 138 139 140 141 142

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

      this.saveSelected();
143
  }
144 145

}