celo-poll/components/VoteButton.tsx
2025-12-08 16:31:30 +03:00

121 lines
4.1 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useAccount, useWriteContract, useWaitForTransactionReceipt } from 'wagmi'
import { CUSD_ADDRESS, CUSD_ABI, PROTOCOL_CONFIG, cUSDToWei } from '@/lib/celo'
import { Poll, PollOption } from '@/types'
import { formatCUSD } from '@/lib/format'
interface VoteButtonProps {
poll: Poll
option: PollOption
onVoteSuccess: (optionId: string, amount: number, txHash: string) => void
disabled?: boolean
}
export default function VoteButton({ poll, option, onVoteSuccess, disabled }: VoteButtonProps) {
const [selectedAmount, setSelectedAmount] = useState<number>(PROTOCOL_CONFIG.VOTE_OPTIONS[0].amount)
const [isVoting, setIsVoting] = useState(false)
const { address, isConnected } = useAccount()
const { writeContract, data: hash, error, isPending } = useWriteContract()
const { isSuccess } = useWaitForTransactionReceipt({ hash })
const handleVote = async () => {
if (!isConnected || !address || disabled) return
setIsVoting(true)
try {
// Send cUSD to protocol wallet
const txHash = await writeContract({
address: CUSD_ADDRESS,
abi: CUSD_ABI,
functionName: 'transfer',
args: [PROTOCOL_CONFIG.WALLET_ADDRESS, cUSDToWei(selectedAmount)],
})
console.log('Transaction sent:', txHash)
} catch (err) {
console.error('Vote failed:', err)
setIsVoting(false)
}
}
// Handle transaction success
if (isSuccess && hash && isVoting) {
onVoteSuccess(option.id, selectedAmount, hash)
setIsVoting(false)
}
// Handle transaction failure
if (error && isVoting) {
console.error('Transaction failed:', error)
setIsVoting(false)
}
return (
<div className="space-y-4">
{/* Vote amount selector */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
{PROTOCOL_CONFIG.VOTE_OPTIONS.map((voteOption) => (
<button
key={voteOption.amount}
onClick={() => setSelectedAmount(voteOption.amount)}
className={`p-4 rounded-xl border-2 transition-all text-center ${
selectedAmount === voteOption.amount
? 'border-celo-green bg-celo-green/10'
: 'border-gray-200 bg-white hover:border-celo-green/50'
}`}
disabled={disabled || isPending}
>
<div className="text-2xl mb-1">{voteOption.emoji}</div>
<div className="text-sm font-semibold text-gray-800">
{voteOption.label}
</div>
</button>
))}
</div>
{/* Vote button */}
<button
onClick={handleVote}
disabled={!isConnected || disabled || isPending || isVoting}
className="w-full bg-gradient-to-r from-celo-green to-celo-yellow text-white font-bold py-4 px-6 rounded-xl shadow-lg hover:shadow-xl transition-all disabled:opacity-50 disabled:cursor-not-allowed"
>
{isPending || isVoting ? (
<span className="flex items-center justify-center gap-2">
<span className="animate-spin">⏳</span>
{isPending ? 'Confirming Transaction...' : 'Voting...'}
</span>
) : !isConnected ? (
'Connect Wallet to Vote'
) : disabled ? (
'You already voted'
) : (
`Vote ${formatCUSD(selectedAmount)} on "${option.text}"`
)}
</button>
{/* Error display */}
{error && (
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg text-sm">
<div className="font-semibold">Transaction Failed</div>
<div>{error.message}</div>
</div>
)}
{/* Success indicator */}
{isSuccess && (
<div className="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg text-sm">
<div className="font-semibold">Vote Successful!</div>
<div>Your vote has been recorded.</div>
</div>
)}
{/* Info text */}
<div className="text-xs text-gray-500 text-center">
💡 You can only vote once per poll. 95% of the pot goes to winners!
</div>
</div>
)
}