ManageCoursePage - handleSave

master
Tudor Stanciu 2020-04-12 00:23:27 +03:00
parent f6970c99ee
commit 233d37d373
4 changed files with 43 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { loadCourses } from "../../redux/actions/courseActions"; import { loadCourses, saveCourse } from "../../redux/actions/courseActions";
import { loadAuthors } from "../../redux/actions/authorActions"; import { loadAuthors } from "../../redux/actions/authorActions";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { bindActionCreators } from "redux"; import { bindActionCreators } from "redux";
@ -33,12 +33,18 @@ function ManageCoursePage({ courses, authors, actions, ...props }) {
})); }));
} }
function handleSave(event) {
event.preventDefault();
actions.saveCourse(course);
}
return ( return (
<CourseForm <CourseForm
course={course} course={course}
errors={errors} errors={errors}
authors={authors} authors={authors}
onChange={handleChange} onChange={handleChange}
onSave={handleSave}
/> />
); );
} }
@ -63,7 +69,8 @@ function mapDispatchToProps(dispatch) {
actions: bindActionCreators( actions: bindActionCreators(
{ {
loadCourses, loadCourses,
loadAuthors loadAuthors,
saveCourse
}, },
dispatch dispatch
) )

View File

@ -1,3 +1,5 @@
export const CREATE_COURSE = "CREATE_COURSE"; export const CREATE_COURSE = "CREATE_COURSE";
export const LOAD_COURSES_SUCCESS = "LOAD_COURSES_SUCCESS"; export const LOAD_COURSES_SUCCESS = "LOAD_COURSES_SUCCESS";
export const LOAD_AUTHORS_SUCCESS = "LOAD_AUTHORS_SUCCESS"; export const LOAD_AUTHORS_SUCCESS = "LOAD_AUTHORS_SUCCESS";
export const CREATE_COURSE_SUCCESS = "CREATE_COURSE_SUCCESS";
export const UPDATE_COURSE_SUCCESS = "UPDATE_COURSE_SUCCESS";

View File

@ -1,14 +1,18 @@
import * as types from "./actionTypes"; import * as types from "./actionTypes";
import * as courseApi from "../../api/courseApi"; import * as courseApi from "../../api/courseApi";
export function createCourse(course) {
return { type: types.CREATE_COURSE, course };
}
function loadCoursesSuccess(courses) { function loadCoursesSuccess(courses) {
return { type: types.LOAD_COURSES_SUCCESS, 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() { export function loadCourses() {
return function (dispatch) { return function (dispatch) {
return courseApi return courseApi
@ -21,3 +25,19 @@ export function loadCourses() {
}); });
}; };
} }
export function saveCourse(course) {
//eslint-disable-next-line no-unused-vars
return function (dispatch, getState) {
return courseApi
.saveCourse(course)
.then((savedCourse) => {
course.id
? dispatch(updateCourseSuccess(savedCourse))
: dispatch(createCourseSuccess(savedCourse));
})
.catch((error) => {
throw error;
});
};
}

View File

@ -3,12 +3,17 @@ import initialState from "./initialState";
export default function courseReducer(state = initialState.courses, action) { export default function courseReducer(state = initialState.courses, action) {
switch (action.type) { switch (action.type) {
case types.CREATE_COURSE:
return [...state, { ...action.course }];
case types.LOAD_COURSES_SUCCESS: case types.LOAD_COURSES_SUCCESS:
return action.courses; return action.courses;
case types.CREATE_COURSE_SUCCESS:
return [...state, { ...action.course }];
case types.UPDATE_COURSE_SUCCESS:
return state.map((course) =>
course.id === action.course.id ? action.course : course
);
default: default:
return state; return state;
} }