mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-12-05 17:30:50 +00:00
feat(like / dislike): Added possibility to like and dislike
This commit is contained in:
@@ -7,19 +7,118 @@ import {
|
||||
Heading,
|
||||
Box,
|
||||
CardHeader,
|
||||
useToast,
|
||||
} from "@chakra-ui/react";
|
||||
import Carousel from "../../../Carousel";
|
||||
import { BiHeart } from "react-icons/bi";
|
||||
import { RxCross1 } from "react-icons/rx";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import PassionTagList from "@/components/layout/dashboard/card_user/PassionTagList";
|
||||
import { useState } from "react";
|
||||
import SearchFailCard from "./SearchFailCard";
|
||||
import LoadingPage from "@/components/LoadingPage";
|
||||
|
||||
export default function CardUser(props) {
|
||||
const { user, loggedUser, userLiked, setLiked, userDisliked, setDisliked } =
|
||||
props;
|
||||
const {
|
||||
users,
|
||||
loggedUser,
|
||||
userLikes,
|
||||
setUserLikes,
|
||||
userDislikes,
|
||||
setUserDislikes,
|
||||
} = props;
|
||||
|
||||
const [hidden, setHidden] = useState(false);
|
||||
console.log(userLikes);
|
||||
console.log(userDislikes);
|
||||
|
||||
const toast = useToast({
|
||||
position: "top",
|
||||
duration: 3000,
|
||||
isClosable: true,
|
||||
});
|
||||
const [listUsers, setListUsers] = useState(users);
|
||||
|
||||
const likeMutation = useMutation({
|
||||
mutationKey: "like",
|
||||
mutationFn: async (id) => {
|
||||
return fetch(`/api/users/${loggedUser.id}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ UserLikesID: [...userLikes, id] }),
|
||||
})
|
||||
.then((res) => {
|
||||
setUserLikes([...userLikes, user.id]);
|
||||
toast({
|
||||
title: "J'aime",
|
||||
description: "Votre action a bien été prise en compte",
|
||||
status: "success",
|
||||
});
|
||||
res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
return err;
|
||||
});
|
||||
},
|
||||
onError: (err) => {
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Une erreur est survenue",
|
||||
status: "error",
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
toast({
|
||||
title: "J'aime",
|
||||
description: "Votre action a bien été prise en compte",
|
||||
status: "success",
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const dislikeMutation = useMutation({
|
||||
mutationKey: "dislike",
|
||||
mutationFn: async (id) => {
|
||||
return fetch(`/api/users/${loggedUser.id}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ UserDislikesID: [...userDislikes, id] }),
|
||||
})
|
||||
.then((res) => {
|
||||
setUserDislikes([...userDislikes, user.id]);
|
||||
toast({
|
||||
title: "J'aime pas",
|
||||
description: "Votre action a bien été prise en compte",
|
||||
status: "success",
|
||||
});
|
||||
res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
return err;
|
||||
});
|
||||
},
|
||||
onError: (err) => {
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Une erreur est survenue",
|
||||
status: "error",
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
toast({
|
||||
title: "J'aime pas",
|
||||
description: "Votre action a bien été prise en compte",
|
||||
status: "success",
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const {
|
||||
isLoading: passionLoading,
|
||||
@@ -44,24 +143,25 @@ export default function CardUser(props) {
|
||||
return Math.abs(ageDate.getUTCFullYear() - 1970);
|
||||
};
|
||||
|
||||
if (listUsers.length === 0) {
|
||||
return <SearchFailCard />;
|
||||
}
|
||||
|
||||
if (likeMutation.isLoading || dislikeMutation.isLoading) {
|
||||
return <LoadingPage />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Card
|
||||
// position={"absolute"}
|
||||
w={"100%"}
|
||||
h={"100%"}
|
||||
borderRadius={"1rem"}
|
||||
overflow={"hidden"}
|
||||
hidden={hidden}
|
||||
>
|
||||
<Card w={"100%"} h={"100%"} borderRadius={"1rem"} overflow={"hidden"}>
|
||||
<CardHeader>
|
||||
<Carousel borderRadius={"1rem"} images={user.images} />
|
||||
<Carousel borderRadius={"1rem"} images={listUsers[0].images} />
|
||||
</CardHeader>
|
||||
|
||||
<CardBody>
|
||||
<Flex justify={"space-between"} mb={"20px"}>
|
||||
<Heading fontSize={"1.5rem"} fontWeight={"bold"} flexBasis={"70%"}>
|
||||
{user.firstName} {user.lastName}, {formateDateToAge(user.birthdate)}{" "}
|
||||
ans
|
||||
{listUsers[0].firstName} {listUsers[0].lastName},{" "}
|
||||
{formateDateToAge(listUsers[0].birthdate)} ans
|
||||
</Heading>
|
||||
|
||||
<Flex gap={1}>
|
||||
@@ -69,8 +169,8 @@ export default function CardUser(props) {
|
||||
borderRadius={"1rem"}
|
||||
colorScheme={"purple"}
|
||||
onClick={() => {
|
||||
// setLiked([...userLiked, user.id]);
|
||||
setHidden(true);
|
||||
likeMutation.mutate(listUsers[0].id);
|
||||
setListUsers(listUsers.slice(1));
|
||||
}}
|
||||
>
|
||||
<BiHeart />
|
||||
@@ -79,7 +179,10 @@ export default function CardUser(props) {
|
||||
borderRadius={"1rem"}
|
||||
colorScheme={"purple"}
|
||||
variant={"outline"}
|
||||
// onClick={setDisiked([...userDisliked, user.id])}
|
||||
onClick={() => {
|
||||
dislikeMutation.mutate(listUsers[0].id);
|
||||
setListUsers(listUsers.slice(1));
|
||||
}}
|
||||
>
|
||||
<RxCross1 />
|
||||
</IconButton>
|
||||
@@ -90,7 +193,7 @@ export default function CardUser(props) {
|
||||
<Heading size={"sm"} fontWeight={"bold"} mb="0.5rem">
|
||||
A propos :
|
||||
</Heading>
|
||||
<Text as="i">"{user.bio}"</Text>
|
||||
<Text as="i">"{listUsers[0].bio}"</Text>
|
||||
</Box>
|
||||
|
||||
<Box>
|
||||
@@ -103,7 +206,7 @@ export default function CardUser(props) {
|
||||
<Text>Erreur lors du chargement des passions</Text>
|
||||
) : (
|
||||
<PassionTagList
|
||||
passions={user.PassionID}
|
||||
passions={listUsers[0].PassionID}
|
||||
userPassions={loggedUser.PassionID}
|
||||
listPassions={listPassions}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user