-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<div className="flex items-center gap-1"> | ||
{!isPlayerMe && ( | ||
<button | ||
className={`btn btn-circle btn-ghost btn-sm ${ | ||
playerVote === 1 ? 'btn-active' : '' | ||
}`} | ||
onClick={() => handleVote(playerVote === 1 ? 0 : 1)} | ||
> | ||
<ArrowUpIcon className="h-5 w-5" /> | ||
</button> | ||
)} | ||
<p | ||
className={`${ | ||
totalVotes && totalVotes > 0 | ||
? 'text-green-500' | ||
: totalVotes && totalVotes < 0 | ||
? 'text-red-500' | ||
: '' | ||
}`} | ||
> | ||
{totalVotes ?? 0} | ||
</p> | ||
{!isPlayerMe && ( | ||
<button | ||
className={`btn btn-circle btn-ghost btn-sm ${ | ||
playerVote === -1 ? 'btn-active' : '' | ||
}`} | ||
onClick={() => handleVote(playerVote === -1 ? 0 : -1)} | ||
> | ||
<ArrowDownIcon className="h-5 w-5" /> | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default PlayerVote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { supabase } from '@api/supabase'; | ||
import { useAuth } from '@contexts/AuthContext'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
|
||
interface PlayerVoteMutationProps { | ||
playerId: string; | ||
vote: number; | ||
} | ||
|
||
const usePlayerVoteMutation = () => { | ||
const { user } = useAuth(); | ||
|
||
return useMutation(async ({ playerId, vote }: PlayerVoteMutationProps) => { | ||
if (!user) { | ||
throw new Error('You must be logged in to do this.'); | ||
} | ||
|
||
const { data, error } = await supabase | ||
.from('player_votes') | ||
.upsert({ user_id: user.id, player_id: playerId, vote }); | ||
|
||
if (error) { | ||
throw new Error(error.message); | ||
} | ||
|
||
return data; | ||
}); | ||
}; | ||
|
||
export default usePlayerVoteMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { supabase } from '@api/supabase'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
const usePlayerTotalVotesQuery = (playerId: string) => { | ||
const queryKey = ['playerTotalVotes', playerId]; | ||
|
||
return useQuery(queryKey, async () => { | ||
const { data } = await supabase.rpc('get_player_votes', { | ||
var_player_id: playerId, | ||
}); | ||
console.log(data); | ||
|
||
return data ?? 0; | ||
}); | ||
}; | ||
|
||
export default usePlayerTotalVotesQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { supabase } from '@api/supabase'; | ||
import { useAuth } from '@contexts/AuthContext'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
const usePlayerVoteQuery = (playerId: string) => { | ||
const { user } = useAuth(); | ||
|
||
const queryKey = ['playerVote', playerId, user?.id]; | ||
|
||
return useQuery(queryKey, async () => { | ||
if (!user) { | ||
return null; | ||
} | ||
|
||
const { data } = await supabase | ||
.from('player_votes') | ||
.select('vote') | ||
.eq('user_id', user.id) | ||
.eq('player_id', playerId) | ||
.single(); | ||
|
||
return data?.vote ?? null; | ||
}); | ||
}; | ||
|
||
export default usePlayerVoteQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters