From a580df5c9f9b0f19770c73fd8e3ddc84d571bee0 Mon Sep 17 00:00:00 2001 From: boojack Date: Sun, 22 May 2022 11:01:20 +0800 Subject: [PATCH] chore: update redux store and service --- web/src/components/ChangePasswordDialog.tsx | 4 +- .../components/ConfirmResetOpenIdDialog.tsx | 4 +- web/src/components/CreateShortcutDialog.tsx | 11 ++- web/src/components/DeletedMemo.tsx | 5 +- web/src/components/Memo.tsx | 19 ++-- web/src/components/MemoCardDialog.tsx | 2 +- web/src/components/MemoEditor.tsx | 18 ++-- web/src/components/MemoFilter.tsx | 4 +- web/src/components/MemoTrashDialog.tsx | 4 +- web/src/components/MenuBtnsPopup.tsx | 4 +- .../components/Settings/MyAccountSection.tsx | 5 +- .../Settings/PreferencesSection.tsx | 6 +- web/src/components/ShortcutList.tsx | 18 +--- web/src/components/UsageHeatMap.tsx | 2 +- web/src/helpers/api.ts | 56 +---------- web/src/less/search-bar.less | 2 +- web/src/services/editorStateService.ts | 12 ++- web/src/services/locationService.ts | 58 +++++------ web/src/services/memoService.ts | 96 ++++++------------- web/src/services/shortcutService.ts | 45 ++------- web/src/services/userService.ts | 31 ++---- web/src/store/modules/editor.ts | 10 +- web/src/store/modules/location.ts | 37 +++++-- web/src/store/modules/memo.ts | 5 +- web/src/store/modules/user.ts | 10 +- web/src/types/location.d.ts | 20 ---- web/src/types/modules/shortcut.d.ts | 12 +++ 27 files changed, 196 insertions(+), 304 deletions(-) diff --git a/web/src/components/ChangePasswordDialog.tsx b/web/src/components/ChangePasswordDialog.tsx index 3fc98105..7aa924c1 100644 --- a/web/src/components/ChangePasswordDialog.tsx +++ b/web/src/components/ChangePasswordDialog.tsx @@ -55,7 +55,9 @@ const ChangePasswordDialog: React.FC = ({ destroy }: Props) => { } try { - await userService.updatePassword(newPassword); + await userService.patchUser({ + password: newPassword, + }); toastHelper.info("Password changed."); handleCloseBtnClick(); } catch (error: any) { diff --git a/web/src/components/ConfirmResetOpenIdDialog.tsx b/web/src/components/ConfirmResetOpenIdDialog.tsx index e301d696..ffa60597 100644 --- a/web/src/components/ConfirmResetOpenIdDialog.tsx +++ b/web/src/components/ConfirmResetOpenIdDialog.tsx @@ -25,7 +25,9 @@ const ConfirmResetOpenIdDialog: React.FC = ({ destroy }: Props) => { resetBtnClickLoadingState.setLoading(); try { - await userService.resetOpenId(); + await userService.patchUser({ + resetOpenId: true, + }); } catch (error) { toastHelper.error("Request reset open API failed."); return; diff --git a/web/src/components/CreateShortcutDialog.tsx b/web/src/components/CreateShortcutDialog.tsx index d24a6018..cadb0952 100644 --- a/web/src/components/CreateShortcutDialog.tsx +++ b/web/src/components/CreateShortcutDialog.tsx @@ -48,9 +48,16 @@ const CreateShortcutDialog: React.FC = (props: Props) => { try { if (shortcutId) { - await shortcutService.updateShortcut(shortcutId, title, JSON.stringify(filters)); + await shortcutService.patchShortcut({ + id: shortcutId, + title, + payload: JSON.stringify(filters), + }); } else { - await shortcutService.createShortcut(title, JSON.stringify(filters)); + await shortcutService.createShortcut({ + title, + payload: JSON.stringify(filters), + }); } } catch (error: any) { toastHelper.error(error.message); diff --git a/web/src/components/DeletedMemo.tsx b/web/src/components/DeletedMemo.tsx index 957c3805..9877d5f7 100644 --- a/web/src/components/DeletedMemo.tsx +++ b/web/src/components/DeletedMemo.tsx @@ -38,7 +38,10 @@ const DeletedMemo: React.FC = (props: Props) => { const handleRestoreMemoClick = async () => { try { - await memoService.restoreMemoById(memo.id); + await memoService.patchMemo({ + id: memo.id, + rowStatus: "NORMAL", + }); handleDeletedMemoAction(memo.id); toastHelper.info("Restored successfully"); } catch (error: any) { diff --git a/web/src/components/Memo.tsx b/web/src/components/Memo.tsx index d584b010..41b1348e 100644 --- a/web/src/components/Memo.tsx +++ b/web/src/components/Memo.tsx @@ -33,16 +33,8 @@ const Memo: React.FC = (props: Props) => { try { if (memo.pinned) { await memoService.unpinMemo(memo.id); - memoService.editMemo({ - ...memo, - pinned: false, - }); } else { await memoService.pinMemo(memo.id); - memoService.editMemo({ - ...memo, - pinned: true, - }); } } catch (error) { // do nth @@ -50,23 +42,26 @@ const Memo: React.FC = (props: Props) => { }; const handleMarkMemoClick = () => { - editorStateService.setMarkMemo(memo.id); + editorStateService.setMarkMemoWithId(memo.id); }; const handleEditMemoClick = () => { - editorStateService.setEditMemo(memo.id); + editorStateService.setEditMemoWithId(memo.id); }; const handleDeleteMemoClick = async () => { if (showConfirmDeleteBtn) { try { - await memoService.archiveMemoById(memo.id); + await memoService.patchMemo({ + id: memo.id, + rowStatus: "ARCHIVED", + }); } catch (error: any) { toastHelper.error(error.message); } if (editorStateService.getState().editMemoId === memo.id) { - editorStateService.setEditMemo(UNKNOWN_ID); + editorStateService.clearEditMemo(); } } else { toggleConfirmDeleteBtn(); diff --git a/web/src/components/MemoCardDialog.tsx b/web/src/components/MemoCardDialog.tsx index 0a4a5e14..5293e0eb 100644 --- a/web/src/components/MemoCardDialog.tsx +++ b/web/src/components/MemoCardDialog.tsx @@ -96,7 +96,7 @@ const MemoCardDialog: React.FC = (props: Props) => { const handleEditMemoBtnClick = useCallback(() => { props.destroy(); - editorStateService.setEditMemo(memo.id); + editorStateService.setEditMemoWithId(memo.id); }, [memo.id]); return ( diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx index a551950e..463e0f9a 100644 --- a/web/src/components/MemoEditor.tsx +++ b/web/src/components/MemoEditor.tsx @@ -57,7 +57,7 @@ const MemoEditor: React.FC = () => { const editorCurrentValue = editorRef.current?.getContent(); const memoLinkText = `${editorCurrentValue ? "\n" : ""}Mark: [@MEMO](${editorState.markMemoId})`; editorRef.current?.insertText(memoLinkText); - editorStateService.setMarkMemo(UNKNOWN_ID); + editorStateService.clearMarkMemo(); } if ( @@ -155,14 +155,16 @@ const MemoEditor: React.FC = () => { const prevMemo = memoService.getMemoById(editMemoId ?? UNKNOWN_ID); if (prevMemo && prevMemo.content !== content) { - const editedMemo = await memoService.updateMemo(prevMemo.id, content); - editedMemo.createdTs = Date.now(); - memoService.editMemo(editedMemo); + await memoService.patchMemo({ + id: prevMemo.id, + content, + }); } - editorStateService.setEditMemo(UNKNOWN_ID); + editorStateService.clearEditMemo(); } else { - const newMemo = await memoService.createMemo(content); - memoService.pushMemo(newMemo); + await memoService.createMemo({ + content, + }); locationService.clearQuery(); } } catch (error: any) { @@ -173,7 +175,7 @@ const MemoEditor: React.FC = () => { }, []); const handleCancelBtnClick = useCallback(() => { - editorStateService.setEditMemo(UNKNOWN_ID); + editorStateService.clearEditMemo(); editorRef.current?.setContent(""); setEditorContentCache(""); }, []); diff --git a/web/src/components/MemoFilter.tsx b/web/src/components/MemoFilter.tsx index ac7cd29b..f9d765b1 100644 --- a/web/src/components/MemoFilter.tsx +++ b/web/src/components/MemoFilter.tsx @@ -28,7 +28,7 @@ const MemoFilter: React.FC = () => {
{ - locationService.setTagQuery(""); + locationService.setTagQuery(undefined); }} > 🏷️ {tagQuery} @@ -45,7 +45,7 @@ const MemoFilter: React.FC = () => {
{ - locationService.setFromAndToQuery(0, 0); + locationService.setFromAndToQuery(); }} > 🗓️ {utils.getDateString(duration.from)} to {utils.getDateString(duration.to)} diff --git a/web/src/components/MemoTrashDialog.tsx b/web/src/components/MemoTrashDialog.tsx index 5a2c0331..dab6754f 100644 --- a/web/src/components/MemoTrashDialog.tsx +++ b/web/src/components/MemoTrashDialog.tsx @@ -18,9 +18,7 @@ const MemoTrashDialog: React.FC = (props: Props) => { memoService .fetchDeletedMemos() .then((result) => { - if (result !== false) { - setDeletedMemos(result); - } + setDeletedMemos(result); }) .catch((error) => { toastHelper.error("Failed to fetch deleted memos: ", error); diff --git a/web/src/components/MenuBtnsPopup.tsx b/web/src/components/MenuBtnsPopup.tsx index 0b612f4d..b13285ff 100644 --- a/web/src/components/MenuBtnsPopup.tsx +++ b/web/src/components/MenuBtnsPopup.tsx @@ -32,7 +32,9 @@ const MenuBtnsPopup: React.FC = (props: Props) => { }; const handleSignOutBtnClick = async () => { - await userService.doSignOut(); + userService.doSignOut().catch(() => { + // do nth + }); locationService.replaceHistory("/signin"); window.location.reload(); }; diff --git a/web/src/components/Settings/MyAccountSection.tsx b/web/src/components/Settings/MyAccountSection.tsx index ea3d4f43..bd131810 100644 --- a/web/src/components/Settings/MyAccountSection.tsx +++ b/web/src/components/Settings/MyAccountSection.tsx @@ -39,8 +39,9 @@ const MyAccountSection: React.FC = () => { } try { - await userService.updateUsername(username); - await userService.doSignIn(); + await userService.patchUser({ + name: username, + }); toastHelper.info("Username changed"); } catch (error: any) { toastHelper.error(error.message); diff --git a/web/src/components/Settings/PreferencesSection.tsx b/web/src/components/Settings/PreferencesSection.tsx index d9b5c7e2..93903f25 100644 --- a/web/src/components/Settings/PreferencesSection.tsx +++ b/web/src/components/Settings/PreferencesSection.tsx @@ -45,7 +45,11 @@ const PreferencesSection: React.FC = () => { const createdTs = (memo as any).createdAt || memo.createdTs || Date.now(); try { - await memoService.importMemo(content, createdTs); + const memoCreate = { + content, + createdTs: Math.floor(utils.getTimeStampByDate(createdTs) / 1000), + }; + await memoService.createMemo(memoCreate); succeedAmount++; } catch (error) { // do nth diff --git a/web/src/components/ShortcutList.tsx b/web/src/components/ShortcutList.tsx index def1555d..94cc8419 100644 --- a/web/src/components/ShortcutList.tsx +++ b/web/src/components/ShortcutList.tsx @@ -96,19 +96,11 @@ const ShortcutContainer: React.FC = (props: ShortcutCont event.stopPropagation(); try { - if (shortcut.rowStatus === "ARCHIVED") { - await shortcutService.unpinShortcut(shortcut.id); - shortcutService.editShortcut({ - ...shortcut, - rowStatus: "NORMAL", - }); - } else { - await shortcutService.pinShortcut(shortcut.id); - shortcutService.editShortcut({ - ...shortcut, - rowStatus: "ARCHIVED", - }); - } + const shortcutPatch: ShortcutPatch = { + id: shortcut.id, + rowStatus: shortcut.rowStatus === "ARCHIVED" ? "NORMAL" : "ARCHIVED", + }; + await shortcutService.patchShortcut(shortcutPatch); } catch (error) { // do nth } diff --git a/web/src/components/UsageHeatMap.tsx b/web/src/components/UsageHeatMap.tsx index 972a611c..ced02b0f 100644 --- a/web/src/components/UsageHeatMap.tsx +++ b/web/src/components/UsageHeatMap.tsx @@ -72,7 +72,7 @@ const UsageHeatMap: React.FC = () => { const handleUsageStatItemClick = useCallback((item: DailyUsageStat) => { if (locationService.getState().query?.duration?.from === item.timestamp) { - locationService.setFromAndToQuery(0, 0); + locationService.setFromAndToQuery(); setCurrentStat(null); } else if (item.count > 0) { if (!["/"].includes(locationService.getState().pathname)) { diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts index 7aff8de9..082f6c85 100644 --- a/web/src/helpers/api.ts +++ b/web/src/helpers/api.ts @@ -159,26 +159,6 @@ namespace api { }); } - export function archiveMemo(memoId: MemoId) { - return request({ - method: "PATCH", - url: `/api/memo/${memoId}`, - data: { - rowStatus: "ARCHIVED", - }, - }); - } - - export function restoreMemo(memoId: MemoId) { - return request({ - method: "PATCH", - url: `/api/memo/${memoId}`, - data: { - rowStatus: "NORMAL", - }, - }); - } - export function deleteMemo(memoId: MemoId) { return request({ method: "DELETE", @@ -193,25 +173,19 @@ namespace api { }); } - export function createShortcut(title: string, payload: string) { + export function createShortcut(shortcutCreate: ShortcutCreate) { return request({ method: "POST", url: "/api/shortcut", - data: { - title, - payload, - }, + data: shortcutCreate, }); } - export function updateShortcut(shortcutId: ShortcutId, title: string, payload: string) { + export function patchShortcut(shortcutPatch: ShortcutPatch) { return request({ method: "PATCH", - url: `/api/shortcut/${shortcutId}`, - data: { - title, - payload, - }, + url: `/api/shortcut/${shortcutPatch.id}`, + data: shortcutPatch, }); } @@ -222,26 +196,6 @@ namespace api { }); } - export function pinShortcut(shortcutId: ShortcutId) { - return request({ - method: "PATCH", - url: `/api/shortcut/${shortcutId}`, - data: { - rowStatus: "ARCHIVED", - }, - }); - } - - export function unpinShortcut(shortcutId: ShortcutId) { - return request({ - method: "PATCH", - url: `/api/shortcut/${shortcutId}`, - data: { - rowStatus: "NORMAL", - }, - }); - } - export function uploadFile(formData: FormData) { return request({ method: "POST", diff --git a/web/src/less/search-bar.less b/web/src/less/search-bar.less index 3847e0aa..62321618 100644 --- a/web/src/less/search-bar.less +++ b/web/src/less/search-bar.less @@ -5,7 +5,7 @@ > .search-bar-inputer { .flex(row, flex-start, center); - @apply w-full py-2 px-4 rounded-lg flex flex-row justify-start items-center bg-gray-200; + @apply w-full py-2 px-4 rounded-lg flex flex-row justify-start items-center bg-zinc-200; > .icon-img { @apply mr-2 w-4 h-auto opacity-80; diff --git a/web/src/services/editorStateService.ts b/web/src/services/editorStateService.ts index 9960da67..0e0b0716 100644 --- a/web/src/services/editorStateService.ts +++ b/web/src/services/editorStateService.ts @@ -6,13 +6,21 @@ const editorStateService = { return store.getState().editor; }, - setEditMemo: (editMemoId: MemoId) => { + setEditMemoWithId: (editMemoId: MemoId) => { store.dispatch(setEditMemoId(editMemoId)); }, - setMarkMemo: (markMemoId: MemoId) => { + clearEditMemo: () => { + store.dispatch(setEditMemoId()); + }, + + setMarkMemoWithId: (markMemoId: MemoId) => { store.dispatch(setMarkMemoId(markMemoId)); }, + + clearMarkMemo: () => { + store.dispatch(setMarkMemoId()); + }, }; export default editorStateService; diff --git a/web/src/services/locationService.ts b/web/src/services/locationService.ts index 27f087c2..882b2e05 100644 --- a/web/src/services/locationService.ts +++ b/web/src/services/locationService.ts @@ -1,6 +1,6 @@ import utils from "../helpers/utils"; import store from "../store"; -import { setQuery, setPathname } from "../store/modules/location"; +import { setQuery, setPathname, Query } from "../store/modules/location"; const updateLocationUrl = (method: "replace" | "push" = "replace") => { const { query, pathname, hash } = store.getState().location; @@ -23,36 +23,34 @@ const locationService = { return store.getState().location; }, - clearQuery: () => { - store.dispatch(setQuery({})); - updateLocationUrl(); - }, - - setQuery: (query: Query) => { - store.dispatch(setQuery(query)); - updateLocationUrl(); - }, - - setPathname: (pathname: AppRouter) => { + setPathname: (pathname: string) => { store.dispatch(setPathname(pathname)); updateLocationUrl(); }, - pushHistory: (pathname: AppRouter) => { + pushHistory: (pathname: string) => { store.dispatch(setPathname(pathname)); updateLocationUrl("push"); }, - replaceHistory: (pathname: AppRouter) => { + replaceHistory: (pathname: string) => { store.dispatch(setPathname(pathname)); updateLocationUrl("replace"); }, + setQuery: (query: Query) => { + store.dispatch(setQuery(query)); + updateLocationUrl(); + }, + + clearQuery: () => { + store.dispatch(setQuery({})); + updateLocationUrl(); + }, + setMemoTypeQuery: (type?: MemoSpecType) => { - const { query } = store.getState().location; store.dispatch( setQuery({ - ...query, type: type, }) ); @@ -60,10 +58,8 @@ const locationService = { }, setMemoShortcut: (shortcutId?: ShortcutId) => { - const { query } = store.getState().location; store.dispatch( setQuery({ - ...query, shortcutId: shortcutId, }) ); @@ -71,10 +67,8 @@ const locationService = { }, setTextQuery: (text?: string) => { - const { query } = store.getState().location; store.dispatch( setQuery({ - ...query, text: text, }) ); @@ -82,34 +76,30 @@ const locationService = { }, setTagQuery: (tag?: string) => { - const { query } = store.getState().location; store.dispatch( setQuery({ - ...query, tag: tag, }) ); updateLocationUrl(); }, - setFromAndToQuery: (from: number, to: number) => { - const { query } = store.getState().location; + setFromAndToQuery: (from?: number, to?: number) => { + let duration = undefined; + if (from && to && from < to) { + duration = { + from, + to, + }; + } + store.dispatch( setQuery({ - ...query, - duration: { from, to }, + duration, }) ); updateLocationUrl(); }, - - getValidPathname: (pathname: string): AppRouter => { - if (["/", "/signin"].includes(pathname)) { - return pathname as AppRouter; - } else { - return "/"; - } - }, }; export default locationService; diff --git a/web/src/services/memoService.ts b/web/src/services/memoService.ts index 4819c21b..73729d06 100644 --- a/web/src/services/memoService.ts +++ b/web/src/services/memoService.ts @@ -1,9 +1,7 @@ import api from "../helpers/api"; import { TAG_REG } from "../helpers/consts"; -import utils from "../helpers/utils"; -import { patchMemo, setMemos, setTags } from "../store/modules/memo"; +import { createMemo, patchMemo, setMemos, setTags } from "../store/modules/memo"; import store from "../store"; -import userService from "./userService"; const convertResponseModelMemo = (memo: Memo): Memo => { return { @@ -19,36 +17,24 @@ const memoService = { }, fetchAllMemos: async () => { - if (!userService.getState().user) { - return false; - } - const data = await api.getMyMemos(); - const memos: Memo[] = data.filter((m) => m.rowStatus !== "ARCHIVED").map((m) => convertResponseModelMemo(m)); + const memos = data.filter((m) => m.rowStatus !== "ARCHIVED").map((m) => convertResponseModelMemo(m)); store.dispatch(setMemos(memos)); return memos; }, fetchDeletedMemos: async () => { - if (!userService.getState().user) { - return false; - } - const data = await api.getMyArchivedMemos(); - const deletedMemos: Memo[] = data.map((m) => { + const deletedMemos = data.map((m) => { return convertResponseModelMemo(m); }); return deletedMemos; }, - pushMemo: (memo: Memo) => { - store.dispatch(setMemos(memoService.getState().memos.concat(memo))); - }, - - getMemoById: (id: MemoId) => { + getMemoById: (memoId: MemoId) => { for (const m of memoService.getState().memos) { - if (m.id === id) { + if (m.id === memoId) { return m; } } @@ -56,35 +42,6 @@ const memoService = { return null; }, - archiveMemoById: async (id: MemoId) => { - const memo = memoService.getMemoById(id); - if (!memo) { - return; - } - - await api.archiveMemo(id); - store.dispatch( - patchMemo({ - ...memo, - rowStatus: "ARCHIVED", - }) - ); - }, - - restoreMemoById: async (id: MemoId) => { - await api.restoreMemo(id); - memoService.clearMemos(); - memoService.fetchAllMemos(); - }, - - deleteMemoById: async (id: MemoId) => { - await api.deleteMemo(id); - }, - - editMemo: (memo: Memo) => { - store.dispatch(patchMemo(memo)); - }, - updateTagsState: () => { const { memos } = memoService.getState(); const tagsSet = new Set(); @@ -97,43 +54,46 @@ const memoService = { store.dispatch(setTags(Array.from(tagsSet).filter((t) => Boolean(t)))); }, - clearMemos: () => { - store.dispatch(setMemos([])); - }, - getLinkedMemos: async (memoId: MemoId): Promise => { const { memos } = memoService.getState(); return memos.filter((m) => m.content.includes(`${memoId}`)); }, - createMemo: async (content: string): Promise => { - const memo = await api.createMemo({ - content, - }); - return convertResponseModelMemo(memo); + createMemo: async (memoCreate: MemoCreate) => { + const data = await api.createMemo(memoCreate); + const memo = convertResponseModelMemo(data); + store.dispatch(createMemo(memo)); }, - updateMemo: async (memoId: MemoId, content: string): Promise => { - const memo = await api.patchMemo({ - id: memoId, - content, - }); - return convertResponseModelMemo(memo); + patchMemo: async (memoPatch: MemoPatch): Promise => { + 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, + }) + ); }, - importMemo: async (content: string, createdTs: TimeStamp) => { - await api.createMemo({ - content, - createdTs: Math.floor(utils.getTimeStampByDate(createdTs) / 1000), - }); + deleteMemoById: async (memoId: MemoId) => { + await api.deleteMemo(memoId); }, }; diff --git a/web/src/services/shortcutService.ts b/web/src/services/shortcutService.ts index 3b9685cc..26a87e46 100644 --- a/web/src/services/shortcutService.ts +++ b/web/src/services/shortcutService.ts @@ -1,8 +1,6 @@ -import userService from "./userService"; import api from "../helpers/api"; -import { UNKNOWN_ID } from "../helpers/consts"; import store from "../store/"; -import { deleteShortcut, patchShortcut, setShortcuts } from "../store/modules/shortcut"; +import { createShortcut, deleteShortcut, patchShortcut, setShortcuts } from "../store/modules/shortcut"; const convertResponseModelShortcut = (shortcut: Shortcut): Shortcut => { return { @@ -18,21 +16,12 @@ const shortcutService = { }, getMyAllShortcuts: async () => { - if (!userService.getState().user) { - return false; - } - - const data = await api.getMyShortcuts(); - const shortcuts = data.map((s) => convertResponseModelShortcut(s)); + const rawData = await api.getMyShortcuts(); + const shortcuts = rawData.map((s) => convertResponseModelShortcut(s)); store.dispatch(setShortcuts(shortcuts)); - return shortcuts; }, getShortcutById: (id: ShortcutId) => { - if (id === UNKNOWN_ID) { - return null; - } - for (const s of shortcutService.getState().shortcuts) { if (s.id === id) { return s; @@ -42,11 +31,15 @@ const shortcutService = { return null; }, - pushShortcut: (shortcut: Shortcut) => { - store.dispatch(setShortcuts(shortcutService.getState().shortcuts.concat(shortcut))); + createShortcut: async (shortcutCreate: ShortcutCreate) => { + const data = await api.createShortcut(shortcutCreate); + const shortcut = convertResponseModelShortcut(data); + store.dispatch(createShortcut(shortcut)); }, - editShortcut: (shortcut: Shortcut) => { + patchShortcut: async (shortcutPatch: ShortcutPatch) => { + const data = await api.patchShortcut(shortcutPatch); + const shortcut = convertResponseModelShortcut(data); store.dispatch(patchShortcut(shortcut)); }, @@ -54,24 +47,6 @@ const shortcutService = { await api.deleteShortcutById(shortcutId); store.dispatch(deleteShortcut(shortcutId)); }, - - createShortcut: async (title: string, payload: string) => { - const data = await api.createShortcut(title, payload); - shortcutService.pushShortcut(convertResponseModelShortcut(data)); - }, - - updateShortcut: async (shortcutId: ShortcutId, title: string, payload: string) => { - const data = await api.updateShortcut(shortcutId, title, payload); - store.dispatch(patchShortcut(convertResponseModelShortcut(data))); - }, - - pinShortcut: async (shortcutId: ShortcutId) => { - await api.pinShortcut(shortcutId); - }, - - unpinShortcut: async (shortcutId: ShortcutId) => { - await api.unpinShortcut(shortcutId); - }, }; export default shortcutService; diff --git a/web/src/services/userService.ts b/web/src/services/userService.ts index cde54af9..acfa5378 100644 --- a/web/src/services/userService.ts +++ b/web/src/services/userService.ts @@ -1,6 +1,6 @@ import api from "../helpers/api"; -import { signin, signout } from "../store/modules/user"; import store from "../store"; +import { setUser, patchUser } from "../store/modules/user"; const convertResponseModelUser = (user: User): User => { return { @@ -18,7 +18,7 @@ const userService = { doSignIn: async () => { const user = await api.getUser(); if (user) { - store.dispatch(signin(convertResponseModelUser(user))); + store.dispatch(setUser(convertResponseModelUser(user))); } else { userService.doSignOut(); } @@ -26,29 +26,14 @@ const userService = { }, doSignOut: async () => { - store.dispatch(signout); - api.signout().catch(() => { - // do nth - }); + store.dispatch(setUser()); + await api.signout(); }, - updateUsername: async (name: string): Promise => { - await api.patchUser({ - name, - }); - }, - - updatePassword: async (password: string): Promise => { - await api.patchUser({ - password, - }); - }, - - resetOpenId: async (): Promise => { - const user = await api.patchUser({ - resetOpenId: true, - }); - return user.openId; + patchUser: async (userPatch: UserPatch): Promise => { + const data = await api.patchUser(userPatch); + const user = convertResponseModelUser(data); + store.dispatch(patchUser(user)); }, }; diff --git a/web/src/store/modules/editor.ts b/web/src/store/modules/editor.ts index b5e6bb20..2c989f8d 100644 --- a/web/src/store/modules/editor.ts +++ b/web/src/store/modules/editor.ts @@ -10,10 +10,16 @@ const editorSlice = createSlice({ initialState: {} as State, reducers: { setMarkMemoId: (state, action: PayloadAction>) => { - state.markMemoId = action.payload; + return { + ...state, + markMemoId: action.payload, + }; }, setEditMemoId: (state, action: PayloadAction>) => { - state.editMemoId = action.payload; + return { + ...state, + editMemoId: action.payload, + }; }, }, }); diff --git a/web/src/store/modules/location.ts b/web/src/store/modules/location.ts index a5037b81..21239156 100644 --- a/web/src/store/modules/location.ts +++ b/web/src/store/modules/location.ts @@ -1,14 +1,27 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +interface Duration { + from: number; + to: number; +} + +export interface Query { + tag?: string; + duration?: Duration; + type?: MemoSpecType; + text?: string; + shortcutId?: ShortcutId; +} + interface State { - pathname: AppRouter; + pathname: string; hash: string; - query?: Query; + query: Query; } -const getValidPathname = (pathname: string): AppRouter => { +const getValidPathname = (pathname: string): string => { if (["/", "/signin"].includes(pathname)) { - return pathname as AppRouter; + return pathname; } else { return "/"; } @@ -20,6 +33,7 @@ const getStateFromLocation = () => { const state: State = { pathname: getValidPathname(pathname), hash: hash, + query: {}, }; if (search !== "") { @@ -48,11 +62,20 @@ const locationSlice = createSlice({ updateStateWithLocation: () => { return getStateFromLocation(); }, - setPathname: (state, action: PayloadAction) => { - state.pathname = action.payload; + setPathname: (state, action: PayloadAction) => { + return { + ...state, + pathname: action.payload, + }; }, setQuery: (state, action: PayloadAction>) => { - state.query = action.payload; + return { + ...state, + query: { + ...state.query, + ...action.payload, + }, + }; }, }, }); diff --git a/web/src/store/modules/memo.ts b/web/src/store/modules/memo.ts index 0dc66280..b6650e72 100644 --- a/web/src/store/modules/memo.ts +++ b/web/src/store/modules/memo.ts @@ -33,12 +33,9 @@ const memoSlice = createSlice({ } }); }, - deleteMemo: (state, action: PayloadAction) => { - state.memos = [...state.memos].filter((memo) => memo.id !== action.payload); - }, }, }); -export const { setMemos, setTags, createMemo, patchMemo, deleteMemo } = memoSlice.actions; +export const { setMemos, setTags, createMemo, patchMemo } = memoSlice.actions; export default memoSlice.reducer; diff --git a/web/src/store/modules/user.ts b/web/src/store/modules/user.ts index 53fa3a6a..9cb759c6 100644 --- a/web/src/store/modules/user.ts +++ b/web/src/store/modules/user.ts @@ -8,18 +8,12 @@ const userSlice = createSlice({ name: "user", initialState: {} as State, reducers: { - signin: (state, action: PayloadAction) => { + setUser: (state, action: PayloadAction) => { return { ...state, user: action.payload, }; }, - signout: (state) => { - return { - ...state, - user: undefined, - }; - }, patchUser: (state, action: PayloadAction>) => { state.user = { ...state.user, @@ -29,6 +23,6 @@ const userSlice = createSlice({ }, }); -export const { signin, signout, patchUser } = userSlice.actions; +export const { setUser, patchUser } = userSlice.actions; export default userSlice.reducer; diff --git a/web/src/types/location.d.ts b/web/src/types/location.d.ts index 10eb1517..e69de29b 100644 --- a/web/src/types/location.d.ts +++ b/web/src/types/location.d.ts @@ -1,20 +0,0 @@ -interface Duration { - from: number; - to: number; -} - -interface Query { - tag?: string; - duration?: Duration; - type?: MemoSpecType; - text?: string; - shortcutId?: ShortcutId; -} - -type AppRouter = "/" | "/signin"; - -interface AppLocation { - pathname: AppRouter; - hash: string; - query: Query; -} diff --git a/web/src/types/modules/shortcut.d.ts b/web/src/types/modules/shortcut.d.ts index bfaac285..10f66685 100644 --- a/web/src/types/modules/shortcut.d.ts +++ b/web/src/types/modules/shortcut.d.ts @@ -10,3 +10,15 @@ interface Shortcut { title: string; payload: string; } + +interface ShortcutCreate { + title: string; + payload: string; +} + +interface ShortcutPatch { + id: ShortcutId; + title?: string; + payload?: string; + rowStatus?: RowStatus; +}