delete action
This commit is contained in:
parent
4da1b081a8
commit
ed3c4daaf6
|
|
@ -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<ChainState>((set, get) => ({
|
||||
|
|
@ -107,4 +108,38 @@ export const useChainState = create<ChainState>((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;
|
||||
}),
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -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 | HTMLElement>(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({
|
|||
|
||||
<Divider sx={{ my: 0.5 }} />
|
||||
|
||||
<MenuItem>
|
||||
<MenuItem
|
||||
onClick={async () => {
|
||||
deleteAction(chain._id!, actionIndex);
|
||||
setIsOpen(false);
|
||||
await upsertChain(chain);
|
||||
navigate({ to: "/$namespace/$chainId" });
|
||||
}}
|
||||
>
|
||||
<DeleteOutlineIcon
|
||||
sx={{ color: "#4C4E64DE", opacity: "87%" }}
|
||||
className="mr-2"
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user