import React, { useState } from "react"; import { GetServerSideProps } from "next"; import Layout from "../components/Layout"; import { useSession, getSession } from "next-auth/react"; import prisma from '../lib/prisma' import Image from "next/image"; import Router from "next/router"; import { ProfileProps } from "../types/profile"; import { Resolver, SubmitHandler, useForm } from "react-hook-form"; export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { const session = await getSession({ req }); if (!session) { res.statusCode = 403; return { props: { profile: null } }; } // si profil ? on retour le Profil if (session) { const isUserProfile = await prisma.profile.findMany({ where: { user: { email: session.user.email }, } }) if (!isUserProfile) { const createProfile = await prisma.profile.create({ data: { bio: "", user: { connect: { email: session.user.email }, }, }, }); return { props: { createProfile } }; } return { props: { isUserProfile } }; } }; // type Props = { // profile: any; // }; interface ResponseData { result: string; message: string } type FormValues = { bio: string; }; const Profil = (props: any) => { const { register, handleSubmit, formState: { errors } } = useForm({ resolver }); const [bio, setBio] = useState(props.isUserProfile[0].bio || ""); const [response, setResponse] = useState(); const { data: session } = useSession(); React.useEffect(() => { console.log('user profil', props.isUserProfile) }, []) const onSubmit: SubmitHandler = async (data) => { console.log('input value', data.bio); setBio(data.bio) try { const body = { bioValue: data.bio }; await fetch(`/api/profile`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }) .then((res) => res.json()) .then((data) => { setResponse(data); console.log('response', data); }) await Router.push("/profile"); } catch (error) { console.error(error); } }; if (!session) { return (

Mon Compte

Vous devez être authentifié pour voir cette page.
); } return (

Mon compte

Ma bio : {bio}

Email : {session.user.email}

{response &&

{response.message}

}
); }; export default Profil;