diff --git a/src/components/courses/ManageCoursePage.js b/src/components/courses/ManageCoursePage.js index 1b7e042..3141097 100644 --- a/src/components/courses/ManageCoursePage.js +++ b/src/components/courses/ManageCoursePage.js @@ -38,8 +38,22 @@ function ManageCoursePage({ courses, authors, actions, history, ...props }) { })); } + function formIsValid() { + const { title, authorId, category } = course; + const errors = {}; + + if (!title) errors.title = "Title is required."; + if (!authorId) errors.author = "Author is required"; + if (!category) errors.category = "Category is required"; + + setErrors(errors); + // Form is valid if the errors object still has no properties + return Object.keys(errors).length === 0; + } + function handleSave(event) { event.preventDefault(); + if (!formIsValid()) return; setSaving(true); actions .saveCourse(course)