You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/store/v1/memo.ts

42 lines
1.1 KiB
TypeScript

import { create } from "zustand";
import { combine } from "zustand/middleware";
import * as api from "@/helpers/api";
import { convertResponseModelMemo } from "../module";
export const useMemoCacheStore = create(
combine({ memoById: new Map<MemoId, Memo>() }, (set, get) => ({
getState: () => get(),
getOrFetchMemoById: async (memoId: MemoId) => {
const memo = get().memoById.get(memoId);
if (memo) {
return memo;
}
const { data } = await api.getMemoById(memoId);
const formatedMemo = convertResponseModelMemo(data);
set((state) => {
state.memoById.set(memoId, formatedMemo);
return state;
});
return formatedMemo;
},
getMemoById: (memoId: MemoId) => {
return get().memoById.get(memoId);
},
setMemoCache: (memo: Memo) => {
set((state) => {
state.memoById.set(memo.id, memo);
return state;
});
},
deleteMemoCache: (memoId: MemoId) => {
set((state) => {
state.memoById.delete(memoId);
return state;
});
},
}))
);