2023-04-11 12:24:01 +00:00
|
|
|
import React, { useState } from "react";
|
2023-04-06 09:39:24 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import estimatesService from "services/estimates.service";
|
2023-04-21 19:29:57 +00:00
|
|
|
import projectService from "services/project.service";
|
2023-04-08 12:35:54 +00:00
|
|
|
// hooks
|
|
|
|
import useProjectDetails from "hooks/use-project-details";
|
2023-04-06 09:39:24 +00:00
|
|
|
// layouts
|
2023-04-08 08:16:46 +00:00
|
|
|
import { ProjectAuthorizationWrapper } from "layouts/auth-layout";
|
2023-04-06 09:39:24 +00:00
|
|
|
// components
|
|
|
|
import { CreateUpdateEstimateModal, SingleEstimate } from "components/estimates";
|
2023-05-15 06:05:19 +00:00
|
|
|
import { SettingsHeader } from "components/project";
|
2023-04-06 09:39:24 +00:00
|
|
|
//hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-06-06 16:06:00 +00:00
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-04-06 09:39:24 +00:00
|
|
|
// ui
|
2023-04-24 13:23:18 +00:00
|
|
|
import { EmptyState, Loader, SecondaryButton } from "components/ui";
|
2023-04-06 09:39:24 +00:00
|
|
|
import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs";
|
|
|
|
// icons
|
|
|
|
import { PlusIcon } from "@heroicons/react/24/outline";
|
2023-04-24 13:23:18 +00:00
|
|
|
// images
|
|
|
|
import emptyEstimate from "public/empty-state/empty-estimate.svg";
|
2023-04-06 09:39:24 +00:00
|
|
|
// types
|
2023-04-11 12:24:01 +00:00
|
|
|
import { IEstimate, IProject } from "types";
|
2023-04-08 08:16:46 +00:00
|
|
|
import type { NextPage } from "next";
|
2023-04-06 09:39:24 +00:00
|
|
|
// fetch-keys
|
2023-04-11 12:24:01 +00:00
|
|
|
import { ESTIMATES_LIST, PROJECT_DETAILS } from "constants/fetch-keys";
|
2023-04-06 09:39:24 +00:00
|
|
|
|
2023-04-08 08:16:46 +00:00
|
|
|
const EstimatesSettings: NextPage = () => {
|
2023-04-06 09:39:24 +00:00
|
|
|
const [estimateFormOpen, setEstimateFormOpen] = useState(false);
|
|
|
|
|
|
|
|
const [estimateToUpdate, setEstimateToUpdate] = useState<IEstimate | undefined>();
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-06-06 16:06:00 +00:00
|
|
|
const { user } = useUserAuth();
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-04-08 12:35:54 +00:00
|
|
|
const { projectDetails } = useProjectDetails();
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
const { data: estimatesList } = useSWR<IEstimate[]>(
|
|
|
|
workspaceSlug && projectId ? ESTIMATES_LIST(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => estimatesService.getEstimatesList(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const editEstimate = (estimate: IEstimate) => {
|
|
|
|
setEstimateToUpdate(estimate);
|
|
|
|
setEstimateFormOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeEstimate = (estimateId: string) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
mutate<IEstimate[]>(
|
|
|
|
ESTIMATES_LIST(projectId as string),
|
|
|
|
(prevData) => (prevData ?? []).filter((p) => p.id !== estimateId),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
estimatesService
|
2023-06-06 16:06:00 +00:00
|
|
|
.deleteEstimate(workspaceSlug as string, projectId as string, estimateId, user)
|
2023-04-06 09:39:24 +00:00
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Error: Estimate could not be deleted. Please try again",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-11 12:24:01 +00:00
|
|
|
const disableEstimates = () => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
mutate<IProject>(
|
|
|
|
PROJECT_DETAILS(projectId as string),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return { ...prevData, estimate: null };
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
projectService
|
2023-06-06 16:06:00 +00:00
|
|
|
.updateProject(workspaceSlug as string, projectId as string, { estimate: null }, user)
|
2023-04-11 12:24:01 +00:00
|
|
|
.catch(() =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Estimate could not be disabled. Please try again",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-04-06 09:39:24 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-05-05 11:37:29 +00:00
|
|
|
<CreateUpdateEstimateModal
|
|
|
|
isOpen={estimateFormOpen}
|
|
|
|
data={estimateToUpdate}
|
|
|
|
handleClose={() => {
|
|
|
|
setEstimateFormOpen(false);
|
|
|
|
setEstimateToUpdate(undefined);
|
|
|
|
}}
|
2023-06-06 16:06:00 +00:00
|
|
|
user={user}
|
2023-05-05 11:37:29 +00:00
|
|
|
/>
|
2023-04-08 08:16:46 +00:00
|
|
|
<ProjectAuthorizationWrapper
|
2023-04-06 09:39:24 +00:00
|
|
|
breadcrumbs={
|
|
|
|
<Breadcrumbs>
|
|
|
|
<BreadcrumbItem
|
|
|
|
title={`${projectDetails?.name ?? "Project"}`}
|
|
|
|
link={`/${workspaceSlug}/projects/${projectDetails?.id}/issues`}
|
|
|
|
/>
|
|
|
|
<BreadcrumbItem title="Estimates Settings" />
|
|
|
|
</Breadcrumbs>
|
|
|
|
}
|
|
|
|
>
|
2023-05-17 09:27:58 +00:00
|
|
|
<div className="p-8">
|
2023-05-05 11:37:29 +00:00
|
|
|
<SettingsHeader />
|
|
|
|
<section className="flex items-center justify-between">
|
|
|
|
<h3 className="text-2xl font-semibold">Estimates</h3>
|
|
|
|
<div className="col-span-12 space-y-5 sm:col-span-7">
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<span
|
|
|
|
className="flex cursor-pointer items-center gap-2 text-theme"
|
|
|
|
onClick={() => {
|
|
|
|
setEstimateToUpdate(undefined);
|
|
|
|
setEstimateFormOpen(true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PlusIcon className="h-4 w-4" />
|
|
|
|
Create New Estimate
|
|
|
|
</span>
|
|
|
|
{projectDetails?.estimate && (
|
|
|
|
<SecondaryButton onClick={disableEstimates}>Disable Estimates</SecondaryButton>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-04-06 09:39:24 +00:00
|
|
|
</div>
|
2023-05-05 11:37:29 +00:00
|
|
|
</section>
|
|
|
|
{estimatesList ? (
|
|
|
|
estimatesList.length > 0 ? (
|
2023-07-10 07:17:00 +00:00
|
|
|
<section className="mt-5 divide-y divide-custom-border-100 rounded-xl border border-custom-border-100 bg-custom-background-100 px-6">
|
2023-05-05 11:37:29 +00:00
|
|
|
{estimatesList.map((estimate) => (
|
|
|
|
<SingleEstimate
|
|
|
|
key={estimate.id}
|
|
|
|
estimate={estimate}
|
|
|
|
editEstimate={(estimate) => editEstimate(estimate)}
|
|
|
|
handleEstimateDelete={(estimateId) => removeEstimate(estimateId)}
|
2023-06-06 16:06:00 +00:00
|
|
|
user={user}
|
2023-05-05 11:37:29 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</section>
|
|
|
|
) : (
|
|
|
|
<div className="mt-5">
|
|
|
|
<EmptyState
|
|
|
|
type="estimate"
|
|
|
|
title="Create New Estimate"
|
|
|
|
description="Estimates help you communicate the complexity of an issue. You can create your own estimate and communicate with your team."
|
|
|
|
imgURL={emptyEstimate}
|
|
|
|
action={() => {
|
|
|
|
setEstimateToUpdate(undefined);
|
|
|
|
setEstimateFormOpen(true);
|
|
|
|
}}
|
2023-04-24 13:23:18 +00:00
|
|
|
/>
|
2023-05-05 11:37:29 +00:00
|
|
|
</div>
|
|
|
|
)
|
2023-04-24 13:23:18 +00:00
|
|
|
) : (
|
2023-05-05 11:37:29 +00:00
|
|
|
<Loader className="mt-5 space-y-5">
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
</Loader>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-04-08 08:16:46 +00:00
|
|
|
</ProjectAuthorizationWrapper>
|
2023-04-06 09:39:24 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EstimatesSettings;
|