add chain logic

This commit is contained in:
Robert 2024-03-08 05:35:05 +07:00
parent 7812c8ccad
commit 3ab9d5e06c
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
2 changed files with 20 additions and 0 deletions

View 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;
}
}

View File

@ -4,8 +4,15 @@ import type { Chain } from "./schema";
interface ChainState {
chains?: Chain[];
setChains: (chains: Chain[]) => void;
addChain: (chain: Chain) => void;
}
export const useChainState = create<ChainState>((set) => ({
setChains: (chains) => set({ chains }),
addChain: (chain) =>
set((state) => {
if (!state.chains) return { chains: [chain] };
return { chains: [...state.chains, chain] };
}),
}));