diff --git a/public/ImageUsers/642c38847f22b339adbf2c16_1680620008823.jpg b/public/ImageUsers/642c38847f22b339adbf2c16_1680620008823.jpg new file mode 100644 index 0000000..f12e4c5 Binary files /dev/null and b/public/ImageUsers/642c38847f22b339adbf2c16_1680620008823.jpg differ diff --git a/public/ImageUsers/642c38847f22b339adbf2c16_1680620012000.jpg b/public/ImageUsers/642c38847f22b339adbf2c16_1680620012000.jpg new file mode 100644 index 0000000..abba647 Binary files /dev/null and b/public/ImageUsers/642c38847f22b339adbf2c16_1680620012000.jpg differ diff --git a/public/ImageUsers/642c46007f22b339adbf2d0d_1680623175469.webp b/public/ImageUsers/642c46007f22b339adbf2d0d_1680623175469.webp new file mode 100644 index 0000000..0e715ed Binary files /dev/null and b/public/ImageUsers/642c46007f22b339adbf2d0d_1680623175469.webp differ diff --git a/public/ImageUsers/642c46007f22b339adbf2d0d_1680623178867.jpg b/public/ImageUsers/642c46007f22b339adbf2d0d_1680623178867.jpg new file mode 100644 index 0000000..2e3184a Binary files /dev/null and b/public/ImageUsers/642c46007f22b339adbf2d0d_1680623178867.jpg differ diff --git a/src/components/layout/dashboard/card_user/CardUser.jsx b/src/components/layout/dashboard/card_user/CardUser.jsx index 104e385..cf3605d 100644 --- a/src/components/layout/dashboard/card_user/CardUser.jsx +++ b/src/components/layout/dashboard/card_user/CardUser.jsx @@ -11,41 +11,75 @@ import { import Carousel from "../../../Carousel"; import { BiHeart } from "react-icons/bi"; import { RxCross1 } from "react-icons/rx"; +import { useQuery } from "@tanstack/react-query"; import PassionTagList from "@/components/layout/dashboard/card_user/PassionTagList"; +import { useState } from "react"; export default function CardUser(props) { - const { user } = props; + const { user, loggedUser, userLiked, setLiked, userDisliked, setDisliked } = + props; - const interestingUser = { - lastName: "dujardin", - firstName: "jean", - age: 19, - aPropos: "Je suis une personne fictive, pas tres fictive", - images: ["/401446.webp", "/135538.webp"], - passions: ["Sport", "Piscine", "Formule1"], + const [hidden, setHidden] = useState(false); + + const { + isLoading: passionLoading, + isError: passionIsError, + data: listPassions, + error: passionError, + } = useQuery({ + queryKey: ["passions"], + queryFn: async () => { + return fetch(`/api/passions/`) + .then((res) => res.json()) + .catch((err) => { + return err; + }); + }, + }); + + const formateDateToAge = (birthdate) => { + const date = new Date(birthdate); + var ageDifMs = Date.now() - date.getTime(); + var ageDate = new Date(ageDifMs); // miliseconds from epoch + return Math.abs(ageDate.getUTCFullYear() - 1970); }; return ( - + diff --git a/src/components/layout/dashboard/card_user/PassionTagList.tsx b/src/components/layout/dashboard/card_user/PassionTagList.tsx index 93c7d94..da7d9f3 100644 --- a/src/components/layout/dashboard/card_user/PassionTagList.tsx +++ b/src/components/layout/dashboard/card_user/PassionTagList.tsx @@ -3,18 +3,23 @@ import { Badge, Flex, Tag } from "@chakra-ui/react"; type Props = { passions: string[]; userPassions?: string[]; + listPassions?: Object[]; }; -export default function PassionTagList({ passions, userPassions = [] }: Props) { +export default function PassionTagList({ + passions, + userPassions = [], + listPassions, +}: Props) { return ( - - {passions.map((passion, index) => ( + + {passions.map((passionID, index) => ( - {passion} + {listPassions?.find((passion) => passion.id === passionID)?.name} ))} diff --git a/src/components/layout/dashboard/card_user/SearchFailCard.jsx b/src/components/layout/dashboard/card_user/SearchFailCard.jsx new file mode 100644 index 0000000..08306de --- /dev/null +++ b/src/components/layout/dashboard/card_user/SearchFailCard.jsx @@ -0,0 +1,14 @@ +import { Card, Text, CardBody, CardHeader } from "@chakra-ui/react"; + +export default function SearchFailCard(props) { + return ( + + Aucun profil correspondant à vos critères + + + Modifiez vos critères de recherche ou attendez de nouveaux inscrits + + + + ); +} diff --git a/src/components/layout/dashboard/left_panel/LeftPanel.jsx b/src/components/layout/dashboard/left_panel/LeftPanel.jsx index 6bc229d..120d1dd 100644 --- a/src/components/layout/dashboard/left_panel/LeftPanel.jsx +++ b/src/components/layout/dashboard/left_panel/LeftPanel.jsx @@ -36,7 +36,11 @@ export default function LeftPanel(props) { > - + {user.images.length === 0 ? ( + + ) : ( + + )} diff --git a/src/pages/api/user/userDashboard.js b/src/pages/api/user/userDashboard.js new file mode 100644 index 0000000..5603ff4 --- /dev/null +++ b/src/pages/api/user/userDashboard.js @@ -0,0 +1,22 @@ +const { PrismaClient } = require("@prisma/client"); + +const get = async (req, res) => { + const { preference, excludedId } = req.query; + const prisma = new PrismaClient(); + // a terme mettre l'age, la distance + const users = await prisma.user.findMany({ + where: { + AND: [ + { gender: { equals: preference } }, + { images: { isEmpty: false }, NOT: { id: { equals: excludedId } } }, + ], + }, + }); + res.status(200).send(users); +}; + +export default (req, res) => { + req.method === "GET" + ? get(req, res) + : res.status(404).send({ message: "Wrong method, please use GET" }); +}; diff --git a/src/pages/dashboard.tsx b/src/pages/dashboard.tsx index 79c566a..e9b9090 100644 --- a/src/pages/dashboard.tsx +++ b/src/pages/dashboard.tsx @@ -1,57 +1,118 @@ -import { Grid, GridItem, Text, Box } from "@chakra-ui/react"; +import { Grid, GridItem, Text, Box, useToast } from "@chakra-ui/react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; +import { useState } from "react"; import type { Session } from "@/models/auth/Session"; import CardUser from "../components/layout/dashboard/card_user/CardUser"; +import SearchFailCard from "../components/layout/dashboard/card_user/SearchFailCard"; + import LeftPanel from "../components/layout/dashboard/left_panel/LeftPanel"; import Head from "next/head"; import { websiteName } from "@/lib/constants"; -import Navbar from "@/components/Navbar"; +import { useQuery } from "@tanstack/react-query"; +import LoadingPage from "@/components/LoadingPage"; export default function Dashboard() { const router = useRouter(); + const toast = useToast({ position: "top", isClosable: true }); + const [liked, setLiked] = useState(null); + const [disliked, setDisliked] = useState(null); + const { data: session, status } = useSession(); - if (status === "loading") return Loading...; - if (status === "unauthenticated") router.push("/login"); + const { + isLoading, + isError, + data: loggedUser, + error, + } = useQuery({ + queryKey: ["LoggedUser"], + enabled: status === "authenticated", + queryFn: async () => { + const { user } = session as unknown as Session; - if (status === "authenticated") { - const { user } = session as unknown as Session; + return fetch(`/api/users/${user.id}`) + .then((res) => res.json()) + .catch((err) => { + return err; + }); + }, + }); - // il faudra l'enlever - const refinedUser = { - ...user, - age: 21, - aPropos: "Je suis la personne fictive la plus fictive", - images: ["135538.webp"], - passions: ["Sport", "Voiture", "Cuisine"], - }; + const { + data: listUsers, + isError: isErrorListUsers, + isLoading: isLoadingListUsers, + error: errorListUsers, + } = useQuery({ + queryKey: ["ListUsers"], + enabled: status === "authenticated" && !isLoading, + queryFn: async () => { + return fetch( + `/api/user/userDashboard?preference=${loggedUser.gender}&excludedId=${loggedUser.id}` + ) //exclure les profils déjà like ou dislike + .then((res) => res.json()) + .catch((err) => { + return err; + }); + }, + }); - return ( - <> - - {websiteName} | Dashboard - - - - - - - - - - - - {/*Right top*/} - {/*Right Bottom*/} - - - ); + if (isLoading) { + if (status === "unauthenticated") router.push("/login"); + return ; } + + if (isError) { + toast({ + title: `Erreur lors de la récupération des données du profil`, + status: "error", + position: "top", + }); + if (status === "unauthenticated") router.push("/"); + return Error: {error.message}; + } + + return ( + <> + + {websiteName} | Dashboard + + + + + + + + + {isLoadingListUsers ? ( + + ) : listUsers.length === 0 && !isErrorListUsers ? ( + + ) : ( + listUsers.map((user: any) => ( + + )) + )} + + + {/*Right top*/} + {/*Right Bottom*/} + + + ); } diff --git a/src/pages/userProfile.tsx b/src/pages/userProfile.tsx index b7609fc..45d4129 100644 --- a/src/pages/userProfile.tsx +++ b/src/pages/userProfile.tsx @@ -57,7 +57,6 @@ export default function UserProfile() { console.log(err); }); }, []); - // faire un useEffect ou je fetch user const { data: session, status } = useSession(); @@ -170,21 +169,11 @@ export default function UserProfile() { > )} - {/* {modal} */} - - {!userData.images ? ( - - ) : ( - - )} + @@ -267,7 +256,8 @@ export default function UserProfile() { color={"grey"} isDisabled={true} defaultValue={ - userData.birthdate === undefined + userData.birthdate === undefined || + userData.birthdate === null ? "Non renseigné" : formateDate(userData.birthdate.toString()) }