import React, { useState, useEffect } from "react"; import { Box, Button, Typography, Modal, FormControlLabel, Switch, TextField, MenuItem, Select, InputLabel, FormControl, } from "@mui/material"; import { useForm } from "react-hook-form"; import { useDispatch } from "react-redux"; import { createSlot } from "../../redux/slices/slotSlice.ts"; // Assuming this is your slice import CloseIcon from "@mui/icons-material/Close"; import { CustomIconButton } from "../AddUserModal/styled.css"; // Assuming this is for custom styled components const AddSlotModal = ({ open, handleClose }: any) => { const dispatch = useDispatch(); // Get dispatch from Redux const { register, handleSubmit, reset, watch, formState: { errors }, } = useForm(); const [isAvailable, setIsAvailable] = useState(true); const [isDateRange, setIsDateRange] = useState(false); const [minEndTime, setMinEndTime] = useState(""); const [durationUnit, setDurationUnit] = useState("minutes"); // New state for duration unit const today = new Date().toISOString().split("T")[0]; const startHour = watch("startHour"); useEffect(() => { if (startHour) { setMinEndTime(startHour); } }, [startHour]); const onSubmit = (data: any) => { const { date, startingDate, endingDate, startHour, endHour, duration } = data; const payload = isDateRange ? { startingDate, endingDate, startHour, endHour, duration: parseInt(duration, 10), durationUnit, // Include the duration unit (minutes or hours) isAvailable, } : { date, startHour, endHour, duration: parseInt(duration, 10), durationUnit, // Include the duration unit (minutes or hours) isAvailable, }; dispatch(createSlot(payload)); reset(); handleClose(); }; return ( { if (reason === "backdropClick") { return; } handleClose(); }} aria-labelledby="add-slot-modal" > {/* Header */} Add Slot {/* Horizontal Line */} {/* Date Range Toggle */} setIsDateRange(!isDateRange)} /> } label="Select Date Range" sx={{ fontWeight: 500, fontSize: "16px", margin: 0, }} /> {/* Date Input Fields */} {isDateRange ? ( <> Start Date value >= today || "Start date cannot be in the past", })} type="date" fullWidth error={!!errors.startingDate} helperText={errors.startingDate?.message} inputProps={{ min: today }} /> End Date value >= today || "End date cannot be in the past", })} type="date" fullWidth error={!!errors.endingDate} helperText={errors.endingDate?.message} inputProps={{ min: today }} /> ) : ( <> Date value >= today || "Date cannot be in the past", })} type="date" fullWidth error={!!errors.date} helperText={errors.date?.message} inputProps={{ min: today }} /> )} {/* Start Hour */} Start Hour {/* End Hour */} End Hour value > startHour || "End hour must be after start hour", })} type="time" fullWidth error={!!errors.endHour} helperText={errors.endHour?.message} inputProps={{ min: minEndTime }} /> {/* Duration and Duration Unit */} Slot Duration {/* Dropdown for selecting Minutes/Hours */} Unit {/* Availability Toggle */} {isAvailable ? "Available" : "Not Available"} {/* Submit Button */} ); }; export default AddSlotModal;