Skip to content

Commit

Permalink
style(search): add message when search is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
rockbenben committed Dec 28, 2024
1 parent 6801134 commit d897e62
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/pages/community-prompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
Expand All @@ -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}`,
});
}
};

Expand All @@ -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}`,
});
}
};

Expand Down Expand Up @@ -186,6 +209,7 @@ const CommunityPrompts = () => {
hashed: false,
algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
}}>
{contextHolder}
<Space wrap style={{ marginBottom: "20px" }}>
<Link to="/">
<HomeOutlined /> <Translate id="link.home">返回首页</Translate>
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit d897e62

Please sign in to comment.