|
| 1 | +import { useState, useMemo, useEffect } from "react"; |
| 2 | +import { Button, Card } from "flowbite-react"; |
| 3 | +import { |
| 4 | + useSocketListener, |
| 5 | + useSocket, |
| 6 | + ServerToClientPayload, |
| 7 | +} from "@wasp/webSocket"; |
| 8 | +import useAuth from "@wasp/auth/useAuth"; |
| 9 | + |
| 10 | +const MainPage = () => { |
| 11 | + const { data: user } = useAuth(); |
| 12 | + const [poll, setPoll] = useState<ServerToClientPayload<"updateState"> | null>( |
| 13 | + null |
| 14 | + ); |
| 15 | + const totalVotes = useMemo(() => { |
| 16 | + return ( |
| 17 | + poll?.options.reduce((acc, option) => acc + option.votes.length, 0) ?? 0 |
| 18 | + ); |
| 19 | + }, [poll]); |
| 20 | + |
| 21 | + const { socket } = useSocket(); |
| 22 | + |
| 23 | + useSocketListener("updateState", (newState) => { |
| 24 | + setPoll(newState); |
| 25 | + }); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + socket.emit("askForStateUpdate"); |
| 29 | + }, []); |
| 30 | + |
| 31 | + function handleVote(optionId: number) { |
| 32 | + socket.emit("vote", optionId); |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="w-full max-w-2xl mx-auto p-8"> |
| 37 | + <h1 className="text-2xl font-bold">{poll?.question ?? "Loading..."}</h1> |
| 38 | + {poll && ( |
| 39 | + <p className="leading-relaxed text-gray-500"> |
| 40 | + Cast your vote for one of the options. |
| 41 | + </p> |
| 42 | + )} |
| 43 | + {poll && ( |
| 44 | + <div className="mt-4 flex flex-col gap-4"> |
| 45 | + {poll.options.map((option) => ( |
| 46 | + <Card key={option.id} className="relative transition-all duration-300 min-h-[130px]"> |
| 47 | + <div className="z-10"> |
| 48 | + <div className="mb-2"> |
| 49 | + <h2 className="text-xl font-semibold">{option.text}</h2> |
| 50 | + <p className="text-gray-700">{option.description}</p> |
| 51 | + </div> |
| 52 | + <div className="absolute bottom-5 right-5"> |
| 53 | + {user && !option.votes.includes(user.username) ? ( |
| 54 | + <Button onClick={() => handleVote(option.id)}>Vote</Button> |
| 55 | + ) : ( |
| 56 | + <Button disabled>Voted</Button> |
| 57 | + )} |
| 58 | + {!user} |
| 59 | + </div> |
| 60 | + {option.votes.length > 0 && ( |
| 61 | + <div className="mt-2 flex gap-2 flex-wrap max-w-[75%]"> |
| 62 | + {option.votes.map((vote) => ( |
| 63 | + <div |
| 64 | + key={vote} |
| 65 | + className="py-1 px-3 bg-gray-100 rounded-lg flex items-center justify-center shadow text-sm" |
| 66 | + > |
| 67 | + <div className="w-2 h-2 bg-green-500 rounded-full mr-2"></div> |
| 68 | + <div className="text-gray-700">{vote}</div> |
| 69 | + </div> |
| 70 | + ))} |
| 71 | + </div> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + <div className="absolute top-5 right-5 p-2 text-sm font-semibold bg-gray-100 rounded-lg z-10"> |
| 75 | + {option.votes.length} / {totalVotes} |
| 76 | + </div> |
| 77 | + <div |
| 78 | + className="absolute inset-0 bg-gradient-to-r from-yellow-400 to-orange-500 opacity-75 rounded-lg transition-all duration-300" |
| 79 | + style={{ |
| 80 | + width: `${ |
| 81 | + totalVotes > 0 |
| 82 | + ? (option.votes.length / totalVotes) * 100 |
| 83 | + : 0 |
| 84 | + }%`, |
| 85 | + }} |
| 86 | + ></div> |
| 87 | + </Card> |
| 88 | + ))} |
| 89 | + </div> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
| 94 | +export default MainPage; |
0 commit comments