Merge pull request #10 from Nekiiinkognito/feature/fixVoteCounter

Feature/fix vote counter
This commit is contained in:
Maxim 2025-01-29 13:54:41 +03:00 committed by GitHub
commit 0f985a85e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "enshi", "name": "enshi",
"private": true, "private": true,
"version": "0.1.6", "version": "0.1.7",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@ -48,7 +48,7 @@ export default function VoteButton(props: TVoteButton) {
queryKey: [props.vote + "voteCheck"], queryKey: [props.vote + "voteCheck"],
}); });
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: ["post_vote_counter"], queryKey: [`post_vote_counter_${props.postId}`],
}); });
}, },
}); });

View File

@ -8,13 +8,14 @@ type TVoteCounter = {
export default function VoteCounter(props: TVoteCounter) { export default function VoteCounter(props: TVoteCounter) {
const { data, isLoading } = useQuery({ const { data, isLoading } = useQuery({
queryKey: ["post_vote_counter"], queryKey: [`post_vote_counter_${props.postId}` ],
queryFn: async () => { queryFn: async () => {
const response = await axiosLocalhost.get( const response = await axiosLocalhost.get(
`post-votes/${props.postId}` `post-votes/${props.postId}`
); );
return response.data as { upvotes: number; downvotes: number }; return response.data as { upvotes: number; downvotes: number };
}, },
gcTime: 1000 * 60,
}); });
const calculateRating = (upvotes: number, downvotes: number) => { const calculateRating = (upvotes: number, downvotes: number) => {
@ -22,12 +23,12 @@ export default function VoteCounter(props: TVoteCounter) {
} }
if (isLoading) { if (isLoading) {
return <Skeleton> return <Skeleton className="w-4">
{calculateRating(0, 0)} {calculateRating(0, 0)}
</Skeleton> </Skeleton>
} }
return <Box> return <Box className="flex justify-center w-4 gap-2">
{calculateRating(data?.upvotes || 0, data?.downvotes || 0)} {calculateRating(data?.upvotes || 0, data?.downvotes || 0)}
</Box>; </Box>;
} }