Created
December 29, 2016 15:57
-
-
Save paulohp/69503e0d6641d2d39bdc736e02f27e2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as types from './actionsTypes'; | |
import courseApi from '../api/mockCourseApi'; | |
export function loadCoursesSucess(courses) { | |
return { | |
type: types.LOAD_COURSES_SUCCESS, | |
courses | |
}; | |
} | |
export function createCourseSuccess(course) { | |
return { | |
type: types.CREATE_COURSE_SUCCESS, | |
course | |
}; | |
} | |
export function updateCourseSuccess(course) { | |
return { | |
type: types.UPDATE_COURSE_SUCCESS, | |
course | |
}; | |
} | |
export function loadCourses() { | |
return function(dispatch) { | |
return courseApi.getAllCourses() | |
.then(courses => { | |
dispatch(loadCoursesSucess(courses)); | |
}) | |
.catch(err => { | |
throw err; | |
}); | |
}; | |
} | |
export function saveCourse(course) { | |
return function (dispatch, getState) { | |
return courseApi.saveCourse(course) | |
.then(savedCourse =>{ | |
course.id | |
? dispatch(updateCourseSuccess(savedCourse)) | |
: dispatch(createCourseSuccess(savedCourse)) | |
}) | |
.catch(err => { | |
throw err; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment