plane/web/components/estimates/estimate-disable-switch.tsx

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-05-24 11:54:50 +00:00
import { FC } from "react";
import { observer } from "mobx-react";
import { TOAST_TYPE, ToggleSwitch, setToast } from "@plane/ui";
// hooks
import { useProject, useProjectEstimates } from "@/hooks/store";
2024-05-24 11:54:50 +00:00
type TEstimateDisableSwitch = {
workspaceSlug: string;
projectId: string;
isAdmin: boolean;
};
export const EstimateDisableSwitch: FC<TEstimateDisableSwitch> = observer((props) => {
const { workspaceSlug, projectId, isAdmin } = props;
// hooks
const { updateProject, currentProjectDetails } = useProject();
const { currentActiveEstimateId } = useProjectEstimates();
const currentProjectActiveEstimate = currentProjectDetails?.estimate || undefined;
2024-05-24 11:54:50 +00:00
const disableEstimate = async () => {
if (!workspaceSlug || !projectId) return;
2024-05-24 11:54:50 +00:00
try {
await updateProject(workspaceSlug, projectId, {
estimate: currentProjectActiveEstimate ? null : currentActiveEstimateId,
});
2024-05-24 11:54:50 +00:00
setToast({
type: TOAST_TYPE.SUCCESS,
title: "Success!",
2024-05-29 09:06:04 +00:00
message: currentProjectActiveEstimate ? "Estimates have been disabled" : "Estimates have been enabled",
2024-05-24 11:54:50 +00:00
});
} catch (err) {
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: "Estimate could not be disabled. Please try again",
});
}
};
return (
<ToggleSwitch
value={Boolean(currentProjectActiveEstimate)}
2024-05-24 11:54:50 +00:00
onChange={disableEstimate}
disabled={!isAdmin}
size="sm"
/>
);
});