diff --git a/src/components/layout/dashboard/match_notification/ModalMatch.tsx b/src/components/layout/dashboard/match_notification/ModalMatch.tsx index 137f992..89ee90d 100644 --- a/src/components/layout/dashboard/match_notification/ModalMatch.tsx +++ b/src/components/layout/dashboard/match_notification/ModalMatch.tsx @@ -22,6 +22,9 @@ import { formateDate } from "@/lib/formateDate"; import { Notification } from "@prisma/client"; import PassionTagList from "../card_user/PassionTagList"; +import { useRef } from "react"; +import { useRouter } from "next/router"; + type modalMatchProps = { key: string; loggedUser: User; @@ -29,16 +32,25 @@ type modalMatchProps = { }; export default function ModalMatch({ notif, loggedUser }: modalMatchProps) { + const salute = useRef(false); + const router = useRouter(); + const handleClose = () => { const options = { method: "PATCH", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ notificationId: notif.id }), + body: JSON.stringify({ + notificationId: notif.id, + salute: salute.current, + }), }; fetch(`/api/user/consultNotification`, options) .then((res) => res.json()) + .then((data) => { + router.push(`chat/${data.chat.id}`); + }) .catch((err) => console.log(err)); }; @@ -85,7 +97,7 @@ export default function ModalMatch({ notif, loggedUser }: modalMatchProps) { return ( <> {matchedUser && ( - + Vous avez un nouveau match @@ -132,7 +144,10 @@ export default function ModalMatch({ notif, loggedUser }: modalMatchProps) { diff --git a/src/components/layout/map/ModalInviteBar.tsx b/src/components/layout/map/ModalInviteBar.tsx index 8efd495..171374b 100644 --- a/src/components/layout/map/ModalInviteBar.tsx +++ b/src/components/layout/map/ModalInviteBar.tsx @@ -22,8 +22,7 @@ import { useMutation, useQuery } from "@tanstack/react-query"; import { useSession } from "next-auth/react"; import type { Session } from "@/models/auth/Session"; import CardMatchedUser from "./CardMatchedUser"; -import { Router } from "next/router"; -import { log } from "console"; +import { useRouter } from "next/router"; type ModalInviteBarProps = { isOpen: boolean; @@ -38,6 +37,7 @@ export default function ModalInviteBar({ }: ModalInviteBarProps) { const { data: session, status } = useSession(); const toast = useToast(); + const router = useRouter(); const { value, getRadioProps, getRootProps } = useRadioGroup({ defaultValue: "-1", @@ -73,7 +73,7 @@ export default function ModalInviteBar({ }); return; } - console.log(data); + router.push(`/chat/${data.message_sent.ChatID}`); }, }); @@ -100,8 +100,6 @@ export default function ModalInviteBar({ }, }); - console.log(bar); - return ( <> Inviter diff --git a/src/pages/api/user/consultNotification.ts b/src/pages/api/user/consultNotification.ts index 3fd4860..dcff551 100644 --- a/src/pages/api/user/consultNotification.ts +++ b/src/pages/api/user/consultNotification.ts @@ -3,7 +3,7 @@ const { PrismaClient } = require("@prisma/client"); const patch = async (req: any, res: any) => { const prisma = new PrismaClient(); - const { notificationId } = req.body; + const { notificationId, salute } = req.body; try { const notification = await prisma.notification.update({ @@ -15,7 +15,31 @@ const patch = async (req: any, res: any) => { }, }); - res.status(200).json(notification); + let chat = null; + if (!salute) { + res.status(200).json({ + message: "notification has been consulted", + notification: notification, + }); + } else { + chat = await prisma.chat.findFirst({ + where: { + AND: [ + { User: { some: { id: notification.MatchedUserID } } }, + { User: { some: { id: notification.UserID } } }, + ], + }, + include: { + User: true, + }, + }); + + res.status(200).json({ + message: "notification has been consulted", + notification: notification, + chat: chat, + }); + } } catch (error) { res.status(400).json({ message: "Something went wrong" }); } finally {