"use client";
import { DEPARTMENTS } from "@/lib/constants";
import { useShift } from "@/lib/shift-context";
import { useEffect, useState } from "react";
import { formatDateISO } from "./MonthlyCalendar";
import type { ShiftCreateModalProps } from "./ShiftSchedule.types";
function TimeInput({
value,
onChange,
title,
}: {
value: string;
onChange: (v: string) => void;
title: string;
}) {
const [h24, m] = value.split(":").map(Number);
const isPM = h24 >= 12;
const hour12 = h24 === 0 ? 12 : h24 > 12 ? h24 - 12 : h24;
const emit = (newH12: number, newM: number, newIsPM: boolean) => {
let h = newIsPM
? newH12 === 12
? 12
: newH12 + 12
: newH12 === 12
? 0
: newH12;
onChange(
`${h.toString().padStart(2, "0")}:${newM.toString().padStart(2, "0")}`,
);
};
return (
emit(Math.min(12, Math.max(1, Number(e.target.value))), m, isPM)
}
className="text-foreground w-12 border-none bg-transparent py-2.5 pl-3 text-sm outline-none"
/>
:
emit(hour12, Math.min(59, Math.max(0, Number(e.target.value))), isPM)
}
className="text-foreground w-10 border-none bg-transparent py-2.5 text-sm outline-none"
/>
);
}
export default function ShiftCreateModal({
isOpen,
onClose,
defaultDate,
}: ShiftCreateModalProps) {
const { createShift } = useShift();
const [date, setDate] = useState(new Date(defaultDate ?? "2026-04-10"));
const [startTime, setStartTime] = useState("08:00");
const [endTime, setEndTime] = useState("12:00");
const [department, setDepartment] = useState("bar");
const [maxStaff, setMaxStaff] = useState(2);
const [wage, setWage] = useState(120000);
const [error, setError] = useState(null);
useEffect(() => {
if (isOpen) {
setDate(new Date(defaultDate ?? "2026-04-10"));
}
}, [defaultDate, isOpen]);
if (!isOpen) return null;
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
setError(null);
// Validate
if (!date || !startTime || !endTime) {
setError("Please fill in all required fields.");
return;
}
const [sh, sm] = startTime.split(":").map(Number);
const [eh, em] = endTime.split(":").map(Number);
const startMinutes = sh * 60 + sm;
const endMinutes = eh * 60 + em;
if (endMinutes <= startMinutes) {
setError("End time must be after start time.");
return;
}
const deptName =
DEPARTMENTS.find((d) => d.id === department)?.name ?? department;
createShift({
name: `${deptName} - ${formatDateISO(date)}`,
date,
startTime,
endTime,
wage,
maxStaff,
});
onClose();
};
return (
{/* Backdrop */}
{/* Modal */}
{/* Header */}
Create New Shift
Add a shift time slot for staff
{/* Form */}
);
}