From 1e70e1de6e9e9471e281d47d2403c13480a82d86 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 12 Apr 2020 00:49:06 +0300 Subject: [PATCH] ManageCoursePage - edit course --- src/components/courses/ManageCoursePage.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/courses/ManageCoursePage.js b/src/components/courses/ManageCoursePage.js index 37fb0fb..0dd7235 100644 --- a/src/components/courses/ManageCoursePage.js +++ b/src/components/courses/ManageCoursePage.js @@ -16,6 +16,8 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) { actions.loadCourses().catch((error) => { alert("Loading courses failed. " + error); }); + } else { + setCourse({ ...props.course }); } if (authors.length === 0) { @@ -23,7 +25,7 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) { alert("Loading authors failed. " + error); }); } - }, []); + }, [props.course]); function handleChange(event) { const { name, value } = event.target; @@ -59,14 +61,23 @@ ManageCoursePage.propTypes = { 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 { - course: newCourse, + course, courses: state.courses, authors: state.authors }; } +function getCourseBySlug(courses, slug) { + return courses.find((course) => course.slug === slug) || null; +} + function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(