|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { useSnapshot } from 'valtio'; |
| 3 | +import ConfirmationDialog from '../components/ui/ConfirmationDialog'; |
| 4 | +import ResultCard from '../components/ui/ResultCard'; |
| 5 | +import { actions, state } from '../state'; |
| 6 | + |
| 7 | +export const Results: React.FC = () => { |
| 8 | + const { poll, isAdmin, participantCount, rankingsCount } = useSnapshot(state); |
| 9 | + const [isConfirmationOpen, setIsConfirmationOpen] = useState(false); |
| 10 | + const [isLeavePollOpen, setIsLeavePollOpen] = useState(false); |
| 11 | + |
| 12 | + return ( |
| 13 | + <> |
| 14 | + <div className="mx-auto flex flex-col w-full justify-between items-center h-full max-w-sm"> |
| 15 | + <div className="w-full"> |
| 16 | + <h1 className="text-center mt-12 mb-4">Results</h1> |
| 17 | + {poll?.results.length ? ( |
| 18 | + <ResultCard results={poll?.results} /> |
| 19 | + ) : ( |
| 20 | + <p className="text-center text-xl"> |
| 21 | + <span className="text-orange-600">{rankingsCount}</span> of{' '} |
| 22 | + <span className="text-purple-600">{participantCount}</span>{' '} |
| 23 | + participants have voted |
| 24 | + </p> |
| 25 | + )} |
| 26 | + </div> |
| 27 | + <div className="flex flex-col justify-center"> |
| 28 | + {isAdmin && !poll?.results.length && ( |
| 29 | + <> |
| 30 | + <button |
| 31 | + className="box btn-orange my-2" |
| 32 | + onClick={() => setIsConfirmationOpen(true)} |
| 33 | + > |
| 34 | + End Poll |
| 35 | + </button> |
| 36 | + </> |
| 37 | + )} |
| 38 | + {!isAdmin && !poll?.results.length && ( |
| 39 | + <div className="my-2 italic"> |
| 40 | + Waiting for Admin,{' '} |
| 41 | + <span className="font-semibold"> |
| 42 | + {poll?.participants[poll?.adminID]} |
| 43 | + </span> |
| 44 | + , to finalize the poll. |
| 45 | + </div> |
| 46 | + )} |
| 47 | + {!!poll?.results.length && ( |
| 48 | + <button |
| 49 | + className="box btn-purple my-2" |
| 50 | + onClick={() => setIsLeavePollOpen(true)} |
| 51 | + > |
| 52 | + Leave Poll |
| 53 | + </button> |
| 54 | + )} |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + {isAdmin && ( |
| 58 | + <ConfirmationDialog |
| 59 | + message="Are you sure close the poll and calculate the results?" |
| 60 | + showDialog={isConfirmationOpen} |
| 61 | + onCancel={() => setIsConfirmationOpen(false)} |
| 62 | + onConfirm={() => { |
| 63 | + actions.closePoll(); |
| 64 | + setIsConfirmationOpen(false); |
| 65 | + }} |
| 66 | + /> |
| 67 | + )} |
| 68 | + {isLeavePollOpen && ( |
| 69 | + <ConfirmationDialog |
| 70 | + message="You'll lose ya results. Dat alright?" |
| 71 | + showDialog={isLeavePollOpen} |
| 72 | + onCancel={() => setIsLeavePollOpen(false)} |
| 73 | + onConfirm={() => actions.startOver()} |
| 74 | + /> |
| 75 | + )} |
| 76 | + </> |
| 77 | + ); |
| 78 | +}; |
0 commit comments