2023-04-08 12:35:54 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import estimatesService from "services/estimates.service";
|
|
|
|
// hooks
|
|
|
|
import useProjectDetails from "hooks/use-project-details";
|
2023-04-11 12:24:01 +00:00
|
|
|
// helpers
|
|
|
|
import { orderArrayBy } from "helpers/array.helper";
|
2023-04-08 12:35:54 +00:00
|
|
|
// fetch-keys
|
2023-04-21 19:29:57 +00:00
|
|
|
import { ESTIMATE_DETAILS } from "constants/fetch-keys";
|
2023-04-08 12:35:54 +00:00
|
|
|
|
2023-04-17 06:00:48 +00:00
|
|
|
const useEstimateOption = (estimateKey?: number | null) => {
|
2023-04-08 12:35:54 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { projectDetails } = useProjectDetails();
|
|
|
|
|
2023-04-21 19:29:57 +00:00
|
|
|
const { data: estimateDetails, error: estimateDetailsError } = useSWR(
|
2023-04-08 12:35:54 +00:00
|
|
|
workspaceSlug && projectId && projectDetails && projectDetails?.estimate
|
2023-04-21 19:29:57 +00:00
|
|
|
? ESTIMATE_DETAILS(projectDetails.estimate as string)
|
2023-04-08 12:35:54 +00:00
|
|
|
: null,
|
|
|
|
workspaceSlug && projectId && projectDetails && projectDetails.estimate
|
|
|
|
? () =>
|
2023-04-21 19:29:57 +00:00
|
|
|
estimatesService.getEstimateDetails(
|
|
|
|
workspaceSlug.toString(),
|
|
|
|
projectId.toString(),
|
2023-04-11 12:24:01 +00:00
|
|
|
projectDetails.estimate as string
|
2023-04-08 12:35:54 +00:00
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const estimateValue: any =
|
2023-04-21 19:29:57 +00:00
|
|
|
(estimateKey && estimateDetails?.points?.find((e) => e.key === estimateKey)?.value) ?? "None";
|
2023-04-08 12:35:54 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isEstimateActive: projectDetails?.estimate ? true : false,
|
2023-04-21 19:29:57 +00:00
|
|
|
estimatePoints: orderArrayBy(estimateDetails?.points ?? [], "key"),
|
2023-04-08 12:35:54 +00:00
|
|
|
estimateValue,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useEstimateOption;
|