From 5b845baac9308a7c71a028d8de44a6afdd793c86 Mon Sep 17 00:00:00 2001 From: acheung-94 Date: Wed, 1 May 2024 12:03:12 -0700 Subject: [PATCH 1/5] add past/future date validations to schema --- backend/app/models/Pet.js | 3 ++- backend/app/models/Reminder.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/app/models/Pet.js b/backend/app/models/Pet.js index 4d5f2eb..3c93f41 100644 --- a/backend/app/models/Pet.js +++ b/backend/app/models/Pet.js @@ -7,7 +7,8 @@ const petSchema = Schema({ }, dob: { type: Date, - required: true + required: true, + max: Date.now }, sex: { type: String, diff --git a/backend/app/models/Reminder.js b/backend/app/models/Reminder.js index 7ed4ef9..40d6518 100644 --- a/backend/app/models/Reminder.js +++ b/backend/app/models/Reminder.js @@ -8,11 +8,13 @@ const reminderSchema = Schema({ }, dueDate: { type: Date, - required: true + required: true, + min: Date.now }, performDate: { type: Date, - required: false + required: false, + max: Date.now }, description: { type: String, From 8ae4bdfdd6990a0d9ded79d259bdf65c59217b5f Mon Sep 17 00:00:00 2001 From: acheung-94 Date: Wed, 1 May 2024 13:05:30 -0700 Subject: [PATCH 2/5] add error rendering to due date and perform date on reminders, customize validation messages --- backend/app/models/Pet.js | 2 +- backend/app/models/Reminder.js | 8 +-- .../ReminderFormModal/ReminderFormModal.jsx | 56 ++++++++++++------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/backend/app/models/Pet.js b/backend/app/models/Pet.js index 3c93f41..f01ded5 100644 --- a/backend/app/models/Pet.js +++ b/backend/app/models/Pet.js @@ -8,7 +8,7 @@ const petSchema = Schema({ dob: { type: Date, required: true, - max: Date.now + max: [Date.now, 'Pet birthdate can\'t be in the future.'] }, sex: { type: String, diff --git a/backend/app/models/Reminder.js b/backend/app/models/Reminder.js index 40d6518..51fe9cb 100644 --- a/backend/app/models/Reminder.js +++ b/backend/app/models/Reminder.js @@ -3,18 +3,18 @@ import mongoose, { Schema } from 'mongoose' const reminderSchema = Schema({ title: { type: String, - required: true, + required: [true, 'Must select a title.'], maxLength: 32 }, dueDate: { type: Date, - required: true, - min: Date.now + required: [true, 'Must enter a date.'], + min: [Date.now, 'Due date can\'t be in the past.'] }, performDate: { type: Date, required: false, - max: Date.now + max: [Date.now, 'Perform date can\'t be in the future.'] }, description: { type: String, diff --git a/frontend/src/components/ReminderFormModal/ReminderFormModal.jsx b/frontend/src/components/ReminderFormModal/ReminderFormModal.jsx index 453c740..83231cf 100644 --- a/frontend/src/components/ReminderFormModal/ReminderFormModal.jsx +++ b/frontend/src/components/ReminderFormModal/ReminderFormModal.jsx @@ -19,6 +19,7 @@ const ReminderFormModal = ({modalState, setModalState, pet, reminder={}}) => { modalState === 'edit' ? reminder.description : '') const [location, setLocation] = useState( modalState === 'edit' ? reminder.location : '') + const [reminderErrors, setReminderErrors] = useState({}) const dispatch = useDispatch() const [errors, setErrors] = useState({}) @@ -90,31 +91,43 @@ const ReminderFormModal = ({modalState, setModalState, pet, reminder={}}) => { pet: pet._id } - modalState === 'edit' ? - dispatch(updateReminder(reminderInfo)) : - dispatch(createReminder(reminderInfo)) - .then(() => { - setErrors({}) + if (modalState === 'edit') { + dispatch(updateReminder(reminderInfo)) + .then( () => { + setModalState(null) + setType('') + setTitle('') + setDue('') + setPerformDate('') + setDescription('') + setLocation('') }) - .catch(async res =>{ - let data = await res.json() - setErrors(data.errors) + .catch( async (res) => { + let errors = await res.json() + console.log(errors.errors) + setReminderErrors(errors.errors) }) - if(Object.keys(errors).length !== 0) { - setModalState(null) + }else { + dispatch(createReminder(reminderInfo)) + .then( () => { + setModalState(null) + setType('') + setTitle('') + setDue('') + setPerformDate('') + setDescription('') + setLocation('') + }) + .catch( async (res) => { + let errors = await res.json() + setReminderErrors(errors.errors) + console.log(errors.errors) + }) } - setType('') - setTitle('') - setDue('') - setPerformDate('') - setDescription('') - setLocation('') } - useEffect(() => { - },[errors]) - + const reminderForm = () => ( <> + {reminderErrors.title && {reminderErrors.title.message}} + {reminderErrors.dueDate && {reminderErrors.dueDate.message}}