import { useEffect, useState } from "react"; import { getVotes, toggleVote, type VoteCount } from "../api/collaboration"; interface Props { ideaId: string; } export default function VoteButtons({ ideaId }: Props) { const [votes, setVotes] = useState({ up: 0, down: 0, user_vote: null }); const [sending, setSending] = useState(false); useEffect(() => { getVotes(ideaId).then(setVotes).catch(console.error); }, [ideaId]); async function handleVote(vote: "up" | "down") { if (sending) return; setSending(true); try { const updated = await toggleVote(ideaId, vote); setVotes(updated); } catch (e) { console.error(e); } finally { setSending(false); } } const upActive = votes.user_vote === "up"; const downActive = votes.user_vote === "down"; return (
); }