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/services/memoService.ts

101 lines
2.4 KiB
TypeScript

4 years ago
import api from "../helpers/api";
import { TAG_REG } from "../helpers/consts";
import { createMemo, patchMemo, setMemos, setTags } from "../store/modules/memo";
import store from "../store";
4 years ago
const convertResponseModelMemo = (memo: Memo): Memo => {
return {
...memo,
createdTs: memo.createdTs * 1000,
updatedTs: memo.updatedTs * 1000,
};
};
4 years ago
const memoService = {
getState: () => {
return store.getState().memo;
},
4 years ago
fetchAllMemos: async () => {
const data = await api.getMyMemos();
const memos = data.filter((m) => m.rowStatus !== "ARCHIVED").map((m) => convertResponseModelMemo(m));
store.dispatch(setMemos(memos));
4 years ago
return memos;
},
4 years ago
fetchDeletedMemos: async () => {
const data = await api.getMyArchivedMemos();
const deletedMemos = data.map((m) => {
return convertResponseModelMemo(m);
});
return deletedMemos;
},
4 years ago
getMemoById: (memoId: MemoId) => {
for (const m of memoService.getState().memos) {
if (m.id === memoId) {
4 years ago
return m;
}
}
return null;
},
4 years ago
updateTagsState: () => {
const { memos } = memoService.getState();
4 years ago
const tagsSet = new Set<string>();
for (const m of memos) {
for (const t of Array.from(m.content.match(TAG_REG) ?? [])) {
tagsSet.add(t.replace(TAG_REG, "$1").trim());
}
}
store.dispatch(setTags(Array.from(tagsSet).filter((t) => Boolean(t))));
},
getLinkedMemos: async (memoId: MemoId): Promise<Memo[]> => {
const { memos } = memoService.getState();
return memos.filter((m) => m.content.includes(`${memoId}`));
},
createMemo: async (memoCreate: MemoCreate) => {
const data = await api.createMemo(memoCreate);
const memo = convertResponseModelMemo(data);
store.dispatch(createMemo(memo));
},
4 years ago
patchMemo: async (memoPatch: MemoPatch): Promise<Memo> => {
const data = await api.patchMemo(memoPatch);
const memo = convertResponseModelMemo(data);
store.dispatch(patchMemo(memo));
return memo;
},
pinMemo: async (memoId: MemoId) => {
await api.pinMemo(memoId);
store.dispatch(
patchMemo({
id: memoId,
pinned: true,
})
);
},
unpinMemo: async (memoId: MemoId) => {
await api.unpinMemo(memoId);
store.dispatch(
patchMemo({
id: memoId,
pinned: false,
})
);
},
deleteMemoById: async (memoId: MemoId) => {
await api.deleteMemo(memoId);
},
};
4 years ago
export default memoService;