ManageCoursePage - edit course

master
Tudor Stanciu 2020-04-12 00:49:06 +03:00
parent 7a147e1dde
commit 1e70e1de6e
1 changed files with 14 additions and 3 deletions

View File

@ -16,6 +16,8 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) {
actions.loadCourses().catch((error) => { actions.loadCourses().catch((error) => {
alert("Loading courses failed. " + error); alert("Loading courses failed. " + error);
}); });
} else {
setCourse({ ...props.course });
} }
if (authors.length === 0) { if (authors.length === 0) {
@ -23,7 +25,7 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) {
alert("Loading authors failed. " + error); alert("Loading authors failed. " + error);
}); });
} }
}, []); }, [props.course]);
function handleChange(event) { function handleChange(event) {
const { name, value } = event.target; const { name, value } = event.target;
@ -59,14 +61,23 @@ ManageCoursePage.propTypes = {
history: PropTypes.object.isRequired history: PropTypes.object.isRequired
}; };
function mapStateToProps(state) { function mapStateToProps(state, ownProps) {
const slug = ownProps.match.params.slug;
const course =
slug && state.courses.length > 0
? getCourseBySlug(state.courses, slug)
: newCourse;
return { return {
course: newCourse, course,
courses: state.courses, courses: state.courses,
authors: state.authors authors: state.authors
}; };
} }
function getCourseBySlug(courses, slug) {
return courses.find((course) => course.slug === slug) || null;
}
function mapDispatchToProps(dispatch) { function mapDispatchToProps(dispatch) {
return { return {
actions: bindActionCreators( actions: bindActionCreators(