delete chain logic

This commit is contained in:
Robert 2024-03-08 22:43:11 +07:00
parent d380f259b1
commit 7eefcecdb5
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
2 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import { getAxios } from "@/shared/lib/getAxios";
export async function deleteChainById(id: string) {
const axios = getAxios();
await axios.delete(`/chain/delete/${id}`);
}

View File

@ -7,6 +7,7 @@ interface ChainState {
addChain: (chain: Chain) => void;
getChain: (id: string) => Chain | null;
updateChain: (chain: Chain) => void;
deleteChain: (id: string) => void;
}
export const useChainState = create<ChainState>((set, get) => ({
@ -43,4 +44,19 @@ export const useChainState = create<ChainState>((set, get) => ({
],
};
}),
deleteChain: (id) =>
set((state) => {
const chains = state.chains;
if (!chains) return state;
const chainIndex = chains.findIndex((value) => value._id === id);
if (chainIndex === -1) return state;
return {
chains: [
...chains.slice(0, chainIndex),
...chains.slice(chainIndex + 1),
],
};
}),
}));