Skip to content

Commit

Permalink
fetch candidates for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
its-shivam1008 committed Oct 17, 2024
1 parent 5fad5cd commit 0e20483
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/Component/AdminDashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { uploadImageToCloudinary } from "../upload/uploadImageToCloudinary";

const AdminDashboard = () => {

const [token, setToken] = useState(localStorage.getItem('token'))

const { loggedUser, isLoggedIn, logout, fetchUser, status } = useAuth();

const navigate = useNavigate();
Expand All @@ -29,37 +31,15 @@ const AdminDashboard = () => {
setDataOption(event.target.value);
};

const [candidates, setCandidates] = useState([
{
id: 1,
name: "John Doe",
party: "Democratic Party",
votes: 120,
logo: "https://via.placeholder.com/40", // Replace with real party logo URL
},
{
id: 2,
name: "Jane Smith",
party: "Republican Party",
votes: 85,
logo: "https://via.placeholder.com/40", // Replace with real party logo URL
},
{
id: 3,
name: "Mike Johnson",
party: "Green Party",
votes: 45,
logo: "https://via.placeholder.com/40", // Replace with real party logo URL
},
]);
const [candidates, setCandidates] = useState([]);

const [newCandidateName, setNewCandidateName] = useState("");
const [newPartyName, setNewPartyName] = useState("");
const [editCandidateId, setEditCandidateId] = useState(null);
const [editCandidateName, setEditCandidateName] = useState("");

const totalVotes = candidates.reduce(
(acc, candidate) => acc + candidate.votes,
(acc, candidate) => acc + candidate.voteCount,
0
);

Expand All @@ -68,8 +48,35 @@ const AdminDashboard = () => {
navigate('/login');
}

const fetchCandidates = async () => {
const response = await fetch(`${import.meta.env.VITE_BACKEND_PUBLIC_URL}/admin/candidates`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
})
const res = await response.json()
if(!response.ok){
toast.error(res.message, {
position: "bottom-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "colored",
transition: Bounce,
});
}else{
// console.log("data :",res)
setCandidates([...res.data]);
}
}

useEffect(() => {

fetchCandidates()
}, [])


Expand Down Expand Up @@ -99,7 +106,7 @@ const AdminDashboard = () => {
}
const candidateData = {
name: newCandidateName,
party: newPartyName,
party: dataOption,
profilePic: profilePicture ? profilePictureUrl : loggedUser?.profilePic,
};

Expand Down Expand Up @@ -137,14 +144,7 @@ const AdminDashboard = () => {
theme: "colored",
transition: Bounce,
});
const newCandidate = {
id: candidates.length + 1,
name: newCandidateName,
party: dataOption,
votes: 0,
logo: res.response.profilePic, // Placeholder logo
};
setCandidates([...candidates, newCandidate]);
fetchCandidates();
setNewCandidateName("");
setNewPartyName("");
}
Expand Down Expand Up @@ -353,15 +353,15 @@ const AdminDashboard = () => {
{/* Party Logo */}
<td className="py-3 text-center px-2 sm:px-6 ">
<img
src={candidate.logo}
src={candidate.profilePic}
alt={`${candidate.party} logo`}
className="w-8 h-8 sm:w-10 sm:h-10 rounded-full object-cover"
/>
</td>

{/* Vote Info */}
<td className="py-3 px-3 sm:px-6 text-center">
<span>{candidate.votes}</span>
<span>{candidate.voteCount}</span>
</td>

{/* Action Buttons */}
Expand Down

0 comments on commit 0e20483

Please sign in to comment.