delete action

This commit is contained in:
Robert 2024-03-13 00:26:33 +07:00
parent 4da1b081a8
commit ed3c4daaf6
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
2 changed files with 50 additions and 1 deletions

View File

@ -13,6 +13,7 @@ interface ChainState {
deleteChain: (id: string) => void; deleteChain: (id: string) => void;
addCommentAction: (chainId: string, action: Action) => void; addCommentAction: (chainId: string, action: Action) => void;
updateAction: (chainId: string, actionIndex: number, action: Action) => void; updateAction: (chainId: string, actionIndex: number, action: Action) => void;
deleteAction: (chainId: string, actionIndex: number) => void;
} }
export const useChainState = create<ChainState>((set, get) => ({ export const useChainState = create<ChainState>((set, get) => ({
@ -107,4 +108,38 @@ export const useChainState = create<ChainState>((set, get) => ({
chain.actions[actionIndex] = action; chain.actions[actionIndex] = action;
return state; 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;
}),
})); }));

View File

@ -2,6 +2,9 @@ import type { Chain } from "@/entities/chain/schema";
import type { Action } from "@/entities/action/schema"; import type { Action } from "@/entities/action/schema";
import { useState } from "react"; 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 humanizeDuration from "humanize-duration";
import { ChangeWaitForButton } from "@/features/change-wait-for"; import { ChangeWaitForButton } from "@/features/change-wait-for";
@ -20,6 +23,7 @@ interface ActionCardProps {
action: Action; action: Action;
actionIndex: number; actionIndex: number;
onEdit?: (actionIndex: number) => void; onEdit?: (actionIndex: number) => void;
onClose?: (actionIndex: number) => void;
} }
export default function ActionCard({ export default function ActionCard({
@ -31,6 +35,9 @@ export default function ActionCard({
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const navigate = useNavigate({ from: "/$namespace/$chainId" });
const deleteAction = useChainState((state) => state.deleteAction);
if (action.actionType === "comment") { if (action.actionType === "comment") {
const attachmentType = getActionAttachmentType(action); const attachmentType = getActionAttachmentType(action);
@ -98,7 +105,14 @@ export default function ActionCard({
<Divider sx={{ my: 0.5 }} /> <Divider sx={{ my: 0.5 }} />
<MenuItem> <MenuItem
onClick={async () => {
deleteAction(chain._id!, actionIndex);
setIsOpen(false);
await upsertChain(chain);
navigate({ to: "/$namespace/$chainId" });
}}
>
<DeleteOutlineIcon <DeleteOutlineIcon
sx={{ color: "#4C4E64DE", opacity: "87%" }} sx={{ color: "#4C4E64DE", opacity: "87%" }}
className="mr-2" className="mr-2"