From 02078fcaa235b9ef8e9c53f93923a6a7c4e177e3 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 13 Mar 2024 01:11:46 +0700 Subject: [PATCH] move actions --- src/entities/chain/model.ts | 60 +++++++++++++++++++++++ src/widgets/action-card/ui/ActionCard.tsx | 20 +++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/entities/chain/model.ts b/src/entities/chain/model.ts index 9c28b0b..6635d1a 100644 --- a/src/entities/chain/model.ts +++ b/src/entities/chain/model.ts @@ -14,6 +14,8 @@ interface ChainState { addCommentAction: (chainId: string, action: Action) => void; updateAction: (chainId: string, actionIndex: number, action: Action) => void; deleteAction: (chainId: string, actionIndex: number) => void; + moveActionUp: (chainId: string, actionIndex: number) => void; + moveActionDown: (chainId: string, actionIndex: number) => void; } export const useChainState = create((set, get) => ({ @@ -138,6 +140,64 @@ export const useChainState = create((set, get) => ({ newActions = newActions.slice(0, -1); } + chain.actions = newActions; + chains[chainIndex] = { ...chain, actions: newActions }; + return state; + }), + + moveActionUp: (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]; + const actionIndexToSwap = actionIndex - 2; + + if (actionIndexToSwap < 0) return state; + + newActions[actionIndexToSwap] = chain.actions[actionIndex]; + newActions[actionIndex] = chain.actions[actionIndexToSwap]; + + chain.actions = newActions; + chains[chainIndex] = { ...chain, actions: newActions }; + return state; + }), + + moveActionDown: (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]; + const actionIndexToSwap = actionIndex + 2; + + if (actionIndexToSwap + 1 > chain.actions.length) return state; + + newActions[actionIndexToSwap] = chain.actions[actionIndex]; + newActions[actionIndex] = chain.actions[actionIndexToSwap]; + 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 18dee57..cc75d6a 100644 --- a/src/widgets/action-card/ui/ActionCard.tsx +++ b/src/widgets/action-card/ui/ActionCard.tsx @@ -37,6 +37,8 @@ export default function ActionCard({ const navigate = useNavigate({ from: "/$namespace/$chainId" }); const deleteAction = useChainState((state) => state.deleteAction); + const moveActionUp = useChainState((state) => state.moveActionUp); + const moveActionDown = useChainState((state) => state.moveActionDown); if (action.actionType === "comment") { const attachmentType = getActionAttachmentType(action); @@ -83,7 +85,14 @@ export default function ActionCard({ - + { + moveActionUp(chain._id!, actionIndex); + setIsOpen(false); + await upsertChain(chain); + navigate({ to: "/$namespace/$chainId" }); + }} + > - + { + moveActionDown(chain._id!, actionIndex); + setIsOpen(false); + await upsertChain(chain); + navigate({ to: "/$namespace/$chainId" }); + }} + >