From 28fdb348852a42af8e493cd571924e48b6d1bd20 Mon Sep 17 00:00:00 2001 From: Evan Yang Date: Mon, 31 Jul 2023 16:13:35 -0700 Subject: [PATCH] feat: Create validation functionality, plus relevant utility functions --- .../manageProjects/createNewEvent.js | 4 ++-- .../manageProjects/editMeetingTimes.js | 3 ++- .../components/manageProjects/editProject.js | 3 ++- .../utilities/validateEventForm.js | 19 +++++++++++++++---- client/src/utils/stringUtils.js | 9 +++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 client/src/utils/stringUtils.js diff --git a/client/src/components/manageProjects/createNewEvent.js b/client/src/components/manageProjects/createNewEvent.js index f29f250fc..04df044b3 100644 --- a/client/src/components/manageProjects/createNewEvent.js +++ b/client/src/components/manageProjects/createNewEvent.js @@ -7,7 +7,7 @@ import validateEventForm from './utilities/validateEventForm'; import EventForm from './eventForm'; const CreateNewEvent = ({ - projectName, + projectToEdit, projectID, createNewRecurringEvent, setEventAlert, @@ -72,7 +72,7 @@ const CreateNewEvent = ({ // Handle submission of new recurring event form const handleFormSubmit = async () => { - const errors = validateEventForm(formValues); + const errors = validateEventForm(formValues, projectToEdit); if (!errors) { handleEventCreate(); setFormValues(initialFormValues); diff --git a/client/src/components/manageProjects/editMeetingTimes.js b/client/src/components/manageProjects/editMeetingTimes.js index 7c27edbd9..67beceab0 100644 --- a/client/src/components/manageProjects/editMeetingTimes.js +++ b/client/src/components/manageProjects/editMeetingTimes.js @@ -8,6 +8,7 @@ import validateEventForm from './utilities/validateEventForm'; // This component displays current meeting times for selected project and offers the option to edit those times. const EditMeetingTimes = ({ + projectToEdit, selectedEvent, setEventAlert, setSelectedEvent, @@ -21,7 +22,7 @@ const EditMeetingTimes = ({ startTimeOriginal, durationOriginal ) => async () => { - const errors = validateEventForm(values); + const errors = validateEventForm(values, projectToEdit); if (!errors) { let theUpdatedEvent = {}; diff --git a/client/src/components/manageProjects/editProject.js b/client/src/components/manageProjects/editProject.js index f64cb3931..cd8197f89 100644 --- a/client/src/components/manageProjects/editProject.js +++ b/client/src/components/manageProjects/editProject.js @@ -61,6 +61,7 @@ const EditProject = ({
{ +const validateEventForm = (vals, projectToEdit) => { let newErrors = {}; Object.keys(vals).forEach((key) => { switch (key) { @@ -8,6 +9,16 @@ const validateEventForm = (vals) => { // Required if (!vals[key]) { newErrors = { ...newErrors, name: 'Event name is required' }; + } else if ( + isWordInArrayInString( + ['meeting', 'mtg', projectToEdit.name.toLowerCase()], + vals[key].toLowerCase() + ) + ) { + newErrors = { + ...newErrors, + name: "Event name cannot contain 'meeting' or 'mtg' or the project name", + }; } break; @@ -22,8 +33,8 @@ const validateEventForm = (vals) => { if (!validateLink(vals[key])) { newErrors = { ...newErrors, - videoConferenceLink: 'Invalid link' - } + videoConferenceLink: 'Invalid link', + }; } break; diff --git a/client/src/utils/stringUtils.js b/client/src/utils/stringUtils.js new file mode 100644 index 000000000..987e632ba --- /dev/null +++ b/client/src/utils/stringUtils.js @@ -0,0 +1,9 @@ +export const isWordInArrayInString = (arr, str) => { + const words = str.split(' '); + for (let word of words) { + if (arr.includes(word)) { + return true; + } + } + return false; +};