22
33import { useEffect , useState } from "react" ;
44import { useUserStore } from "@/store/codev-store" ;
5+ import { useFeedsStore } from "@/store/feeds-store" ;
56import { ArrowBigUp } from "lucide-react" ;
67
78import {
@@ -21,6 +22,10 @@ export default function PostUpvote({ post }: PostUpvoteProps) {
2122
2223 const [ isUpvoted , setIsUpvoted ] = useState ( false ) ;
2324 const [ upvotes , setUpvotes ] = useState ( 0 ) ;
25+ const { posts, fetchPosts } = useFeedsStore ( ( state ) => ( {
26+ posts : state . posts ,
27+ fetchPosts : state . fetchPosts ,
28+ } ) ) ;
2429
2530 useEffect ( ( ) => {
2631 const checkUpvote = async ( ) => {
@@ -46,30 +51,32 @@ export default function PostUpvote({ post }: PostUpvoteProps) {
4651 }
4752 } ;
4853 checkUpvote ( ) ;
49- } , [ user ?. id , post ?. id ] ) ;
54+ } , [ user ?. id , post ?. id , posts ] ) ;
5055
5156 const handleUpvote = async ( e : React . MouseEvent ) => {
5257 if ( user ) {
5358 e . stopPropagation ( ) ;
5459 e . preventDefault ( ) ;
5560
61+ setIsUpvoted ( ( prev ) => ! prev ) ;
62+ setUpvotes ( ( prev ) => prev + ( isUpvoted ? - 1 : 1 ) ) ;
63+
5664 if ( ! isUpvoted ) {
5765 const postUpvote = await AddPostUpvote ( post . id , user . id ) ;
5866 } else {
5967 removePostUpvote ( post . id , user . id ) ;
6068 }
6169
62- setIsUpvoted ( ( prev ) => ! prev ) ;
63- setUpvotes ( ( prev ) => prev + ( isUpvoted ? - 1 : 1 ) ) ;
70+ await fetchPosts ( ) ;
6471 }
6572 } ;
6673
6774 return (
6875 < button
6976 className = { `flex items-center space-x-1 ${
7077 isUpvoted
71- ? "text-customBlue-500 dark:text-customBlue-400 "
72- : "hover:text-customBlue-500 dark:hover:text-customBlue-400 "
78+ ? "text-customBlue-300 dark:text-customBlue-200 "
79+ : "hover:text-customBlue-300 dark:hover:text-customBlue-200 "
7380 } `}
7481 onClick = { handleUpvote }
7582 >
0 commit comments