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

parent eec152a7
...@@ -11,14 +11,17 @@ export class CourseFieldsActions { ...@@ -11,14 +11,17 @@ export class CourseFieldsActions {
private _hds: HelperDataService) {} private _hds: HelperDataService) {}
getCourseFields = () => { getCourseFields = () => {
return this._hds.getCourseFields().then(courseFields => { const { courseFields } = this._ngRedux.getState();
return this._ngRedux.dispatch({ if (courseFields.size === 0) {
type: COURSEFIELDS_RECEIVED, return this._hds.getCourseFields().then(courseFields => {
payload: { return this._ngRedux.dispatch({
courseFields type: COURSEFIELDS_RECEIVED,
} payload: {
}); courseFields
}); }
});
});
}
}; };
saveCourseFieldsSelected = (courseFieldsSelected) => { saveCourseFieldsSelected = (courseFieldsSelected) => {
......
...@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Rx'; ...@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions'; import { CourseFieldsActions } from '../../actions/coursefields.actions';
import { DevToolsExtension, NgRedux, select } from 'ng2-redux'; 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 { IAppState } from '../../store/store';
import { import {
...@@ -53,12 +53,14 @@ import {AppSettings} from '../../app.settings'; ...@@ -53,12 +53,14 @@ import {AppSettings} from '../../app.settings';
}; };
ngOnInit() { ngOnInit() {
this._cfa.getCourseFields(); this._cfa.getCourseFields();
this.courseFields$ = this._ngRedux.select(state => { this.courseFields$ = this._ngRedux.select(state => {
for (let courseField in state.courseFields) { state.courseFields.reduce(({}, courseField) =>{
this.cfs.push(new FormControl('', [])); this.cfs.push(new FormControl(courseField.selected, []));
} return courseField;
}, {});
return state.courseFields; return state.courseFields;
}); });
...@@ -67,4 +69,6 @@ import {AppSettings} from '../../app.settings'; ...@@ -67,4 +69,6 @@ import {AppSettings} from '../../app.settings';
saveSelected() { saveSelected() {
this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray); 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