2023-04-06 09:39:24 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
2023-04-08 12:35:54 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-04-06 09:39:24 +00:00
|
|
|
|
2023-04-08 12:35:54 +00:00
|
|
|
// services
|
2023-04-06 09:39:24 +00:00
|
|
|
import projectService from "services/project.service";
|
2023-04-08 12:35:54 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
import useProjectDetails from "hooks/use-project-details";
|
|
|
|
// components
|
2023-04-21 19:29:57 +00:00
|
|
|
import { DeleteEstimateModal } from "components/estimates";
|
2023-04-08 12:35:54 +00:00
|
|
|
// ui
|
2023-04-21 19:29:57 +00:00
|
|
|
import { CustomMenu, SecondaryButton } from "components/ui";
|
2023-04-08 12:35:54 +00:00
|
|
|
//icons
|
2023-04-21 19:29:57 +00:00
|
|
|
import { PencilIcon, TrashIcon } from "@heroicons/react/24/outline";
|
2023-04-18 05:24:19 +00:00
|
|
|
// helpers
|
|
|
|
import { orderArrayBy } from "helpers/array.helper";
|
2023-04-08 12:35:54 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICurrentUserResponse, IEstimate } from "types";
|
2023-04-06 09:39:24 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-04-06 09:39:24 +00:00
|
|
|
estimate: IEstimate;
|
|
|
|
editEstimate: (estimate: IEstimate) => void;
|
|
|
|
handleEstimateDelete: (estimateId: string) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SingleEstimate: React.FC<Props> = ({
|
2023-06-06 16:06:00 +00:00
|
|
|
user,
|
2023-04-06 09:39:24 +00:00
|
|
|
estimate,
|
|
|
|
editEstimate,
|
|
|
|
handleEstimateDelete,
|
|
|
|
}) => {
|
2023-04-12 09:33:04 +00:00
|
|
|
const [isDeleteEstimateModalOpen, setIsDeleteEstimateModalOpen] = useState(false);
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-04-08 12:35:54 +00:00
|
|
|
const { projectDetails, mutateProjectDetails } = useProjectDetails();
|
|
|
|
|
|
|
|
const handleUseEstimate = async () => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
const payload = {
|
2023-04-06 09:39:24 +00:00
|
|
|
estimate: estimate.id,
|
|
|
|
};
|
2023-04-08 12:35:54 +00:00
|
|
|
|
2023-08-15 09:34:46 +00:00
|
|
|
mutateProjectDetails((prevData: any) => {
|
2023-04-08 12:35:54 +00:00
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return { ...prevData, estimate: estimate.id };
|
|
|
|
}, false);
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
await projectService
|
2023-06-06 16:06:00 +00:00
|
|
|
.updateProject(workspaceSlug as string, projectId as string, payload, user)
|
2023-04-06 09:39:24 +00:00
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate points could not be used. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-04-08 12:35:54 +00:00
|
|
|
<>
|
|
|
|
<div className="gap-2 py-3">
|
2023-04-21 19:29:57 +00:00
|
|
|
<div className="flex items-center justify-between">
|
2023-04-08 12:35:54 +00:00
|
|
|
<div>
|
2023-04-21 19:29:57 +00:00
|
|
|
<h6 className="flex w-[40vw] items-center gap-2 truncate text-sm font-medium">
|
2023-04-08 12:35:54 +00:00
|
|
|
{estimate.name}
|
|
|
|
{projectDetails?.estimate && projectDetails?.estimate === estimate.id && (
|
2023-07-11 09:48:47 +00:00
|
|
|
<span className="rounded bg-green-500/20 px-2 py-0.5 text-xs text-green-500">
|
2023-04-08 12:35:54 +00:00
|
|
|
In use
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</h6>
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="font-sm w-[40vw] truncate text-[14px] font-normal text-custom-text-200">
|
2023-04-06 09:39:24 +00:00
|
|
|
{estimate.description}
|
|
|
|
</p>
|
|
|
|
</div>
|
2023-04-21 19:29:57 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
{projectDetails?.estimate !== estimate.id && estimate.points.length > 0 && (
|
2023-07-11 09:48:47 +00:00
|
|
|
<SecondaryButton
|
|
|
|
onClick={handleUseEstimate}
|
|
|
|
className="!py-1 text-custom-text-200 hover:text-custom-text-100"
|
|
|
|
>
|
2023-04-21 19:29:57 +00:00
|
|
|
Use
|
|
|
|
</SecondaryButton>
|
|
|
|
)}
|
|
|
|
<CustomMenu ellipsis>
|
2023-04-11 12:24:01 +00:00
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={() => {
|
2023-04-21 19:29:57 +00:00
|
|
|
editEstimate(estimate);
|
2023-04-11 12:24:01 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="flex items-center justify-start gap-2">
|
2023-04-21 19:29:57 +00:00
|
|
|
<PencilIcon className="h-3.5 w-3.5" />
|
|
|
|
<span>Edit estimate</span>
|
2023-04-11 12:24:01 +00:00
|
|
|
</div>
|
|
|
|
</CustomMenu.MenuItem>
|
2023-04-21 19:29:57 +00:00
|
|
|
{projectDetails?.estimate !== estimate.id && (
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
setIsDeleteEstimateModalOpen(true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="flex items-center justify-start gap-2">
|
|
|
|
<TrashIcon className="h-3.5 w-3.5" />
|
|
|
|
<span>Delete estimate</span>
|
|
|
|
</div>
|
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
)}
|
|
|
|
</CustomMenu>
|
|
|
|
</div>
|
2023-04-06 09:39:24 +00:00
|
|
|
</div>
|
2023-04-21 19:29:57 +00:00
|
|
|
{estimate.points.length > 0 ? (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="flex text-xs text-custom-text-200">
|
2023-04-17 05:45:06 +00:00
|
|
|
Estimate points (
|
|
|
|
<span className="flex gap-1">
|
2023-04-21 19:29:57 +00:00
|
|
|
{orderArrayBy(estimate.points, "key").map((point, index) => (
|
2023-07-10 07:17:00 +00:00
|
|
|
<h6 key={point.id} className="text-custom-text-200">
|
2023-04-17 05:45:06 +00:00
|
|
|
{point.value}
|
2023-04-21 19:29:57 +00:00
|
|
|
{index !== estimate.points.length - 1 && ","}{" "}
|
2023-04-17 05:45:06 +00:00
|
|
|
</h6>
|
|
|
|
))}
|
|
|
|
</span>
|
2023-04-11 12:24:01 +00:00
|
|
|
)
|
2023-04-06 09:39:24 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2023-04-08 12:35:54 +00:00
|
|
|
<div>
|
2023-07-10 07:17:00 +00:00
|
|
|
<p className="text-xs text-custom-text-200">No estimate points</p>
|
2023-04-08 12:35:54 +00:00
|
|
|
</div>
|
2023-04-06 09:39:24 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2023-04-12 09:33:04 +00:00
|
|
|
|
|
|
|
<DeleteEstimateModal
|
|
|
|
isOpen={isDeleteEstimateModalOpen}
|
|
|
|
handleClose={() => setIsDeleteEstimateModalOpen(false)}
|
|
|
|
data={estimate}
|
|
|
|
handleDelete={() => {
|
|
|
|
handleEstimateDelete(estimate.id);
|
|
|
|
setIsDeleteEstimateModalOpen(false);
|
|
|
|
}}
|
|
|
|
/>
|
2023-04-08 12:35:54 +00:00
|
|
|
</>
|
2023-04-06 09:39:24 +00:00
|
|
|
);
|
|
|
|
};
|