redirect between pages

master
Tudor Stanciu 2020-04-12 00:33:16 +03:00
parent 233d37d373
commit 7a147e1dde
2 changed files with 21 additions and 3 deletions

View File

@ -5,8 +5,13 @@ import * as authorActions from "../../redux/actions/authorActions";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { bindActionCreators } from "redux"; import { bindActionCreators } from "redux";
import CourseList from "./CourseList"; import CourseList from "./CourseList";
import { Redirect } from "react-router-dom";
class CoursesPage extends React.Component { class CoursesPage extends React.Component {
state = {
redirectToAddCoursePage: false
};
componentDidMount() { componentDidMount() {
const { courses, authors, actions } = this.props; const { courses, authors, actions } = this.props;
@ -26,7 +31,17 @@ class CoursesPage extends React.Component {
render() { render() {
return ( return (
<> <>
{this.state.redirectToAddCoursePage && <Redirect to="/course" />}
<h2>Courses</h2> <h2>Courses</h2>
<button
style={{ marginBottom: 20 }}
className="btn btn-primary add-course"
onClick={() => this.setState({ redirectToAddCoursePage: true })}
>
Add Course
</button>
<CourseList courses={this.props.courses} /> <CourseList courses={this.props.courses} />
</> </>
); );

View File

@ -7,7 +7,7 @@ import { bindActionCreators } from "redux";
import CourseForm from "./CourseForm"; import CourseForm from "./CourseForm";
import { newCourse } from "../../../tools/mockData"; import { newCourse } from "../../../tools/mockData";
function ManageCoursePage({ courses, authors, actions, ...props }) { function ManageCoursePage({ courses, authors, actions, history, ...props }) {
const [course, setCourse] = useState({ ...props.course }); const [course, setCourse] = useState({ ...props.course });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
@ -35,7 +35,9 @@ function ManageCoursePage({ courses, authors, actions, ...props }) {
function handleSave(event) { function handleSave(event) {
event.preventDefault(); event.preventDefault();
actions.saveCourse(course); actions.saveCourse(course).then(() => {
history.push("/courses");
});
} }
return ( return (
@ -53,7 +55,8 @@ ManageCoursePage.propTypes = {
course: PropTypes.object.isRequired, course: PropTypes.object.isRequired,
courses: PropTypes.array.isRequired, courses: PropTypes.array.isRequired,
authors: PropTypes.array.isRequired, authors: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired actions: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
}; };
function mapStateToProps(state) { function mapStateToProps(state) {