fix problem with like increment

This commit is contained in:
Guillaume Dorce 2022-11-04 14:26:19 +01:00
parent 5889a81358
commit 55bae65444
3 changed files with 11 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import { toastError, toastSuccess } from '@controllers/Toasts';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { getMeInfo } from '@controllers/UserController'; import { getMeInfo } from '@controllers/UserController';
const Like = ({ message }: { message: any}) => { const Like = ({ message }: { message: any }) => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [liked, setLiked] = useState(false); const [liked, setLiked] = useState(false);
const me = useQuery(['me'], getMeInfo, { const me = useQuery(['me'], getMeInfo, {
@ -20,7 +20,7 @@ const Like = ({ message }: { message: any}) => {
useEffect(() => { useEffect(() => {
if (message.likedBy.some((like: any) => like.userId === me.data?.id)) { if (message.likedBy.some((like: any) => like.userId === me.data?.id)) {
setLiked(true); setLiked(true);
} }
}, [message]); }, [message]);
const mutateLike = useMutation(liked ? unlikePost : likePost, { const mutateLike = useMutation(liked ? unlikePost : likePost, {
@ -28,10 +28,11 @@ const Like = ({ message }: { message: any}) => {
queryClient.invalidateQueries(['messages']); queryClient.invalidateQueries(['messages']);
if (data.message === 'Post liked') { if (data.message === 'Post liked') {
setLiked(true); setLiked(true);
toastSuccess('Message aimé'); toastSuccess('Message liké');
} else { } else if (data.message === 'Post unliked') {
setLiked(false); setLiked(false);
toastSuccess('Message non aimé'); } else {
toastError(data.message);
} }
}, },
onError: (error) => { onError: (error) => {
@ -50,7 +51,11 @@ const Like = ({ message }: { message: any}) => {
name="like" name="like"
> >
<FaThumbsUp className={'fill-red-light text-xl w-10 h-10 p-2.5' + (liked ? ' fill-red' : '')} /> <FaThumbsUp className={'fill-red-light text-xl w-10 h-10 p-2.5' + (liked ? ' fill-red' : '')} />
{message.likedBy.length > 0 && <span className="absolute -top-2 right-0 text-white rounded-full bg-red w-5 h-5 text-xs text-center p-0">{message.likedBy.length}</span>} {message.likes > 0 && (
<span className="absolute -top-2 right-0 text-white rounded-full bg-red w-5 h-5 text-xs text-center p-0">
{message.likes}
</span>
)}
<span className="sr-only">Aimer</span> <span className="sr-only">Aimer</span>
</button> </button>
); );

View File

@ -10,8 +10,6 @@ export default async (req: Request, res: Response) => {
} }
return res.status(200).send({ message: 'Post unliked' }); return res.status(200).send({ message: 'Post unliked' });
} catch (error) { } catch (error) {
console.log(error);
return res.status(500).send(error); return res.status(500).send(error);
} }
}; };

View File

@ -190,11 +190,6 @@ const likePost = async (id: number, userId: number): Promise<PrismaPost | Error>
id, id,
}, },
data: { data: {
likedBy: {
connect: {
id: newLike.id,
},
},
likes: { likes: {
increment: 1, increment: 1,
}, },