184 lines
6.0 KiB
TypeScript
184 lines
6.0 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { useAccount } from 'wagmi'
|
|
import sdk from '@farcaster/miniapp-sdk'
|
|
import Header from '@/components/Header'
|
|
import PollCard from '@/components/PollCard'
|
|
import VoteButton from '@/components/VoteButton'
|
|
import { Poll, Vote } from '@/types'
|
|
|
|
export default function HomePage() {
|
|
const router = useRouter()
|
|
const { address } = useAccount()
|
|
const [poll, setPoll] = useState<Poll | null>(null)
|
|
const [userVote, setUserVote] = useState<Vote | null>(null)
|
|
const [selectedOptionId, setSelectedOptionId] = useState<string | null>(null)
|
|
const [userId, setUserId] = useState<string>('')
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
useEffect(() => {
|
|
const loadData = async () => {
|
|
try {
|
|
// Get user context
|
|
const context = await sdk.context
|
|
const fid = context?.user?.fid?.toString() || 'anonymous'
|
|
setUserId(fid)
|
|
|
|
// Load today's poll from API
|
|
const response = await fetch('/api/polls/today')
|
|
const todayPoll = await response.json()
|
|
setPoll(todayPoll)
|
|
|
|
// Check if user already voted - TODO: add API endpoint
|
|
// if (todayPoll) {
|
|
// const existingVote = await getUserVote(todayPoll.id, fid)
|
|
// setUserVote(existingVote)
|
|
// }
|
|
} catch (error) {
|
|
console.error('Failed to load data:', error)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
loadData()
|
|
}, [])
|
|
|
|
const handleVoteSuccess = async (optionId: string, amount: number, txHash: string) => {
|
|
if (!poll || !userId || !address) return
|
|
|
|
try {
|
|
const response = await fetch('/api/votes', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
pollId: poll.id,
|
|
userId,
|
|
userAddress: address,
|
|
optionId,
|
|
amount,
|
|
txHash
|
|
})
|
|
})
|
|
|
|
const vote = await response.json()
|
|
setUserVote(vote)
|
|
|
|
// Redirect to success page
|
|
router.push(`/success?pollId=${poll.id}&optionId=${optionId}&amount=${amount}`)
|
|
} catch (error) {
|
|
console.error('Failed to submit vote:', error)
|
|
}
|
|
}
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center">
|
|
<div className="text-center">
|
|
<div className="text-4xl mb-4 animate-spin">⏳</div>
|
|
<p className="text-gray-600">Loading today's poll...</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!poll) {
|
|
return (
|
|
<div className="min-h-screen">
|
|
<Header />
|
|
<div className="max-w-4xl mx-auto p-4">
|
|
<div className="bg-white rounded-2xl p-8 text-center">
|
|
<div className="text-6xl mb-4">😴</div>
|
|
<h2 className="text-2xl font-bold text-gray-800 mb-2">No poll available</h2>
|
|
<p className="text-gray-600">Check back tomorrow for a new daily poll!</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen pb-20">
|
|
<Header />
|
|
|
|
<main className="max-w-4xl mx-auto p-4 space-y-6">
|
|
{/* Poll Card */}
|
|
<PollCard poll={poll} userVoteOptionId={userVote?.optionId} />
|
|
|
|
{/* Voting Section */}
|
|
{!userVote && poll.status === 'active' && (
|
|
<div className="bg-white rounded-2xl p-6 shadow-xl">
|
|
<h3 className="text-xl font-bold text-gray-800 mb-4">Cast Your Vote</h3>
|
|
|
|
{/* Option Selection */}
|
|
<div className="grid gap-3 mb-6">
|
|
{poll.options.map((option) => (
|
|
<button
|
|
key={option.id}
|
|
onClick={() => setSelectedOptionId(option.id)}
|
|
className={`p-4 rounded-xl border-2 text-left transition-all ${selectedOptionId === option.id
|
|
? 'border-celo-green bg-celo-green/10'
|
|
: 'border-gray-200 hover:border-celo-green/50'
|
|
}`}
|
|
>
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-3xl">{option.emoji}</span>
|
|
<span className="font-semibold text-gray-800">{option.text}</span>
|
|
</div>
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{/* Vote Button */}
|
|
{selectedOptionId && (
|
|
<VoteButton
|
|
poll={poll}
|
|
option={poll.options.find(opt => opt.id === selectedOptionId)!}
|
|
onVoteSuccess={handleVoteSuccess}
|
|
/>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Already Voted */}
|
|
{userVote && (
|
|
<div className="bg-celo-green/10 border-2 border-celo-green rounded-2xl p-6 text-center">
|
|
<div className="text-4xl mb-3">✅</div>
|
|
<h3 className="text-xl font-bold text-gray-800 mb-2">You've voted!</h3>
|
|
<p className="text-gray-600">
|
|
Come back when the poll closes to see if you won
|
|
</p>
|
|
<button
|
|
onClick={() => router.push('/history')}
|
|
className="mt-4 bg-white text-celo-green font-semibold py-2 px-6 rounded-lg hover:bg-gray-50 transition-all"
|
|
>
|
|
View Past Polls
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Navigation */}
|
|
<div className="flex gap-3">
|
|
<button
|
|
onClick={() => router.push('/history')}
|
|
className="flex-1 bg-white text-gray-700 font-semibold py-3 px-6 rounded-xl shadow-md hover:shadow-lg transition-all"
|
|
>
|
|
📜 History
|
|
</button>
|
|
<button
|
|
onClick={() => {
|
|
const frameUrl = `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/poll/${poll.id}`
|
|
sdk.actions.openUrl(frameUrl)
|
|
}}
|
|
className="flex-1 bg-gradient-to-r from-celo-green to-celo-yellow text-white font-semibold py-3 px-6 rounded-xl shadow-md hover:shadow-lg transition-all"
|
|
>
|
|
🔗 Share Poll
|
|
</button>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
)
|
|
}
|