From cbdae2431473d4b080ad56ad9eb73e3b160dbc6b Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 20 Dec 2023 23:23:26 +0800 Subject: [PATCH] chore: update archived page --- web/src/components/ArchivedMemo.tsx | 71 ----------------------------- web/src/pages/Archived.tsx | 63 ++++++++++++++++++++++++- web/src/store/v1/memo.ts | 26 +++++++++++ 3 files changed, 87 insertions(+), 73 deletions(-) delete mode 100644 web/src/components/ArchivedMemo.tsx diff --git a/web/src/components/ArchivedMemo.tsx b/web/src/components/ArchivedMemo.tsx deleted file mode 100644 index 4c96161f..00000000 --- a/web/src/components/ArchivedMemo.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import { Tooltip } from "@mui/joy"; -import { toast } from "react-hot-toast"; -import { getDateTimeString } from "@/helpers/datetime"; -import { useMemoStore } from "@/store/module"; -import { Memo } from "@/types/proto/api/v2/memo_service"; -import { useTranslate } from "@/utils/i18n"; -import { showCommonDialog } from "./Dialog/CommonDialog"; -import Icon from "./Icon"; -import MemoContentV1 from "./MemoContentV1"; -import "@/less/memo.less"; - -interface Props { - memo: Memo; -} - -const ArchivedMemo: React.FC = (props: Props) => { - const { memo } = props; - const t = useTranslate(); - const memoStore = useMemoStore(); - - const handleDeleteMemoClick = async () => { - showCommonDialog({ - title: t("memo.delete-memo"), - content: t("memo.delete-confirm"), - style: "danger", - dialogName: "delete-memo-dialog", - onConfirm: async () => { - await memoStore.deleteMemoById(memo.id); - }, - }); - }; - - const handleRestoreMemoClick = async () => { - try { - await memoStore.patchMemo({ - id: memo.id, - rowStatus: "NORMAL", - }); - await memoStore.fetchMemos(); - toast(t("message.restored-successfully")); - } catch (error: any) { - console.error(error); - toast.error(error.response.data.message); - } - }; - - return ( -
-
-
- {getDateTimeString(memo.displayTime)} -
-
- - - - - - -
-
- -
- ); -}; - -export default ArchivedMemo; diff --git a/web/src/pages/Archived.tsx b/web/src/pages/Archived.tsx index cdf022af..b3193704 100644 --- a/web/src/pages/Archived.tsx +++ b/web/src/pages/Archived.tsx @@ -1,16 +1,23 @@ +import { Tooltip } from "@mui/joy"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; -import ArchivedMemo from "@/components/ArchivedMemo"; +import { showCommonDialog } from "@/components/Dialog/CommonDialog"; import Empty from "@/components/Empty"; +import Icon from "@/components/Icon"; +import MemoContentV1 from "@/components/MemoContentV1"; import MobileHeader from "@/components/MobileHeader"; import { memoServiceClient } from "@/grpcweb"; +import { getDateTimeString } from "@/helpers/datetime"; import useLoading from "@/hooks/useLoading"; +import { useMemoV1Store } from "@/store/v1"; +import { RowStatus } from "@/types/proto/api/v2/common"; import { Memo } from "@/types/proto/api/v2/memo_service"; import { useTranslate } from "@/utils/i18n"; const Archived = () => { const t = useTranslate(); const loadingState = useLoading(); + const memoStore = useMemoV1Store(); const [archivedMemos, setArchivedMemos] = useState([]); useEffect(() => { @@ -30,6 +37,36 @@ const Archived = () => { }); }, []); + const handleDeleteMemoClick = async (memo: Memo) => { + showCommonDialog({ + title: t("memo.delete-memo"), + content: t("memo.delete-confirm"), + style: "danger", + dialogName: "delete-memo-dialog", + onConfirm: async () => { + await memoStore.deleteMemo(memo); + setArchivedMemos((prev) => prev.filter((m) => m.id !== memo.id)); + }, + }); + }; + + const handleRestoreMemoClick = async (memo: Memo) => { + try { + await memoStore.updateMemo( + { + id: memo.id, + rowStatus: RowStatus.ACTIVE, + }, + ["row_status"] + ); + setArchivedMemos((prev) => prev.filter((m) => m.id !== memo.id)); + toast(t("message.restored-successfully")); + } catch (error: any) { + console.error(error); + toast.error(error.response.data.message); + } + }; + return (
@@ -46,7 +83,29 @@ const Archived = () => { ) : (
{archivedMemos.map((memo) => ( - +
+
+
+ {getDateTimeString(memo.updateTime)} +
+
+ + + + + + +
+
+ +
))}
)} diff --git a/web/src/store/v1/memo.ts b/web/src/store/v1/memo.ts index 5c837ca9..35eeea28 100644 --- a/web/src/store/v1/memo.ts +++ b/web/src/store/v1/memo.ts @@ -29,5 +29,31 @@ export const useMemoV1Store = create( getMemoById: (id: number) => { return get().memoById.get(id); }, + updateMemo: async (update: Partial, updateMask: string[]) => { + const { memo } = await memoServiceClient.updateMemo({ + id: update.id!, + memo: update, + updateMask, + }); + if (!memo) { + throw new Error("Memo not found"); + } + + set((state) => { + state.memoById.set(memo.id, memo); + return state; + }); + + return memo; + }, + deleteMemo: async (memo: Memo) => { + await memoServiceClient.deleteMemo({ + id: memo.id, + }); + set((state) => { + state.memoById.delete(memo.id); + return state; + }); + }, })) );