From d897e628d21799bc60e112885d94bb18835f475a Mon Sep 17 00:00:00 2001 From: rockbenben Date: Sat, 28 Dec 2024 13:35:31 +0800 Subject: [PATCH] style(search): add message when search is empty --- src/pages/community-prompts.tsx | 49 +++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/pages/community-prompts.tsx b/src/pages/community-prompts.tsx index 68cf18eba..250d6bec2 100644 --- a/src/pages/community-prompts.tsx +++ b/src/pages/community-prompts.tsx @@ -36,6 +36,7 @@ const DESCRIPTION = translate({ const CommunityPrompts = () => { const { userAuth } = useContext(AuthContext); + const [messageApi, contextHolder] = message.useMessage(); const [open, setOpen] = useState(false); const [userprompts, setUserPrompts] = useState(placeholderData); const [currentPage, setCurrentPage] = useState(1); @@ -64,6 +65,13 @@ const CommunityPrompts = () => { if (result && result[0].length > 0) { setUserPrompts(result[0]); setTotal(result[1].data.meta.pagination.total); + } else if (result && result[0].length === 0) { + messageApi.open({ + type: "warning", + content: "No data found.", + }); + setUserPrompts([]); + setTotal(0); } else { console.log("No data returned from the server"); } @@ -75,7 +83,10 @@ const CommunityPrompts = () => { const onSearch = (value) => { if (!userAuth) { setOpen(true); - message.warning("Please log in to search."); + messageApi.open({ + type: "warning", + content: "Please log in to search.", + }); return; } setSearchTerm(value); @@ -85,11 +96,17 @@ const CommunityPrompts = () => { const vote = async (promptId, action) => { try { await voteOnUserPrompt(promptId, action); - message.success(`Successfully ${action}d!`); + messageApi.open({ + type: "success", + content: `Successfully ${action}d!`, + }); const updateVotedIds = action === "upvote" ? setVotedUpPromptIds : setVotedDownPromptIds; updateVotedIds((prevIds) => [...prevIds, promptId]); } catch (err) { - message.error(`Error: ${err}`); + messageApi.open({ + type: "error", + content: `Failed to ${action}. Error: ${err}`, + }); } }; @@ -108,12 +125,18 @@ const CommunityPrompts = () => { if (!userLoves.includes(promptId)) { userLoves.push(promptId); - message.success("Added to favorites successfully!"); + messageApi.open({ + type: "success", + content: "Added to favorites successfully!", + }); } } await updateFavorite(favoriteId, userLoves, true); } catch (err) { - message.error(`Error: ${err}`); + messageApi.open({ + type: "error", + content: `Failed to add to favorites. Error: ${err}`, + }); } }; @@ -186,6 +209,7 @@ const CommunityPrompts = () => { hashed: false, algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm, }}> + {contextHolder} 返回首页 @@ -244,7 +268,10 @@ const CommunityPrompts = () => { type="default" onClick={() => { if (!userAuth) { - message.error("Please log in to vote and bookmark."); + messageApi.open({ + type: "warning", + content: "Please log in to bookmark.", + }); return; } vote(UserPrompt.id, "upvote"); @@ -260,7 +287,10 @@ const CommunityPrompts = () => { type="default" onClick={() => { if (!userAuth) { - message.error("Please log in to vote and bookmark."); + messageApi.open({ + type: "warning", + content: "Please log in to vote.", + }); return; } vote(UserPrompt.id, "upvote"); @@ -274,7 +304,10 @@ const CommunityPrompts = () => { type="default" onClick={() => { if (!userAuth) { - message.error("Please log in to vote and bookmark."); + messageApi.open({ + type: "warning", + content: "Please log in to vote.", + }); return; } vote(UserPrompt.id, "downvote");