⚡️ add - api generationGain , api superadmin , readme , fichier de seed

This commit is contained in:
eric committed 2022-11-12 20:23:15 +01:00
1 parent 141d9e53e8
commit e85117b2c7
25 files changed
+768 -310

No files matched your search

+82 -57
View File
@@ -3,30 +3,54 @@ 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";
import { Resolver, SubmitHandler } from "react-hook-form";
//https://www.bezkoder.com/react-hook-form-typescript/
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import * as Yup from 'yup';
type UserSubmitForm = {
bio: string;
};
type Profil = {
isUserProfile: any;
}
interface ResponseData {
result: string;
message: string
}
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
const session = await getSession({ req });
//pas de session ? on ne retourne un code erreur
//voir pour rediriger vers la page de connection
//ou afficher pop up "vous n'est pas connecter"
if (!session) {
res.statusCode = 403;
return { props: { profile: null } };
}
// si profil ? on retour le Profil
// si session ? on récupére le profil de l'utilisateur si il existe
if (session) {
const isUserProfile = await prisma.profile.findMany({
where: {
user: { email: session.user.email },
}
})
console.log('profil utilisateur ?', isUserProfile);
if (!isUserProfile) {
const createProfile = await prisma.profile.create({
// il n'existe pas ? on le crée
if (isUserProfile.length <= 0) {
const newProfil = await prisma.profile.create({
data: {
bio: "",
user: {
@@ -34,48 +58,40 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
},
},
});
return { props: { createProfile } };
console.log('newProfil', newProfil);
}
// on renvoi le profil
return { props: { isUserProfile } };
}
};
// type Props = {
// profile: any;
// };
interface ResponseData {
result: string;
message: string
}
const Profil = ({ isUserProfile }: Profil) => {
type FormValues = {
bio: string;
};
const validationSchema = Yup.object().shape({
bio: Yup.string().trim().required("ce champ ne peux etre vide..."),
});
const {
register,
handleSubmit,
reset,
formState: { errors }
} = useForm<UserSubmitForm>({
resolver: yupResolver(validationSchema)
});
const Profil = (props: any) => {
const { register, handleSubmit, formState: { errors } } = useForm<FormValues>({ resolver });
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)
console.log('user profil props', isUserProfile)
}, [])
const onSubmit: SubmitHandler<FormValues> = async (data) => {
console.log('input value', data.bio);
setBio(data.bio)
const onSubmit = async (data: UserSubmitForm) => {
console.log(data);
try {
const body = { bioValue: data.bio };
@@ -89,8 +105,6 @@ const Profil = (props: any) => {
setResponse(data);
console.log('response', data);
})
await Router.push("/profile");
} catch (error) {
console.error(error);
@@ -111,36 +125,47 @@ const Profil = (props: any) => {
<div className="page">
<h1>Mon compte</h1>
<main>
<form onSubmit={handleSubmit(onSubmit)}>
<p>Ma bio : {bio}</p>
<input
autoFocus
placeholder="Ma bio..."
defaultValue={bio}
{...register("bio")}
/>
<input type="submit" value="Save changes" />
</form>
<div className="register-form">
<p>Ma bio : {isUserProfile[0]?.bio}</p>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="form-group">
<label>Ma bio :</label>
<input
placeholder="Ma bio..."
defaultValue={isUserProfile[0]?.bio}
type="text"
{...register('bio')}
className={`form-control ${errors.bio ? 'is-invalid' : ''}`}
/>
<div className="invalid-feedback">{errors.bio?.message}</div>
</div>
<div className="form-group">
<button type="submit" className="btn btn-primary">
Valider les changements
</button>
<button
type="button"
onClick={() => reset()}
className="btn btn-warning float-right"
>
Reset
</button>
</div>
</form>
</div>
<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;