feat: can't delete state with issues

This commit is contained in:
Dakshesh Jain 2022-12-15 10:35:05 +05:30
parent 2d86e9fc88
commit 41271a5e3f

View File

@ -9,6 +9,8 @@ import stateServices from "lib/services/state.service";
import { STATE_LIST } from "constants/fetch-keys"; import { STATE_LIST } from "constants/fetch-keys";
// hooks // hooks
import useUser from "lib/hooks/useUser"; import useUser from "lib/hooks/useUser";
// common
import { groupBy } from "constants/common";
// icons // icons
import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import { ExclamationTriangleIcon } from "@heroicons/react/24/outline";
// ui // ui
@ -25,7 +27,9 @@ type Props = {
const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => { const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => {
const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [isDeleteLoading, setIsDeleteLoading] = useState(false);
const { activeWorkspace } = useUser(); const [issuesWithThisStateExist, setIssuesWithThisStateExist] = useState(true);
const { activeWorkspace, issues } = useUser();
const cancelButtonRef = useRef(null); const cancelButtonRef = useRef(null);
@ -36,7 +40,7 @@ const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => {
const handleDeletion = async () => { const handleDeletion = async () => {
setIsDeleteLoading(true); setIsDeleteLoading(true);
if (!data || !activeWorkspace) return; if (!data || !activeWorkspace || issuesWithThisStateExist) return;
await stateServices await stateServices
.deleteState(activeWorkspace.slug, data.project, data.id) .deleteState(activeWorkspace.slug, data.project, data.id)
.then(() => { .then(() => {
@ -53,6 +57,12 @@ const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => {
}); });
}; };
const groupedIssues = groupBy(issues?.results ?? [], "state");
useEffect(() => {
if (data) setIssuesWithThisStateExist(!!groupedIssues[data.id]);
}, [groupedIssues]);
return ( return (
<Transition.Root show={isOpen} as={React.Fragment}> <Transition.Root show={isOpen} as={React.Fragment}>
<Dialog <Dialog
@ -105,6 +115,14 @@ const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => {
This action cannot be undone. This action cannot be undone.
</p> </p>
</div> </div>
<div className="mt-2">
{issuesWithThisStateExist && (
<p className="text-sm text-red-500">
There are issues with this state. Please move them to another state
before deleting this state.
</p>
)}
</div>
</div> </div>
</div> </div>
</div> </div>
@ -113,7 +131,7 @@ const ConfirmStateDeletion: React.FC<Props> = ({ isOpen, onClose, data }) => {
type="button" type="button"
onClick={handleDeletion} onClick={handleDeletion}
theme="danger" theme="danger"
disabled={isDeleteLoading} disabled={isDeleteLoading || issuesWithThisStateExist}
className="inline-flex sm:ml-3" className="inline-flex sm:ml-3"
> >
{isDeleteLoading ? "Deleting..." : "Delete"} {isDeleteLoading ? "Deleting..." : "Delete"}