import { type FC, useEffect, useState } from "react"; import DefaulModal from "@/components/DefaulModal"; import Input from "@/components/Input"; import Textarea from "@/components/Textarea"; import Button from "@/components/Button"; import { useTranslation } from "react-i18next"; import { toast } from "react-toastify"; import { extractErrorMessage } from "@/config/func"; import { useUpdateCashShift } from "../hooks/useCashShiftData"; import type { CashShift } from "../types/Types"; type Props = { open: boolean; onClose: () => void; shift: CashShift | null; }; const EditCashShiftModal: FC = ({ open, onClose, shift }) => { const { t } = useTranslation("global"); const [openingAmount, setOpeningAmount] = useState(""); const [notes, setNotes] = useState(""); const [error, setError] = useState(""); const updateShift = useUpdateCashShift(); useEffect(() => { if (open && shift) { setOpeningAmount(String(shift.openingAmount)); setNotes(shift.notes ?? ""); setError(""); } }, [open, shift]); const handleClose = () => { setOpeningAmount(""); setNotes(""); setError(""); onClose(); }; const handleSubmit = () => { if (!shift) return; const numericAmount = Number(openingAmount); if (Number.isNaN(numericAmount) || numericAmount < 0) { setError(t("cash_shift.opening_amount_required")); return; } updateShift.mutate( { shiftId: shift.id, payload: { openingAmount: numericAmount, notes: notes.trim() || undefined, }, }, { onSuccess: () => { toast.success(t("cash_shift.updated_success")); handleClose(); }, onError: (err) => { toast.error(extractErrorMessage(err)); }, } ); }; if (!shift) return null; return (
{ setOpeningAmount(e.target.value); setError(""); }} error_text={error} />