Started Poll View Page Frontend 📊
This commit is contained in:
@@ -28,7 +28,7 @@ function App() {
|
|||||||
<Route element={<PrivateRoute />}>
|
<Route element={<PrivateRoute />}>
|
||||||
<Route path="dashboard" element={<Dashboard />} />
|
<Route path="dashboard" element={<Dashboard />} />
|
||||||
<Route path="bookmark" element={<Bookmark />} />
|
<Route path="bookmark" element={<Bookmark />} />
|
||||||
<Route path="/voting/:pollId" element={<VotingPage />} />
|
<Route path="/view/:pollId" element={<VotingPage />} />
|
||||||
<Route path="/create" element={<CreatePollForm />} />
|
<Route path="/create" element={<CreatePollForm />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
|
|||||||
@@ -1,29 +1,16 @@
|
|||||||
import React, { useState } from "react";
|
import React, { } from "react";
|
||||||
import { FaTrashAlt, FaUserEdit } from "react-icons/fa";
|
import { FaTrashAlt} from "react-icons/fa";
|
||||||
import {useMutation} from "react-query"
|
import useDeletePoll from "../../hooks/usedeletePoll";
|
||||||
import deletePollService from "../../services/deletePollService";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { toast } from "react-toastify";
|
|
||||||
|
|
||||||
function PollTableRow({ poll, index, refetch }) {
|
function PollTableRow({ poll, index, refetch }) {
|
||||||
|
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const handleDelete = useDeletePoll(poll._id, refetch);
|
||||||
|
|
||||||
const mutation = useMutation(deletePollService, {
|
const handleViewOnClick = () => {
|
||||||
onSuccess : (data) => {
|
navigator(`/view/${poll._id}`);
|
||||||
console.log(data);
|
|
||||||
refetch();
|
|
||||||
toast.success(data?.message);
|
|
||||||
},
|
|
||||||
onError : (error) => {
|
|
||||||
toast.error("An unexpected error occurred");
|
|
||||||
console.log(error);
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
const handleDelete = () => {
|
|
||||||
const sure = window.confirm("Are you sure you want to delete this poll?");
|
|
||||||
if (!sure) return;
|
|
||||||
mutation.mutate(poll._id);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -42,7 +29,10 @@ function PollTableRow({ poll, index, refetch }) {
|
|||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div className="flex md:flex-row flex-col gap-2 flex-nowrap">
|
<div className="flex md:flex-row flex-wrap flex-col gap-2">
|
||||||
|
<button onClick={handleViewOnClick} className="btn btn-sm btn-primary flex items-center">
|
||||||
|
<FaTrashAlt className="mr-1" /> View
|
||||||
|
</button>
|
||||||
<button onClick={handleDelete} className="btn btn-sm btn-error flex items-center">
|
<button onClick={handleDelete} className="btn btn-sm btn-error flex items-center">
|
||||||
<FaTrashAlt className="mr-1" /> Delete
|
<FaTrashAlt className="mr-1" /> Delete
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
29
frontend/src/hooks/useDeletePoll.js
Normal file
29
frontend/src/hooks/useDeletePoll.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import { useMutation } from 'react-query';
|
||||||
|
import { toast } from 'react-toastify';
|
||||||
|
import deletePollService from '../services/deletePollService';
|
||||||
|
|
||||||
|
function useDeletePoll(id, refetch) {
|
||||||
|
|
||||||
|
const mutation = useMutation(deletePollService, {
|
||||||
|
onSuccess : (data) => {
|
||||||
|
console.log(data);
|
||||||
|
refetch();
|
||||||
|
toast.success(data?.message);
|
||||||
|
},
|
||||||
|
onError : (error) => {
|
||||||
|
toast.error("An unexpected error occurred");
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const handleDelete = () => {
|
||||||
|
const sure = window.confirm("Are you sure you want to delete this poll?");
|
||||||
|
if (!sure) return;
|
||||||
|
mutation.mutate(id);
|
||||||
|
};
|
||||||
|
|
||||||
|
return handleDelete;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useDeletePoll
|
||||||
@@ -4,7 +4,5 @@ import './index.css'
|
|||||||
import App from './App.jsx'
|
import App from './App.jsx'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
createRoot(document.getElementById('root')).render(
|
||||||
<StrictMode>
|
|
||||||
<App />
|
<App />
|
||||||
</StrictMode>,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,11 +2,22 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Bar } from "react-chartjs-2";
|
import { Bar } from "react-chartjs-2";
|
||||||
import { Chart as ChartJS, BarElement, CategoryScale, LinearScale } from "chart.js";
|
import { Chart as ChartJS, BarElement, CategoryScale, LinearScale } from "chart.js";
|
||||||
|
import { useQuery } from "react-query";
|
||||||
|
import { useParams } from "react-router-dom";
|
||||||
|
import getPollData from "../services/getPollData";
|
||||||
|
|
||||||
ChartJS.register(BarElement, CategoryScale, LinearScale);
|
ChartJS.register(BarElement, CategoryScale, LinearScale);
|
||||||
|
|
||||||
function VotingPage() {
|
function VotingPage() {
|
||||||
// Dummy poll data
|
|
||||||
|
const { pollId } = useParams();
|
||||||
|
|
||||||
|
const { data: poll, isLoading, isError } = useQuery(["poll", pollId], () => getPollData(pollId), {
|
||||||
|
cacheTime : 10*100*60, // 10 minutes
|
||||||
|
staleTime : 20*100*60, // 20 minutes
|
||||||
|
});
|
||||||
|
console.log(poll);
|
||||||
|
|
||||||
const pollData = {
|
const pollData = {
|
||||||
title: "What's your favorite programming language?",
|
title: "What's your favorite programming language?",
|
||||||
options: [
|
options: [
|
||||||
|
|||||||
8
frontend/src/services/getPollData.js
Normal file
8
frontend/src/services/getPollData.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import axiosInstance from "../helper/axiosInstance";
|
||||||
|
|
||||||
|
async function getPollData(pollId) {
|
||||||
|
const response = await axiosInstance.get(`/poll/data/${pollId}`);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getPollData;
|
||||||
Reference in New Issue
Block a user