@@ -43,7 +43,7 @@ import { SubscribeToUnlockCard } from '@src/components/subscribe-to-unlock-card/
4343import Popover from '@mui/material/Popover' ;
4444import { useNotifications } from '@src/hooks/use-notifications.ts' ;
4545import { openLoginModal } from '@redux/auth' ;
46- import { useDispatch , useSelector } from 'react-redux' ;
46+ import { useDispatch } from 'react-redux' ;
4747import { useNotificationPayload } from '@src/hooks/use-notification-payload.ts' ;
4848import AvatarProfile from "@src/components/avatar/avatar.tsx" ;
4949import { PublicationDetailProps } from '@src/components/publication-detail-main/types.ts' ;
@@ -56,8 +56,6 @@ import {
5656} from '@src/graphql/generated/hooks.tsx' ;
5757import { resolveSrc } from '@src/utils/image.ts' ;
5858import { useBookmarks } from '@src/hooks/use-bookmark.ts' ;
59- import { RootState } from '@redux/store.ts' ;
60- import { decrementCounterLikes , incrementCounterLikes , setCounterLikes } from '@redux/comments' ;
6159
6260// ----------------------------------------------------------------------
6361
@@ -73,8 +71,8 @@ export default function PublicationDetailMain({
7371 const [ openConfirmModal , setOpenConfirmModal ] = useState ( false ) ;
7472 const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null ) ;
7573 const [ hasLiked , setHasLiked ] = useState ( false ) ;
76- const likesCount = useSelector ( ( s : RootState ) => s . comments . counterLikes [ post . id ] ?? post . likeCount ) ;
77- const counters = useSelector ( ( s : RootState ) => s . comments . counterLikes ) ;
74+ const [ likesCount , setLikesCount ] = useState ( post . likeCount ) ;
75+ const [ commentCount , setCommentCount ] = useState ( post . commentCount ) ;
7876
7977 const router = useRouter ( ) ;
8078 const theme = useTheme ( ) ;
@@ -83,68 +81,61 @@ export default function PublicationDetailMain({
8381 const [ hidePost ] = useHidePostMutation ( ) ;
8482 const { sendNotification } = useNotifications ( ) ;
8583 const { generatePayload } = useNotificationPayload ( sessionData ) ;
86- const [ getIsPostLiked , { data : postLikedData , loading : postLikedLoading } ] = useGetIsPostLikedLazyQuery ( )
84+ const [ getIsPostLiked , { loading : postLikedLoading } ] = useGetIsPostLikedLazyQuery ( )
8785 const [ togglePostLike , { loading : togglePostLikeLoading } ] = useTogglePostLikeMutation ( )
8886 const { has, loading : loadingList } = useBookmarks ( ) ;
8987 const { toggle, loading : loadingToggle } = useToggleBookmark ( ) ;
90- const commentCount = useSelector ( ( s : RootState ) => s . comments . postCommentCount [ post . id ] ?? post . commentCount ) ;
9188
9289 const isBookmarked = has ( post . id ) ;
9390 const isLoading = togglePostLikeLoading || postLikedLoading
9491 const variants = theme . direction === 'rtl' ? varFade ( ) . inLeft : varFade ( ) . inRight ;
9592 const openMenu = Boolean ( anchorEl ) ;
9693
97- const toggleReaction = async ( ) => {
94+ const handleToggleLike = async ( ) => {
9895 if ( ! sessionData ?. authenticated ) return dispatch ( openLoginModal ( ) ) ;
9996
100- // Send a notification to the profile owner using the sendNotification function from useNotifications hook
101- const payloadForNotification = generatePayload (
102- 'LIKE' ,
103- {
104- id : post . author . address ,
105- displayName : post . author . displayName ?? 'Watchit' ,
106- avatar : resolveSrc ( post . author . profilePicture || post . author . address , 'profile' ) ,
107- } ,
108- {
109- rawDescription : `${ sessionData ?. user ?. displayName } liked ${ post . title } ` ,
110- root_id : post . id ,
111- post_title : post . title ,
112- }
113- ) ;
114-
11597 try {
116- const res = await togglePostLike ( {
117- variables : {
118- input : {
119- postId : post . id
120- }
121- }
122- } ) ;
98+ const res = await togglePostLike ( { variables : { input : { postId : post . id } } } ) ;
99+ const isNowLiked = res . data ?. togglePostLike ?? false ;
123100
124- const isLiked = res ?. data ?. togglePostLike ?? false ;
101+ setHasLiked ( isNowLiked ) ;
102+ setLikesCount ( ( prev ) => prev + ( isNowLiked ? 1 : - 1 ) ) ;
125103
126- setHasLiked ( isLiked ) ;
127- dispatch ( isLiked ? incrementCounterLikes ( post . id ) : decrementCounterLikes ( post . id ) ) ;
104+ if ( isNowLiked ) {
105+ // Send a notification to the profile owner using the sendNotification function from useNotifications hook
106+ const payloadForNotification = generatePayload (
107+ 'LIKE' ,
108+ {
109+ id : post . author . address ,
110+ displayName : post . author . displayName ?? 'Watchit' ,
111+ avatar : resolveSrc ( post . author . profilePicture || post . author . address , 'profile' ) ,
112+ } ,
113+ {
114+ rawDescription : `${ sessionData ?. user ?. displayName } liked ${ post . title } ` ,
115+ root_id : post . id ,
116+ post_title : post . title ,
117+ }
118+ ) ;
128119
129- // Send notification to the author when not already liked
130- if ( res ?. data ?. togglePostLike ) {
131120 sendNotification ( post . author . address , sessionData ?. user ?. address ?? '' , payloadForNotification ) ;
132121 }
133122 } catch ( err ) {
134- console . error ( 'Error toggling reaction:' , err ) ;
123+ console . error ( err ) ;
135124 }
136125 } ;
137126
138- useEffect ( ( ) => {
139- setHasLiked ( postLikedData ?. getIsPostLiked ?? false ) ;
140- } , [ postLikedData ] ) ;
127+ const handleCommentSuccess = ( wasReply = false ) => {
128+ if ( ! wasReply ) {
129+ // Es un comentario raíz → solo incrementamos el counter del post
130+ setCommentCount ( ( c ) => ( c ?? 0 ) + 1 ) ;
131+ }
132+ } ;
141133
142134 useEffect ( ( ) => {
143- getIsPostLiked ( { variables : { postId : post . id } } ) ;
144- if ( post . likeCount !== undefined ) {
145- dispatch ( setCounterLikes ( { publicationId : post . id , likes : post . likeCount } ) ) ;
146- }
147- } , [ post . likeCount , post . id ] ) ;
135+ getIsPostLiked ( { variables : { postId : post . id } } ) . then ( ( res ) =>
136+ setHasLiked ( res . data ?. getIsPostLiked ?? false ) ,
137+ ) ;
138+ } , [ post . id ] ) ;
148139
149140 const handleHide = async ( ) => {
150141 await hidePost ( { variables : { postId : post . id } } ) ;
@@ -350,7 +341,7 @@ export default function PublicationDetailMain({
350341 height : '40px' ,
351342 minWidth : '40px' ,
352343 } }
353- onClick = { toggleReaction }
344+ onClick = { handleToggleLike }
354345 disabled = { isLoading }
355346 >
356347 { isLoading ? (
@@ -444,6 +435,7 @@ export default function PublicationDetailMain({
444435 displayName : post . author . displayName ?? 'Watchit' ,
445436 avatar : resolveSrc ( post . author . profilePicture || post . author . address , 'profile' ) ,
446437 } }
438+ onSuccess = { ( ) => handleCommentSuccess ( false ) }
447439 />
448440 ) : (
449441 < Typography
@@ -462,7 +454,7 @@ export default function PublicationDetailMain({
462454 ) }
463455 </ Box >
464456 < Box sx = { { display : 'flex' , flexDirection : 'column' , mt : 2 , pr : 1 } } >
465- < PostCommentList publicationId = { post . id } showReplies />
457+ < PostCommentList publicationId = { post . id } showReplies onReplyCreated = { ( ) => handleCommentSuccess ( true ) } />
466458 </ Box >
467459 </ Box >
468460 ) }
0 commit comments