From ed3c4daaf6feff059616beb509c3d6e19d209107 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 13 Mar 2024 00:26:33 +0700 Subject: [PATCH] delete action --- src/entities/chain/model.ts | 35 +++++++++++++++++++++++ src/widgets/action-card/ui/ActionCard.tsx | 16 ++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/entities/chain/model.ts b/src/entities/chain/model.ts index 43cea6f..9c28b0b 100644 --- a/src/entities/chain/model.ts +++ b/src/entities/chain/model.ts @@ -13,6 +13,7 @@ interface ChainState { deleteChain: (id: string) => void; addCommentAction: (chainId: string, action: Action) => void; updateAction: (chainId: string, actionIndex: number, action: Action) => void; + deleteAction: (chainId: string, actionIndex: number) => void; } export const useChainState = create((set, get) => ({ @@ -107,4 +108,38 @@ export const useChainState = create((set, get) => ({ chain.actions[actionIndex] = action; return state; }), + + deleteAction: (chainId, actionIndex) => + set((state) => { + const chains = state.chains; + if (!chains) return state; + + const chain = state.getChain(chainId); + const chainIndex = chains.findIndex((value) => value._id === chainId); + if (chainIndex === -1) return state; + + if ( + !chain || + typeof chain.actions === "undefined" || + !chain.actions[actionIndex] + ) + return state; + + let newActions = chain.actions + .slice(0, actionIndex) + .concat(chain.actions.slice(actionIndex + 1)); + + if (newActions[actionIndex]?.actionType === "wait") + newActions = newActions + .slice(0, actionIndex) + .concat(newActions.slice(actionIndex + 1)); + + if (newActions.at(-1)?.actionType === "wait") { + newActions = newActions.slice(0, -1); + } + + chain.actions = newActions; + chains[chainIndex] = { ...chain, actions: newActions }; + return state; + }), })); diff --git a/src/widgets/action-card/ui/ActionCard.tsx b/src/widgets/action-card/ui/ActionCard.tsx index d689daa..18dee57 100644 --- a/src/widgets/action-card/ui/ActionCard.tsx +++ b/src/widgets/action-card/ui/ActionCard.tsx @@ -2,6 +2,9 @@ import type { Chain } from "@/entities/chain/schema"; import type { Action } from "@/entities/action/schema"; import { useState } from "react"; +import { useNavigate } from "@tanstack/react-router"; +import { useChainState } from "@/entities/chain/model"; +import { upsertChain } from "@/entities/chain/api/upsert"; import humanizeDuration from "humanize-duration"; import { ChangeWaitForButton } from "@/features/change-wait-for"; @@ -20,6 +23,7 @@ interface ActionCardProps { action: Action; actionIndex: number; onEdit?: (actionIndex: number) => void; + onClose?: (actionIndex: number) => void; } export default function ActionCard({ @@ -31,6 +35,9 @@ export default function ActionCard({ const [isOpen, setIsOpen] = useState(false); const [anchorEl, setAnchorEl] = useState(null); + const navigate = useNavigate({ from: "/$namespace/$chainId" }); + const deleteAction = useChainState((state) => state.deleteAction); + if (action.actionType === "comment") { const attachmentType = getActionAttachmentType(action); @@ -98,7 +105,14 @@ export default function ActionCard({ - + { + deleteAction(chain._id!, actionIndex); + setIsOpen(false); + await upsertChain(chain); + navigate({ to: "/$namespace/$chainId" }); + }} + >