Fixes to not call service if already called. Populate the saved selections into form array

parent eec152a7
......@@ -11,14 +11,17 @@ export class CourseFieldsActions {
private _hds: HelperDataService) {}
getCourseFields = () => {
return this._hds.getCourseFields().then(courseFields => {
return this._ngRedux.dispatch({
type: COURSEFIELDS_RECEIVED,
payload: {
courseFields
}
});
});
const { courseFields } = this._ngRedux.getState();
if (courseFields.size === 0) {
return this._hds.getCourseFields().then(courseFields => {
return this._ngRedux.dispatch({
type: COURSEFIELDS_RECEIVED,
payload: {
courseFields
}
});
});
}
};
saveCourseFieldsSelected = (courseFieldsSelected) => {
......
......@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions';
import { DevToolsExtension, NgRedux, select } from 'ng2-redux';
import { ICourseFields } from '../../store/coursefields/coursefields.types';
import { ICourseFields, ICourseField } from '../../store/coursefields/coursefields.types';
import { IAppState } from '../../store/store';
import {
......@@ -53,12 +53,14 @@ import {AppSettings} from '../../app.settings';
};
ngOnInit() {
this._cfa.getCourseFields();
this.courseFields$ = this._ngRedux.select(state => {
for (let courseField in state.courseFields) {
this.cfs.push(new FormControl('', []));
}
state.courseFields.reduce(({}, courseField) =>{
this.cfs.push(new FormControl(courseField.selected, []));
return courseField;
}, {});
return state.courseFields;
});
......@@ -67,4 +69,6 @@ import {AppSettings} from '../../app.settings';
saveSelected() {
this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment