add chain logic
This commit is contained in:
parent
7812c8ccad
commit
3ab9d5e06c
13
src/entities/chain/api/upsert.ts
Normal file
13
src/entities/chain/api/upsert.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { chainSchema, type Chain } from "../schema";
|
||||||
|
import { getAxios } from "@/shared/lib/getAxios";
|
||||||
|
|
||||||
|
export async function upsertChain(chain: Chain) {
|
||||||
|
const axios = getAxios();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.post("/chain", chain);
|
||||||
|
return chainSchema.parse(response.data);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,8 +4,15 @@ import type { Chain } from "./schema";
|
||||||
interface ChainState {
|
interface ChainState {
|
||||||
chains?: Chain[];
|
chains?: Chain[];
|
||||||
setChains: (chains: Chain[]) => void;
|
setChains: (chains: Chain[]) => void;
|
||||||
|
addChain: (chain: Chain) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useChainState = create<ChainState>((set) => ({
|
export const useChainState = create<ChainState>((set) => ({
|
||||||
setChains: (chains) => set({ chains }),
|
setChains: (chains) => set({ chains }),
|
||||||
|
|
||||||
|
addChain: (chain) =>
|
||||||
|
set((state) => {
|
||||||
|
if (!state.chains) return { chains: [chain] };
|
||||||
|
return { chains: [...state.chains, chain] };
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user