Skip to content

Commit

Permalink
fix: minDate
Browse files Browse the repository at this point in the history
  • Loading branch information
VadymPopov committed Sep 23, 2024
1 parent 8703536 commit 4b64432
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/components/Schedule/Schedule.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getAvailableSlots } from 'api';
import { isTimeWithinLastHour } from 'utils/timeComparison';
import { validationSchemaTime, FormError } from 'utils/formik';
import { pickDuration } from 'utils/helpers';
import { isNotMonTueWed } from 'utils/isNotMonTueWed';
import { findNextAvailableDate } from 'utils/findNextAvailableDate';
import useGlobalState from 'hooks/useGlobalState';
import Button from 'components/Button';
import {
Expand All @@ -21,7 +23,7 @@ import { Input, Legend } from '../WaiverForm/WaiverForm.styled';
import toast from 'react-hot-toast';

const ScheduleForm = () => {
const minDate = new Date();
const minDate = findNextAvailableDate();
const maxDate = new Date(2024, 9, 31);
const [, setSelectedSlot] = useState(null);
const [activeButtonIndex, setActiveButtonIndex] = useState(null);
Expand Down Expand Up @@ -122,24 +124,6 @@ const ScheduleForm = () => {
navigate('/booking/payment');
};

const isNotMonTueWed = date => {
const day = date.getDay();
const month = new Date(date).getMonth();
const dayNumber = new Date(date).getDate();

if (
(dayNumber === 17 || dayNumber === 18 || dayNumber === 19) &&
month === 9
)
return false;
if (
(dayNumber === 15 || dayNumber === 22 || dayNumber === 29) &&
month === 9
)
return true;
return day !== 1 && day !== 2 && day !== 3;
};

return (
<>
<Formik
Expand Down
15 changes: 15 additions & 0 deletions src/utils/findNextAvailableDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { addDays } from 'date-fns';
import { isNotMonTueWed } from './isNotMonTueWed';

export const findNextAvailableDate = () => {
let dateToCheck = new Date();

for (let i = 0; i < 30; i++) {
if (isNotMonTueWed(dateToCheck)) {
return dateToCheck;
}
dateToCheck = addDays(dateToCheck, 1);
}

return null;
};
11 changes: 11 additions & 0 deletions src/utils/isNotMonTueWed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const isNotMonTueWed = date => {
const day = date.getDay();
const month = new Date(date).getMonth();
const dayNumber = new Date(date).getDate();

if ((dayNumber === 17 || dayNumber === 18 || dayNumber === 19) && month === 9)
return false;
if ((dayNumber === 15 || dayNumber === 22 || dayNumber === 29) && month === 9)
return true;
return day !== 1 && day !== 2 && day !== 3;
};

0 comments on commit 4b64432

Please sign in to comment.