Files
LivePoll/frontend/src/pages/Bookmark.jsx

97 lines
2.9 KiB
React
Raw Normal View History

2024-11-05 23:50:55 +05:30
// BookmarkPage.js
import React from "react";
2024-11-13 09:32:20 +05:30
import { getUserBookmarks } from "../services/getUserBookmarks";
import { useQuery, useQueryClient } from "react-query";
import ErrorFallback from "../components/Errors/ErrorFallback";
import useBookmark from "../hooks/useBookmark";
import { useNavigate } from "react-router-dom";
2024-11-13 18:37:05 +05:30
import { formatDataByDate } from "../utils/util";
2024-11-05 23:50:55 +05:30
function Bookmark() {
2024-11-13 09:32:20 +05:30
const { handleBookmark } = useBookmark();
const navigator = useNavigate();
const queryClient = useQueryClient();
const { data, isLoading, isError, refetch, isSuccess } = useQuery(
["bookmarks"],
getUserBookmarks,
{
cacheTime: 1000 * 60 * 5, // 5 minutes
2025-10-17 10:34:20 +08:00
staleTime: 0, // 10 minutes
2024-11-13 09:32:20 +05:30
}
);
const handleViewPollClick = (pollId) => {
navigator(`/view/${pollId}`);
};
const handleRemoveBookmark = async (bookmarkId) => {
queryClient.setQueryData(["bookmarks"], (oldData) => {
return {
...oldData,
data: oldData.data.filter((bookmark) => bookmark._id !== bookmarkId),
};
});
await handleBookmark(bookmarkId);
};
2024-11-13 18:37:05 +05:30
console.log(data);
2024-11-13 10:59:04 +05:30
2024-11-05 23:50:55 +05:30
return (
<div className="p-6 bg-base-200 h-screen">
2024-11-13 09:32:20 +05:30
<h1 className="md:text-4xl text-xl font-bold text-white mb-6">
Bookmarked Polls
</h1>
{isError && (
<div className="h-60 w-full">
<ErrorFallback onRetry={refetch} />
</div>
)}
{isLoading && <div className="skeleton h-40 w-full"></div>}
{isSuccess && (
<div className="overflow-x-auto">
<table className="table w-full bg-base-100">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Description</th>
<th>Actions</th>
2024-11-05 23:50:55 +05:30
</tr>
2024-11-13 09:32:20 +05:30
</thead>
<tbody>
2024-11-13 18:37:05 +05:30
{formatDataByDate(data.data).map((bookmark, index) => (
2024-11-13 09:32:20 +05:30
<tr key={bookmark._id}>
<th>{index + 1}</th>
<td className="text-white text-sm md:text-base">
{bookmark.title}
</td>
<td className="text-gray-400 whitespace-normal break-words max-w-xs text-xs md:text-base">
{bookmark.description}
</td>
<td>
<button
onClick={() => handleViewPollClick(bookmark._id)}
className="btn btn-sm btn-primary text-sm md:text-base mb-2 md:mb-1"
>
View Poll
</button>
<button
onClick={() => handleRemoveBookmark(bookmark._id)}
className="btn btn-sm btn-error text-sm md:text-base ml-2"
>
Remove
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
2024-11-05 23:50:55 +05:30
</div>
);
}
export default Bookmark;