Bookmark in frontend complete 🔖

This commit is contained in:
Manik Maity
2024-11-13 09:32:20 +05:30
parent 6ab90696df
commit d37af8667a
3 changed files with 90 additions and 47 deletions

View File

@@ -17,7 +17,7 @@ function useBookmark() {
}, },
}) })
const handleBookmark = (pollId) => { const handleBookmark = async (pollId) => {
BookmarkMutation.mutate(pollId); BookmarkMutation.mutate(pollId);
} }

View File

@@ -1,56 +1,93 @@
// BookmarkPage.js // BookmarkPage.js
import React from "react"; import React from "react";
import { getUserBookmarks } from "../services/getUserBookmarks";
const dummyBookmarks = [ import { useQuery, useQueryClient } from "react-query";
{ import ErrorFallback from "../components/Errors/ErrorFallback";
id: 1, import useBookmark from "../hooks/useBookmark";
title: "Favorite Programming Language?", import { useNavigate } from "react-router-dom";
description: "Vote for your favorite programming language among the listed options.",
},
{
id: 2,
title: "Best Framework for Web Development?",
description: "Share your thoughts on the best framework for web development.",
},
{
id: 3,
title: "Favorite Food?",
description: "What's your favorite food? Vote and see the results.",
},
];
function Bookmark() { function Bookmark() {
const { handleBookmark } = useBookmark();
const navigator = useNavigate();
const queryClient = useQueryClient();
const { data, isLoading, isError, refetch, isSuccess } = useQuery(
["bookmarks"],
getUserBookmarks,
{
cacheTime: 1000 * 60 * 5, // 5 minutes
staleTime: 1000 * 60 * 10, // 10 minutes
}
);
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);
};
return ( return (
<div className="p-6 bg-base-200 h-screen"> <div className="p-6 bg-base-200 h-screen">
<h1 className="md:text-4xl text-xl font-bold text-white mb-6">Bookmarked Polls</h1> <h1 className="md:text-4xl text-xl font-bold text-white mb-6">
<div className="overflow-x-auto"> Bookmarked Polls
<table className="table w-full bg-base-100"> </h1>
<thead> {isError && (
<tr> <div className="h-60 w-full">
<th>#</th> <ErrorFallback onRetry={refetch} />
<th>Title</th> </div>
<th>Description</th> )}
<th>Actions</th> {isLoading && <div className="skeleton h-40 w-full"></div>}
</tr> {isSuccess && (
</thead> <div className="overflow-x-auto">
<tbody> <table className="table w-full bg-base-100">
{dummyBookmarks.map((bookmark, index) => ( <thead>
<tr key={bookmark.id}> <tr>
<th>{index + 1}</th> <th>#</th>
<td className="text-white text-sm md:text-base">{bookmark.title}</td> <th>Title</th>
<td className="text-gray-400 whitespace-normal break-words max-w-xs text-xs md:text-base"> <th>Description</th>
{bookmark.description} <th>Actions</th>
</td>
<td>
<button className="btn btn-primary text-sm md:text-base">
View Poll
</button>
</td>
</tr> </tr>
))} </thead>
</tbody> <tbody>
</table> {data?.data?.map((bookmark, index) => (
</div> <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>
)}
</div> </div>
); );
} }

View File

@@ -0,0 +1,6 @@
import axiosInstance from "../helper/axiosInstance";
export async function getUserBookmarks() {
const response = await axiosInstance.get("/poll/bookmarks");
return response.data;
}