import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { useForm } from "react-hook-form"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import estimatesService from "services/estimates.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; // helpers import { checkDuplicates } from "helpers/array.helper"; // types import { IEstimate, IEstimateFormData } from "types"; // fetch-keys import { ESTIMATES_LIST, ESTIMATE_DETAILS } from "constants/fetch-keys"; type Props = { isOpen: boolean; handleClose: () => void; data?: IEstimate; }; type FormValues = { name: string; description: string; value1: string; value2: string; value3: string; value4: string; value5: string; value6: string; }; const defaultValues: Partial = { name: "", description: "", value1: "", value2: "", value3: "", value4: "", value5: "", value6: "", }; export const CreateUpdateEstimateModal: React.FC = ({ handleClose, data, isOpen }) => { const { register, formState: { isSubmitting }, handleSubmit, reset, } = useForm({ defaultValues, }); const onClose = () => { handleClose(); reset(); }; const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const createEstimate = async (payload: IEstimateFormData) => { if (!workspaceSlug || !projectId) return; await estimatesService .createEstimate(workspaceSlug as string, projectId as string, payload) .then(() => { mutate(ESTIMATES_LIST(projectId as string)); onClose(); }) .catch((err) => { if (err.status === 400) setToastAlert({ type: "error", title: "Error!", message: "Estimate with that name already exists. Please try again with another name.", }); else setToastAlert({ type: "error", title: "Error!", message: "Estimate could not be created. Please try again.", }); }); }; const updateEstimate = async (payload: IEstimateFormData) => { if (!workspaceSlug || !projectId || !data) return; mutate( ESTIMATES_LIST(projectId.toString()), (prevData) => prevData?.map((p) => { if (p.id === data.id) return { ...p, name: payload.estimate.name, description: payload.estimate.description, points: p.points.map((point, index) => ({ ...point, value: payload.estimate_points[index].value, })), }; return p; }), false ); await estimatesService .patchEstimate(workspaceSlug as string, projectId as string, data?.id as string, payload) .then(() => { mutate(ESTIMATES_LIST(projectId.toString())); mutate(ESTIMATE_DETAILS(data.id)); handleClose(); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Estimate could not be updated. Please try again.", }); }); onClose(); }; const onSubmit = async (formData: FormValues) => { if (!formData.name || formData.name === "") { setToastAlert({ type: "error", title: "Error!", message: "Estimate title cannot be empty.", }); return; } if ( formData.value1 === "" || formData.value2 === "" || formData.value3 === "" || formData.value4 === "" || formData.value5 === "" || formData.value6 === "" ) { setToastAlert({ type: "error", title: "Error!", message: "Estimate point cannot be empty.", }); return; } if ( checkDuplicates([ formData.value1, formData.value2, formData.value3, formData.value4, formData.value5, formData.value6, ]) ) { setToastAlert({ type: "error", title: "Error!", message: "Estimate points cannot have duplicate values.", }); return; } const payload: IEstimateFormData = { estimate: { name: formData.name, description: formData.description, }, estimate_points: data ? [ { id: data.points[0].id, key: 0, value: formData.value1, }, { id: data.points[1].id, key: 1, value: formData.value2, }, { id: data.points[2].id, key: 2, value: formData.value3, }, { id: data.points[3].id, key: 3, value: formData.value4, }, { id: data.points[4].id, key: 4, value: formData.value5, }, { id: data.points[5].id, key: 5, value: formData.value6, }, ] : [ { 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, }, ], }; if (data) await updateEstimate(payload); else await createEstimate(payload); }; useEffect(() => { if (data) reset({ ...defaultValues, ...data, value1: data.points[0]?.value, value2: data.points[1]?.value, value3: data.points[2]?.value, value4: data.points[3]?.value, value5: data.points[4]?.value, value6: data.points[5]?.value, }); else reset({ ...defaultValues }); }, [data, reset]); return ( <>
{data ? "Update" : "Create"} Estimate