diff --git a/src/components/PlayerVote.tsx b/src/components/PlayerVote.tsx new file mode 100644 index 0000000..9f8743d --- /dev/null +++ b/src/components/PlayerVote.tsx @@ -0,0 +1,74 @@ +import queryClient from '@api/queryClient'; +import { useAuth } from '@contexts/AuthContext'; +import { ArrowDownIcon, ArrowUpIcon } from '@heroicons/react/24/outline'; +import usePlayerVoteMutation from '@hooks/mutations/usePlayerVoteMutation'; +import usePlayerTotalVotesQuery from '@hooks/queries/usePlayerTotalVotesQuery'; +import usePlayerVoteQuery from '@hooks/queries/usePlayerVoteQuery'; +import toast from 'react-hot-toast/headless'; + +interface PlayerVoteProps { + playerId: string; +} + +const PlayerVote = ({ playerId }: PlayerVoteProps) => { + const { user } = useAuth(); + + const { mutateAsync } = usePlayerVoteMutation(); + + const { data: playerVote } = usePlayerVoteQuery(playerId); + const { data: totalVotes } = usePlayerTotalVotesQuery(playerId); + + const handleVote = async (vote: number) => { + try { + await mutateAsync({ playerId, vote }).then(() => { + queryClient.setQueryData(['playerVote', playerId, user?.id], vote); + queryClient.invalidateQueries(['playerTotalVotes', playerId]); + }); + } catch (error) { + if (error instanceof Error) { + toast('Something went wrong!'); + return; + } + } + }; + + const isPlayerMe = user?.id === playerId; + + return ( +
0 + ? 'text-green-500' + : totalVotes && totalVotes < 0 + ? 'text-red-500' + : '' + }`} + > + {totalVotes ?? 0} +
+ {!isPlayerMe && ( + + )} +{username}
- -- {'Member since: '} - - {format(new Date(player.created_at), 'd MMM yyyy')} - -
+{username}
++ {'Member since: '} + + {format(new Date(player.created_at), 'd MMM yyyy')} + +
+