import { FC, Fragment, useEffect, useRef, useState } from "react"; import { observer } from "mobx-react"; import { Check, GripVertical, MoveRight, Pencil, Trash2, X } from "lucide-react"; import { Select } from "@headlessui/react"; import { TEstimatePointsObject } from "@plane/types"; import { Draggable, Spinner } from "@plane/ui"; // constants import { EEstimateUpdateStages } from "@/constants/estimates"; // helpers import { cn } from "@/helpers/common.helper"; import { useEstimate } from "@/hooks/store"; type TEstimatePointItem = { estimateId: string | undefined; mode: EEstimateUpdateStages; item: TEstimatePointsObject; editItem: (value: string) => void; deleteItem: () => void; }; export const EstimatePointItem: FC = observer((props) => { // props const { estimateId, mode, item, editItem, deleteItem } = props; const { id, key, value } = item; // hooks const { asJson: estimate, updateEstimate, deleteEstimate } = useEstimate(estimateId); // ref const inputRef = useRef(null); // states const [inputValue, setInputValue] = useState(undefined); // handling editing states const [estimateEditLoader, setEstimateEditLoader] = useState(false); const [deletedEstimateValue, setDeletedEstimateValue] = useState(undefined); const [isEstimateEditing, setIsEstimateEditing] = useState(false); const [isEstimateDeleting, setIsEstimateDeleting] = useState(false); useEffect(() => { if (inputValue === undefined || inputValue != value) setInputValue(value); }, [value, inputValue]); const handleCreateEdit = (value: string) => { setInputValue(value); editItem(value); }; const handleEdit = async () => { if (id) { try { setEstimateEditLoader(true); await updateEstimate({ estimate_points: [{ id: id, key: key, value: value }] }); setIsEstimateEditing(false); setEstimateEditLoader(false); } catch (error) { setEstimateEditLoader(false); } } else { if (inputValue) editItem(inputValue); } }; const handleDelete = async () => { if (id) { try { setEstimateEditLoader(true); await deleteEstimate(deletedEstimateValue); setIsEstimateDeleting(false); setEstimateEditLoader(false); } catch (error) { setEstimateEditLoader(false); } } else { deleteItem(); } }; const selectDropdownOptions = estimate && estimate?.points ? estimate?.points.filter((point) => point.id !== id) : []; return ( {!id && (
handleCreateEdit(e.target.value)} className="flex-grow border-none bg-transparent focus:ring-0 focus:border-0 focus:outline-none py-2.5 w-full" />
)} {id && ( <> {mode === EEstimateUpdateStages.EDIT && ( <> {!isEstimateEditing && !isEstimateDeleting && (
setIsEstimateEditing(true)}> {value}
setIsEstimateEditing(true)} >
setIsEstimateDeleting(true)} >
)} {(isEstimateEditing || isEstimateDeleting) && (
setInputValue(e.target.value)} className={cn( "border-none focus:ring-0 focus:border-0 focus:outline-none p-2.5 w-full", isEstimateDeleting ? `bg-custom-background-90` : `bg-transparent` )} disabled={isEstimateDeleting} />
{isEstimateDeleting && (
Mark as
)} {isEstimateDeleting && (
)}
{estimateEditLoader ? (
) : (
(isEstimateEditing ? handleEdit() : handleDelete())} > {isEstimateEditing ? : }
)}
(isEstimateEditing ? setIsEstimateEditing(false) : setIsEstimateDeleting(false))} >
)} )} {mode === EEstimateUpdateStages.SWITCH && (
setInputValue(e.target.value)} className="flex-grow border-none focus:ring-0 focus:border-0 focus:outline-none p-2.5 bg-custom-background-90 w-full" disabled />
setInputValue(e.target.value)} className="flex-grow border-none bg-transparent focus:ring-0 focus:border-0 focus:outline-none p-2.5 w-full" />
)} )}
); });