import React, { useState, useRef } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // services import estimatesService from "services/estimates.service"; import projectService from "services/project.service"; // lib import { requiredAdmin } from "lib/auth"; // layouts import AppLayout from "layouts/app-layout"; // components import { CreateUpdateEstimateModal, SingleEstimate } from "components/estimates"; //hooks import useToast from "hooks/use-toast"; // ui import { Loader, PrimaryButton } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons import { PlusIcon } from "@heroicons/react/24/outline"; // types import { IEstimate, UserAuth, IProject } from "types"; import type { GetServerSidePropsContext, NextPage } from "next"; // fetch-keys import { ESTIMATES_LIST, PROJECT_DETAILS } from "constants/fetch-keys"; const EstimatesSettings: NextPage = (props) => { const { isMember, isOwner, isViewer, isGuest } = props; const [estimateFormOpen, setEstimateFormOpen] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const [estimateToUpdate, setEstimateToUpdate] = useState(); const [activeEstimate, setActiveEstimate] = useState(null); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const scollToRef = useRef(null); const { data: estimatesList } = useSWR( workspaceSlug && projectId ? ESTIMATES_LIST(projectId as string) : null, workspaceSlug && projectId ? () => estimatesService.getEstimatesList(workspaceSlug as string, projectId as string) : null ); const editEstimate = (estimate: IEstimate) => { setIsUpdating(true); setEstimateToUpdate(estimate); setEstimateFormOpen(true); }; const removeEstimate = (estimateId: string) => { if (!workspaceSlug || !projectId) return; mutate( ESTIMATES_LIST(projectId as string), (prevData) => (prevData ?? []).filter((p) => p.id !== estimateId), false ); estimatesService .deleteEstimate(workspaceSlug as string, projectId as string, estimateId) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Error: Estimate could not be deleted. Please try again", }); }); }; const { data: projectDetails } = useSWR( workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, workspaceSlug && projectId ? () => projectService.getProject(workspaceSlug as string, projectId as string) : null ); return ( <> } settingsLayout > { setEstimateFormOpen(false); setEstimateToUpdate(undefined); }} />

Estimates

{ setEstimateToUpdate(undefined); setEstimateFormOpen(true); }} > Create New Estimate

{estimatesList && estimatesList.length > 0 && (
<> {estimatesList ? ( estimatesList.map((estimate) => ( editEstimate(estimate)} handleEstimateDelete={(estimateId) => removeEstimate(estimateId)} /> )) ) : ( )}
)}
); }; export const getServerSideProps = async (ctx: GetServerSidePropsContext) => { const projectId = ctx.query.projectId as string; const workspaceSlug = ctx.query.workspaceSlug as string; const memberDetail = await requiredAdmin(workspaceSlug, projectId, ctx.req?.headers.cookie); return { props: { isOwner: memberDetail?.role === 20, isMember: memberDetail?.role === 15, isViewer: memberDetail?.role === 10, isGuest: memberDetail?.role === 5, }, }; }; export default EstimatesSettings;