App.PollResults = function PollResults({ code }) {
const [poll, setPoll] = React.useState(null);
const [results, setResults] = React.useState([]);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState('');
const loadResults = () => {
App.API.get('results/' + code)
.then(data => {
setPoll(data.poll);
setResults(data.results);
setLoading(false);
})
.catch(err => {
setError(err.message);
setLoading(false);
});
};
React.useEffect(() => { loadResults(); }, [code]);
if (loading) return ;
if (error) return
;
const renderCarList = (result) => {
const cars = result.cars || [];
const catBadges = (result.categories || []).map((cat, i) => (
All {cat.category_name}
));
return (
{cars.map((car, i) => (
{car.name}
))}
{catBadges}
);
};
const renderVoterList = (result) => {
if (!result.votes || result.votes.length === 0) return null;
return (
Individual Votes
{result.votes.map((vote, i) => {
const voterName = vote.voter_nickname
? vote.voter_nickname
: vote.voter_id
? 'User #' + vote.voter_id
: 'Anonymous';
return (
);
})}
);
};
return (
{results.map((result, index) => (
#{index + 1}
{result.combo_name}
Track: {result.track_name}
{renderCarList(result)}
{result.avg_rating.toFixed(1)}
{result.vote_count} vote{result.vote_count !== 1 ? 's' : ''}
{[5, 4, 3, 2, 1].map(star => {
const count = result.distribution[star] || 0;
const pct = result.vote_count > 0 ? (count / result.vote_count * 100) : 0;
return (
);
})}
{renderVoterList(result)}
))}
{results.length === 0 && (
No votes yet. Share the poll link to get started!
)}
);
};