diff --git a/.vscode/settings.json b/.vscode/settings.json index 7a73a41..297b8d0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,2 +1,7 @@ { + "workbench.colorCustomizations": { + "activityBar.background": "#4C1F25", + "titleBar.activeBackground": "#6A2C34", + "titleBar.activeForeground": "#FEFCFC" + } } \ No newline at end of file diff --git a/pages/_fichier_exemple_/api/post/[id].ts b/pages/_fichier_exemple_/api/post/[id].ts deleted file mode 100644 index c25f56c..0000000 --- a/pages/_fichier_exemple_/api/post/[id].ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next' -import { getSession } from 'next-auth/react'; -import prisma from '../../../../lib/prisma' - - -// DELETE /api/post/:id -export default async function handle(req: NextApiRequest, res: NextApiResponse) { - const postId = req.query.id; - - const session = await getSession({ req }) - - if (req.method === "DELETE") { - if (session) { - const post = await prisma.post.delete({ - where: { id: Number(postId) }, - }); - res.json(post); - } else { - res.status(401).send({ message: 'Unauthorized' }) - } - } else { - throw new Error( - `The HTTP ${req.method} method is not supported at this route.` - ); - } -} diff --git a/pages/_fichier_exemple_/api/post/index.ts b/pages/_fichier_exemple_/api/post/index.ts deleted file mode 100644 index 3f8b12e..0000000 --- a/pages/_fichier_exemple_/api/post/index.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next' -import prisma from '../../../../lib/prisma' -import { getSession } from 'next-auth/react' - - -// POST /api/post -// Required fields in body: title -// Optional fields in body: content -export default async function handle(req: NextApiRequest, res: NextApiResponse) { - const { title, content } = req.body; - - const session = await getSession({ req }); - if (session) { - const result = await prisma.post.create({ - data: { - title: title, - content: content, - author: { connect: { email: session?.user?.email } }, - }, - }); - res.json(result); - } else { - res.status(401).send({ message: 'Unauthorized' }) - } -} diff --git a/pages/_fichier_exemple_/api/profile/index.ts b/pages/_fichier_exemple_/api/profile/index.ts deleted file mode 100644 index 2d03ede..0000000 --- a/pages/_fichier_exemple_/api/profile/index.ts +++ /dev/null @@ -1,34 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next' -import prisma from '../../../../lib/prisma' -import { getSession } from 'next-auth/react' - - -// UPDATE /api/profile -// Required fields in body: title -export default async function handle(req: NextApiRequest, res: NextApiResponse) { - const { bioValue } = req.body; - console.log('server biovalue:', bioValue) - const session = await getSession({ req }); - - if (req.method === "PUT") { - if (session) { - const updateUserProfil = await prisma.user.update({ - where: { email: session.user.email }, - data: { - profile: { - update: { - bio: bioValue, - }, - }, - } - }) - res.status(200).send({ result: updateUserProfil, message: 'Profiled updated !' }); - } else { - res.status(401).send({ message: 'Unauthorized' }) - } - } else { - throw new Error( - `The HTTP ${req.method} method is not supported at this route.` - ); - } -} \ No newline at end of file diff --git a/pages/_fichier_exemple_/api/publish/[id].ts b/pages/_fichier_exemple_/api/publish/[id].ts deleted file mode 100644 index 3d1f064..0000000 --- a/pages/_fichier_exemple_/api/publish/[id].ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next' -import { getSession } from 'next-auth/react'; -import prisma from '../../../../lib/prisma' - -// PUT /api/publish/:id -export default async function handle(req: NextApiRequest, res: NextApiResponse) { - const postId = req.query.id; - const session = await getSession({ req }) - - if (session) { - const post = await prisma.post.update({ - where: { id: Number(postId) }, - data: { published: true }, - }); - res.json(post); - } else { - res.status(401).send({ message: 'Unauthorized' }) - } -} diff --git a/pages/_fichier_exemple_/create.tsx b/pages/_fichier_exemple_/create.tsx deleted file mode 100644 index d9f8b1a..0000000 --- a/pages/_fichier_exemple_/create.tsx +++ /dev/null @@ -1,81 +0,0 @@ -import React, { useState } from "react"; -import Layout from "../../components/Layout"; -import Router from "next/router"; - -const Draft = () => { - const [title, setTitle] = useState(""); - const [content, setContent] = useState(""); - - const submitData = async (e: React.SyntheticEvent) => { - e.preventDefault(); - try { - const body = { title, content }; - await fetch(`/api/post`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }); - await Router.push("/drafts"); - } catch (error) { - console.error(error); - } - }; - - return ( - - - - New Draft - setTitle(e.target.value)} - placeholder="Title" - type="text" - value={title} - /> - setContent(e.target.value)} - placeholder="Content" - rows={8} - value={content} - /> - - Router.push("/")}> - or Cancel - - - - - - ); -}; - -export default Draft; diff --git a/pages/_fichier_exemple_/drafts.tsx b/pages/_fichier_exemple_/drafts.tsx deleted file mode 100644 index e89283a..0000000 --- a/pages/_fichier_exemple_/drafts.tsx +++ /dev/null @@ -1,78 +0,0 @@ -import React from "react"; -import { GetServerSideProps } from "next"; -import Layout from "../../components/Layout"; -import Post, { PostProps } from "../../components/Post"; -import { useSession, getSession } from "next-auth/react"; -import prisma from '../../lib/prisma' - - -export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { - const session = await getSession({ req }); - if (!session) { - res.statusCode = 403; - return { props: { drafts: [] } }; - } - - const drafts = await prisma.post.findMany({ - where: { - author: { email: session.user.email }, - published: false, - }, - include: { - author: { - select: { name: true }, - }, - }, - }); - return { - props: { drafts }, - }; -}; - -type Props = { - drafts: PostProps[]; -}; - -const Drafts = (props: Props) => { - const { data: session } = useSession(); - - if (!session) { - return ( - - My Drafts - You need to be authenticated to view this page. - - ); - } - - return ( - - - My Drafts - - {props.drafts.map((post) => ( - - - - ))} - - - - - ); -}; - -export default Drafts; diff --git a/pages/_fichier_exemple_/p/[id].tsx b/pages/_fichier_exemple_/p/[id].tsx deleted file mode 100644 index f11fa58..0000000 --- a/pages/_fichier_exemple_/p/[id].tsx +++ /dev/null @@ -1,91 +0,0 @@ -import React from "react"; -import { GetServerSideProps } from "next"; -import ReactMarkdown from "react-markdown"; -import Layout from "../../../components/Layout"; -import Router from "next/router"; -import { PostProps } from "../../../components/Post"; -import prisma from '../../../lib/prisma' -import { useSession } from "next-auth/react"; - - -export const getServerSideProps: GetServerSideProps = async ({ params }) => { - const post = await prisma.post.findUnique({ - where: { - id: Number(params?.id) || -1, - }, - include: { - author: { - select: { name: true, email: true }, - }, - }, - }); - return { - props: post, - }; -}; - -async function publishPost(id: number): Promise { - await fetch(`/api/publish/${id}`, { - method: "PUT", - }); - await Router.push("/") -} - -async function deletePost(id: number): Promise { - await fetch(`/api/post/${id}`, { - method: "DELETE", - }); - await Router.push("/") -} - -const Post = (props: PostProps) => { - const { data: session, status } = useSession(); - if (status === 'loading') { - return Authenticating ...; - } - const userHasValidSession = Boolean(session); - const postBelongsToUser = session?.user?.email === props.author?.email; - let title = props.title; - if (!props.published) { - title = `${title} (Draft)`; - } - - return ( - - - {title} - By {props?.author?.name || "Unknown author"} - - {!props.published && userHasValidSession && postBelongsToUser && ( - publishPost(props.id)}>Publish - )} - {userHasValidSession && postBelongsToUser && ( - deletePost(props.id)}>Delete - )} - - - - ); -}; - -export default Post; diff --git a/pages/_fichier_exemple_/profile.tsx b/pages/_fichier_exemple_/profile.tsx deleted file mode 100644 index d9faaa1..0000000 --- a/pages/_fichier_exemple_/profile.tsx +++ /dev/null @@ -1,171 +0,0 @@ -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 Router from "next/router"; -import { ProfileProps } from "../../types/profile"; -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 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); - - - // il n'existe pas ? on le crée - if (isUserProfile.length <= 0) { - const newProfil = await prisma.profile.create({ - data: { - bio: "", - user: { - connect: { email: session.user.email }, - }, - }, - }); - console.log('newProfil', newProfil); - - } - - // on renvoi le profil - return { props: { isUserProfile } }; - } -}; - - -const Profil = ({ isUserProfile }: Profil) => { - - const validationSchema = Yup.object().shape({ - bio: Yup.string().trim().required("ce champ ne peux etre vide..."), - }); - - const { - register, - handleSubmit, - reset, - formState: { errors } - } = useForm({ - resolver: yupResolver(validationSchema) - }); - - const [response, setResponse] = useState(); - const { data: session } = useSession(); - - React.useEffect(() => { - console.log('user profil props', isUserProfile) - }, []) - - const onSubmit = async (data: UserSubmitForm) => { - console.log(data); - - 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 : {isUserProfile[0]?.bio} - - - Ma bio : - - {errors.bio?.message} - - - - - - Valider les changements - - reset()} - className="btn btn-warning float-right" - > - Reset - - - - - - - Email : {session.user.email} - {response && {response.message}} - - - - ); -}; - - - -export default Profil; diff --git a/pages/admin.tsx b/pages/admin.tsx index cfbaf3f..20c2b4f 100644 --- a/pages/admin.tsx +++ b/pages/admin.tsx @@ -202,7 +202,7 @@ const Admin = (props: Props) => { fetcher('/api/user/update-gift', 'PUT', body) console.log('g', gain); - // setCurrentUserGain(gain) + setCurrentUserGain(gain) // router.refresh(); };
By {props?.author?.name || "Unknown author"}
Ma bio : {isUserProfile[0]?.bio}
Email : {session.user.email}
{response.message}