⚡️ ⚡️ add - ajout d'une page mon compte connecter a la bdd + API + routes
This commit is contained in:
1 parent
ae437a097b
commit
38159b90c1
12 files changed
+303
-16
No files matched your search
@@ -0,0 +1,139 @@
|
||||
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";
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
const Profil = (props: any) => {
|
||||
const [bio, setBio] = useState(props.isUserProfile[0].bio || "");
|
||||
const [response, setResponse] = useState<ResponseData | undefined>();
|
||||
const { data: session } = useSession();
|
||||
|
||||
React.useEffect(() => {
|
||||
console.log('user profil', props.isUserProfile)
|
||||
}, [])
|
||||
|
||||
|
||||
|
||||
|
||||
const submitData = async (e: React.SyntheticEvent) => {
|
||||
e.preventDefault();
|
||||
console.log('input value', e.target[0].value);
|
||||
setBio(e.target[0].value)
|
||||
|
||||
try {
|
||||
const body = { bioValue: e.target[0].value };
|
||||
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 (
|
||||
<Layout>
|
||||
<h1>Mon Compte</h1>
|
||||
<div>Vous devez être authentifié pour voir cette page.</div>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<div className="page">
|
||||
<h1>Mon compte</h1>
|
||||
<main>
|
||||
<form onSubmit={submitData}>
|
||||
<p>Ma bio : {bio}</p>
|
||||
<input
|
||||
autoFocus
|
||||
placeholder="Ma bio..."
|
||||
type="text"
|
||||
defaultValue={bio}
|
||||
/>
|
||||
<input type="submit" value="Save changes" />
|
||||
</form>
|
||||
<p>Email : {session.user.email}</p>
|
||||
{response && <p>{response.message}</p>}
|
||||
</main>
|
||||
</div>
|
||||
<style jsx>{`
|
||||
.post {
|
||||
background: white;
|
||||
transition: box-shadow 0.1s ease-in;
|
||||
}
|
||||
|
||||
.post:hover {
|
||||
box-shadow: 1px 1px 3px #aaa;
|
||||
}
|
||||
|
||||
.post + .post {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
`}</style>
|
||||
</Layout>
|
||||
);
|
||||
};
|
||||
|
||||
export default Profil;
|
||||
Reference in new issue
Block a user