import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { useForm } from "react-hook-form"; import { mutate } from "swr"; // services import estimatesService from "services/estimates.service"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // hooks import useToast from "hooks/use-toast"; // ui import { Input, PrimaryButton, SecondaryButton } from "components/ui"; // types import type { IEstimate, IEstimatePoint } from "types"; // fetch-keys import { ESTIMATE_POINTS_LIST } from "constants/fetch-keys"; type Props = { isOpen: boolean; data?: IEstimatePoint[]; estimate: IEstimate | null; onClose: () => void; }; interface FormValues { value1: string; value2: string; value3: string; value4: string; value5: string; value6: string; } const defaultValues: FormValues = { value1: "", value2: "", value3: "", value4: "", value5: "", value6: "", }; export const EstimatePointsModal: React.FC = ({ isOpen, data, estimate, onClose }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { register, formState: { isSubmitting }, handleSubmit, reset, } = useForm({ defaultValues }); const handleClose = () => { onClose(); reset(); }; const createEstimatePoints = async (formData: FormValues) => { if (!workspaceSlug || !projectId) return; const payload = { estimate_points: [ { key: 0, value: formData.value1, }, { key: 1, value: formData.value2, }, { key: 2, value: formData.value3, }, { key: 3, value: formData.value4, }, { key: 4, value: formData.value5, }, { key: 5, value: formData.value6, }, ], }; await estimatesService .createEstimatePoints( workspaceSlug as string, projectId as string, estimate?.id as string, payload ) .then(() => { handleClose(); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Estimate points could not be created. Please try again.", }); }); }; const updateEstimatePoints = async (formData: FormValues) => { if (!workspaceSlug || !projectId || !data || data.length === 0) return; const payload = { estimate_points: data.map((d, index) => ({ id: d.id, value: (formData as any)[`value${index + 1}`], })), }; await estimatesService .patchEstimatePoints( workspaceSlug as string, projectId as string, estimate?.id as string, payload ) .then(() => { handleClose(); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Estimate points could not be created. Please try again.", }); }); }; const onSubmit = async (formData: FormValues) => { let c = 0; Object.keys(formData).map((key) => { if (formData[key as keyof FormValues] === "") c++; }); if (c !== 0) { setToastAlert({ type: "error", title: "Error!", message: "Please fill all the fields.", }); return; } if (data && data.length !== 0) await updateEstimatePoints(formData); else await createEstimatePoints(formData); if (estimate) mutate(ESTIMATE_POINTS_LIST(estimate.id)); }; useEffect(() => { if (!data || data.length < 6) return; reset({ ...defaultValues, value1: data[0].value, value2: data[1].value, value3: data[2].value, value4: data[3].value, value5: data[4].value, value6: data[5].value, }); }, [data, reset]); return ( handleClose()}>

{data && data.length > 0 ? "Update" : "Create"} Estimate Points

1
2
3
4
5
6
handleClose()}>Cancel {data && data.length > 0 ? isSubmitting ? "Updating Points..." : "Update Points" : isSubmitting ? "Creating Points..." : "Create Points"}
); };