import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions';
import { NgRedux, select } from '@angular-redux/store';
import { ICourseFields } from '../../store/coursefields/coursefields.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: `
`
})
@Injectable() export default class CourseFieldsSelect implements OnInit {
private courseFields$: Observable;
public formGroup: FormGroup;
public cfs = new FormArray([]);
constructor(private fb: FormBuilder,
private _cfa: CourseFieldsActions,
private _ngRedux: NgRedux,
private router: Router) {
this.formGroup = this.fb.group({
formArray: this.cfs
});
};
ngOnInit() {
this._cfa.getCourseFields();
this.courseFields$ = this._ngRedux.select(state => {
state.courseFields.reduce(({}, courseField) =>{
this.cfs.push(new FormControl(courseField.selected, []));
return courseField;
}, {});
return state.courseFields;
});
}
saveSelected() {
this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray);
this.router.navigate(['/region-schools-select']);
}
}