import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import { useForm } from "react-hook-form"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // hooks import useToast from "hooks/use-toast"; // ui import { Input, PrimaryButton, SecondaryButton } from "components/ui"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // types import type { IEstimate, IEstimatePoint } from "types"; import estimatesService from "services/estimates.service"; 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; value7: string; value8: string; } const defaultValues: FormValues = { value1: "", value2: "", value3: "", value4: "", value5: "", value6: "", value7: "", value8: "", }; export const EstimatePointsModal: React.FC = ({ isOpen, data, estimate, onClose }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { register, formState: { errors, 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, }, { key: 6, value: formData.value7, }, { key: 7, value: formData.value8, }, ], }; 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) 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, }, { key: 6, value: formData.value7, }, { key: 7, value: formData.value8, }, ], }; await estimatesService .updateEstimatesPoints( workspaceSlug as string, projectId as string, estimate?.id as string, data?.[0]?.id as string, payload ) .then(() => { handleClose(); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Estimate points could not be created. Please try again.", }); }); }; useEffect(() => { if(!data) return reset({ ...defaultValues, ...data, }); }, [data, reset]); return ( handleClose()}>

Create Estimate Points

V0
V1
V2
V3
V4
V5
V6
V7
handleClose()}>Cancel {data ? isSubmitting ? "Updating Points..." : "Update Points" : isSubmitting ? "Creating Points..." : "Create Points"}
); };