fix like message

This commit is contained in:
Guillaume Dorce 2022-11-03 15:14:48 +01:00
parent d69f31addc
commit b6bda6e436
2 changed files with 32 additions and 14 deletions

View File

@ -1,15 +1,39 @@
import { FaThumbsUp } from 'react-icons/fa'; import { FaThumbsUp } from 'react-icons/fa';
import { likePost, unlikePost } from '@controllers/LikeController'; import { likePost, unlikePost } from '@controllers/LikeController';
import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toastError, toastSuccess } from '@controllers/Toasts'; import { toastError, toastSuccess } from '@controllers/Toasts';
import { useEffect, useState } from 'react';
import { getMeInfo } from '@controllers/UserController';
const Like = ({ messageId, isLiked }: { messageId: string; isLiked: boolean }) => { const Like = ({ message }: { message: any}) => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [liked, setLiked] = useState(false);
const me = useQuery(['me'], getMeInfo, {
onSuccess: (data) => {
return data;
},
onError: (error) => {
toastError(error as string);
},
});
const mutateLike = useMutation(isLiked ? unlikePost : likePost, { useEffect(() => {
if (message.likedBy.some((like: any) => like.userId === me.data?.id)) {
setLiked(true);
}
}, [message]);
const mutateLike = useMutation(liked ? unlikePost : likePost, {
onSuccess: (data) => { onSuccess: (data) => {
queryClient.invalidateQueries(['messages']); queryClient.invalidateQueries(['messages']);
isLiked ? toastSuccess('Message aimé') : null; if (data.message === 'Post liked') {
setLiked(true);
toastSuccess('Message aimé');
} else {
setLiked(false);
toastSuccess('Message non aimé');
}
toastSuccess(data.message);
}, },
onError: (error) => { onError: (error) => {
toastError(error as string); toastError(error as string);
@ -17,7 +41,7 @@ const Like = ({ messageId, isLiked }: { messageId: string; isLiked: boolean }) =
}); });
const like = () => { const like = () => {
mutateLike.mutate(messageId); mutateLike.mutate(message.id);
}; };
return ( return (
@ -26,7 +50,7 @@ const Like = ({ messageId, isLiked }: { messageId: string; isLiked: boolean }) =
onClick={like} onClick={like}
name="like" name="like"
> >
<FaThumbsUp className={'fill-red-light text-xl w-10 h-10 p-2.5' + (isLiked ? ' fill-red' : '')} /> <FaThumbsUp className={'fill-red-light text-xl w-10 h-10 p-2.5' + (liked ? ' fill-red' : '')} />
<span className="sr-only">Aimer</span> <span className="sr-only">Aimer</span>
</button> </button>
); );

View File

@ -6,6 +6,7 @@ import { useQuery } from '@tanstack/react-query';
import { toastError } from '@controllers/Toasts'; import { toastError } from '@controllers/Toasts';
import User from './User'; import User from './User';
import Image from './Image'; import Image from './Image';
import { useState } from 'react';
const Text = ({ text }: { text: string }) => { const Text = ({ text }: { text: string }) => {
if (text === '') { if (text === '') {
@ -52,14 +53,7 @@ const Message = ({ message }: any) => {
</div> </div>
{message.edited && <div className="text-grey-light italic">Modifié</div>} {message.edited && <div className="text-grey-light italic">Modifié</div>}
</div> </div>
{me.data?.id === message.author.id ? null : ( {me.data?.id === message.author.id ? null : <Like message={message} />}
<Like
messageId={message.id}
isLiked={
message.likes > 0 && message.likedBy.find((like: any) => like.userId === me.data?.id) ? true : false
}
/>
)}
</div> </div>
</div> </div>
</> </>