diff --git a/backend/src/routes/v1/poll.route.js b/backend/src/routes/v1/poll.route.js index fa4c266..1613f50 100644 --- a/backend/src/routes/v1/poll.route.js +++ b/backend/src/routes/v1/poll.route.js @@ -59,7 +59,44 @@ pollRouter.get("/test", (req, res) => { * */ pollRouter.post("/create", validator(pollDataSchema), verifyToken, createPollController); + +/** + * @swagger + * /poll/data/{pollId}: + * get: + * summary: Get poll data + * tags: [Poll] + * parameters: + * - in: path + * name: pollId + * schema: + * type: string + * required: true + * description: Poll ID + * responses: + * 200: + * description: Poll data fetched successfully + * 401: + * description: Unauthorized + * 500: + * description: Internal server error + */ pollRouter.get("/data/:pollId", verifyToken, getPollDataController); + +/** + * @swagger + * /poll/created: + * get: + * summary: Get all created polls + * tags: [Poll] + * responses: + * 200: + * description: All created polls fetched successfully + * 401: + * description: Unauthorized + * 500: + * description: Internal server error + */ pollRouter.get("/created", verifyToken, getAllCreatedPollsController); export default pollRouter; \ No newline at end of file diff --git a/frontend/src/components/Errors/ErrorFallback.jsx b/frontend/src/components/Errors/ErrorFallback.jsx new file mode 100644 index 0000000..94558d5 --- /dev/null +++ b/frontend/src/components/Errors/ErrorFallback.jsx @@ -0,0 +1,22 @@ +import React from "react"; + +const ErrorFallback = ({ onRetry }) => { + return ( +
+
+

Oops! Something went wrong

+

+ We encountered an unexpected error. Please try again. +

+ +
+
+ ); +}; + +export default ErrorFallback; diff --git a/frontend/src/components/PollTableRow/PollTableRow.jsx b/frontend/src/components/PollTableRow/PollTableRow.jsx index fc82bc6..06c24a0 100644 --- a/frontend/src/components/PollTableRow/PollTableRow.jsx +++ b/frontend/src/components/PollTableRow/PollTableRow.jsx @@ -2,13 +2,12 @@ import React, { useState } from "react"; import { FaTrashAlt, FaUserEdit } from "react-icons/fa"; -function PollTableRow({ poll, index }) { +function PollTableRow({ poll, index, refetch }) { const [showEditForm, setShowEditForm] = useState(false); const handleDelete = () => { const sure = window.confirm("Are you sure you want to delete this poll?"); if (sure) { - // Call the delete poll function here console.log(`Poll with ID: ${poll._id} has been deleted.`); } }; @@ -26,7 +25,6 @@ function PollTableRow({ poll, index }) { {poll.description} - {poll.totalVotes} {poll.published ? ( Published @@ -36,9 +34,6 @@ function PollTableRow({ poll, index }) {
- diff --git a/frontend/src/pages/Dashboard.jsx b/frontend/src/pages/Dashboard.jsx index d305be0..bbdf943 100644 --- a/frontend/src/pages/Dashboard.jsx +++ b/frontend/src/pages/Dashboard.jsx @@ -5,70 +5,116 @@ import PollTableRow from "../components/PollTableRow/PollTableRow"; import { useNavigate } from "react-router-dom"; import useUserStore from "../store/useStore"; import useLogout from "../hooks/useLogout"; +import { useQuery } from "react-query"; +import getUserPollData from "../services/getUserPollData"; +import ErrorFallback from "../components/Errors/ErrorFallback"; function Dashboard() { - const navigator = useNavigate(); - const {handleLogout} = useLogout(); + const { handleLogout } = useLogout(); + const { user } = useUserStore(); + + const { data, isLoading, isError, refetch, isSuccess } = useQuery( + ["polls", user._id], + getUserPollData, + { + cacheTime: 1000 * 60 * 5, // 5 minutes + staleTime: 1000 * 60 * 10, // 10 minutes + } + ); + + console.log(data); const pollData = [ - { _id: "1", title: "Poll 1", description: "Description of Poll 1", totalVotes: 120, published: true }, - { _id: "2", title: "Poll 2", description: "Description of Poll 2", totalVotes: 45, published: false }, + { + _id: "1", + title: "Poll 1", + description: "Description of Poll 1", + totalVotes: 120, + published: true, + }, + { + _id: "2", + title: "Poll 2", + description: "Description of Poll 2", + totalVotes: 45, + published: false, + }, // Add more poll data as needed ]; - const {user} = useUserStore(); - return (
{/* User Profile Sidebar */} {/* Dashboard Main Content */}
{/* Dashboard Header */}
-

Poll Dashboard

-

Manage your polls, view results, and edit as needed.

+

+ Poll Dashboard +

+

+ Manage your polls, view results, and edit as needed. +

{/* Add New Poll Button */}
-
{/* Polls Table */} -
- - - - - - - - - - - - - {pollData.map((poll, index) => ( - // Replace with actual path - ))} - -
#TitleDescriptionTotal VotesPublishedActions
-
+ {isError &&
} + {isLoading &&
} + {isSuccess && +
+ + + + + + + + + + + + + {data.map((poll, index) => ( + // Replace with actual path + ))} + +
#TitleDescriptionPublishedActions
+
+ }
); diff --git a/frontend/src/services/getUserPollData.js b/frontend/src/services/getUserPollData.js new file mode 100644 index 0000000..296caae --- /dev/null +++ b/frontend/src/services/getUserPollData.js @@ -0,0 +1,8 @@ +import axiosInstance from "../helper/axiosInstance" + +async function getUserPollData() { + const responses = await axiosInstance.get("/poll/created"); + return responses.data.data; +} + +export default getUserPollData