import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { Observable } from 'rxjs/Rx'; import { Injectable } from "@angular/core"; import { RegionSchoolsActions } from '../../actions/regionschools.actions'; import { NgRedux, select } from 'ng2-redux'; import { IRegions } from '../../store/regionschools/regionschools.types'; import { SectorCoursesActions } from '../../actions/sectorcourses.actions'; import { ISectors } from '../../store/sectorcourses/sectorcourses.types'; import { IAppState } from '../../store/store'; import { FormBuilder, FormGroup, FormControl, FormArray } from '@angular/forms'; import {AppSettings} from '../../app.settings'; @Component({ selector: 'course-fields-select', template: `
  • {{region$.region_name}}
  • {{epal$.epal_name}}
` }) @Injectable() export default class RegionSchoolsSelect implements OnInit { private regions$: Observable; private sectors$: Observable; private formGroup: FormGroup; private rss = new FormArray([]); private regionActive = -1; private numSelected = 0; private courseActive = "-1"; constructor(private fb: FormBuilder, private _rsa: RegionSchoolsActions, private _rsb: SectorCoursesActions, private _ngRedux: NgRedux, private router: Router ) { this.formGroup = this.fb.group({ formArray: this.rss }); }; ngOnInit() { this.courseActive = this.getCourseActive(); this._rsa.getRegionSchools(this.courseActive, false); this.regions$ = this._ngRedux.select(state => { let numsel = 0; state.regions.reduce((prevRegion, region) =>{ region.epals.reduce((prevEpal, epal) =>{ this.rss.push( new FormControl(epal.selected, [])); if (epal.selected === true) { numsel++; console.log(epal.epal_name); } return epal; }, {}); return region; }, {}); this.numSelected = numsel; return state.regions; }); } setActiveRegion(ind) { if (ind === this.regionActive) ind = -1; this.regionActive = ind; } toggleBackgroundColor(ind) { return ((this.regionActive === ind) ? "cyan" : "#eeeeee"); } saveSelected() { this._rsa.saveRegionSchoolsSelected(this.formGroup.value.formArray); //this.router.navigate(['/student-application-form-main']); } navigateToApplication() { this.router.navigate(['/student-application-form-main']); } //not used updateCheckedOptions(globalIndex, cb){ /* if (cb.checked) this.numselected--; else this.numselected++; */ this.saveSelected(); } getCourseActive() { const { sectors } = this._ngRedux.getState(); let l,m; for ( l=0; l