Skip to content

Commit

Permalink
added 3 validations for issues Together-100Devs#328 Together-100Devs#329
Browse files Browse the repository at this point in the history
  • Loading branch information
gflacruz committed Feb 25, 2023
1 parent 8af0a7c commit 584b31e
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions client/src/features/form/FormMoverControl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import { useFormContext } from "contexts/FormContext";
import { parseISO, sub } from "date-fns";
import { parseISO, sub, isAfter, isSameDay } from "date-fns";

const FormMoverControl = () => {
const {
Expand Down Expand Up @@ -90,10 +90,32 @@ const FormMoverControl = () => {
}

// initialDate should be the same as or before the finalDate
const initialDate = new Date(formData.initialDate);
const finalDate = new Date(formData.finalDate);
if (finalDate < initialDate) {
errorArray.push("Error: End time is before Start time");
const initialDate = new Date(
`${formData.initialDate}T${formData.startTime}:00`
);
const finalDate = new Date(
`${formData.finalDate}T${formData.endTime}:00`
);
const currentDate = new Date();
if (isAfter(initialDate, finalDate)) {
errorArray.push("Error: End date/time is before Start date/time");
}

// Validate if not reoccuring is checked that user must have same initial and final date
if (
formData["recurring"]["rate"] === "noRecurr" &&
!isSameDay(initialDate, finalDate)
) {
errorArray.push(
"Error: If event is not reoccuring start date and end date must be the same day"
);
}

// Validate that start date and time is after the current time
if (isAfter(currentDate, initialDate)) {
errorArray.push(
"Error: start date/time must be after the current time"
);
}

setFormScheduleEventErrors(errorArray);
Expand Down

0 comments on commit 584b31e

Please sign in to comment.