diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..44aeb40 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules\\typescript\\lib" +} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index aca04a2..7c17614 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -40,9 +40,17 @@ model User { UserLikes User[] @relation("Likes", fields: [UserLikesID], references: [id]) // Les personnes qui aiment l'utilisateur - OtherUserLikesID String[] @db.ObjectId + OtherUserLikesID String[] @db.ObjectId OtherUserLikes User[] @relation("Likes", fields: [OtherUserLikesID], references: [id]) + // Les personnes que l'utilisateur a dislike + UserDislikesID String[] @db.ObjectId + UserDislikes User[] @relation("Dislikes", fields: [UserDislikesID], references: [id]) + + // Les personnes qui aiment l'utilisateur + OtherUserDislikesID String[] @db.ObjectId + OtherUserDislikes User[] @relation("Dislikes", fields: [OtherUserDislikesID], references: [id]) + MatchID String[] @db.ObjectId Match Match[] @relation(fields: [MatchID], references: [id]) diff --git a/src/components/layout/dashboard/card_user/CardUser.jsx b/src/components/layout/dashboard/card_user/CardUser.jsx index cf3605d..291a9a7 100644 --- a/src/components/layout/dashboard/card_user/CardUser.jsx +++ b/src/components/layout/dashboard/card_user/CardUser.jsx @@ -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 ; + } + + if (likeMutation.isLoading || dislikeMutation.isLoading) { + return ; + } + return ( -