From 0b9bbe44d68bd16d215770851de06f427d1c0db6 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 3 Dec 2022 14:09:04 +0100 Subject: [PATCH] add - page mon compte --- .vscode/settings.json | 5 - pages/api/user/index.ts | 79 ++++++++++ pages/mon-compte.tsx | 147 ++++++++++++------ .../migrations/20221203122940_/migration.sql | 9 ++ .../migrations/20221203130315_/migration.sql | 9 ++ prisma/schema.prisma | 4 +- 6 files changed, 199 insertions(+), 54 deletions(-) create mode 100644 pages/api/user/index.ts create mode 100644 prisma/migrations/20221203122940_/migration.sql create mode 100644 prisma/migrations/20221203130315_/migration.sql diff --git a/.vscode/settings.json b/.vscode/settings.json index 5a976ad..7a73a41 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,2 @@ { - "workbench.colorCustomizations": { - "activityBar.background": "#521342", - "titleBar.activeBackground": "#731B5D", - "titleBar.activeForeground": "#FDF8FC" - } } \ No newline at end of file diff --git a/pages/api/user/index.ts b/pages/api/user/index.ts new file mode 100644 index 0000000..88a00df --- /dev/null +++ b/pages/api/user/index.ts @@ -0,0 +1,79 @@ +import prisma from '../../../lib/prisma'; +import chance from 'chance' +import cryptoRandomString from 'crypto-random-string'; + + +import { NextApiRequest, NextApiResponse } from 'next'; +import { getSession } from 'next-auth/react'; +import { Cadeaux } from '../../../enum/cadeaux.enum'; +import { Role } from '../../../enum/role.enum'; + + + +//API gain qui va generer un code cadeau qui sera sur la facture du client +//On part du principe qu'une 'API caisse'(qu'on ne gere pas) déclenche l'appel à l'API gain + +export default async function updateOrCreate(req: NextApiRequest, res: NextApiResponse) { + const session = await getSession({ req }) + const { address, name, phoneNumber, surname, zipCode } = req.body; + + // est-ce la méthode est un POST ou PUT? + if (req.method !== ("POST" || "PUT")) { + throw new Error( + `The HTTP ${req.method} method is not supported at this route.` + ); + } + + // est-ce que utilisateur est connecté ? + if (!session) { + res.status(401).json({ err: "Vous devez être connecté" }); + return; + } + + + //récupération des informations de l'utilisateur connecté + const user = await prisma.user.findFirst({ + where: { email: session.user.email }, + }) + + //mise a jour ou création des données pour l'utilisateur + const upsertUser = await prisma.user.upsert({ + where: { + email: user.email, + }, + update: { + name, + surname, + address, + phoneNumber, + zipCode + }, + create: { + name, + surname, + address, + phoneNumber, + zipCode + }, + }) + res.status(200).send({ message: 'Vos informations ont été mise à jours !', res: upsertUser }); + +} + + + + + + + + + + + + + + + + + + diff --git a/pages/mon-compte.tsx b/pages/mon-compte.tsx index 15a3708..7444499 100644 --- a/pages/mon-compte.tsx +++ b/pages/mon-compte.tsx @@ -22,7 +22,12 @@ import imageConnexion from '../public/images/connexion_image.png' import Link from "next/link"; -import { GrValidate } from 'react-icons/gr' + + +import { TfiInfoAlt } from 'react-icons/tfi' +import { TfiGift } from 'react-icons/tfi' + + import { toast } from "react-toastify"; import { Token } from "nodemailer/lib/xoauth2"; import { isGeneratorFunction } from "util/types"; @@ -36,8 +41,8 @@ type FormData = { name: string; surname: string; address: string; - zipCode: string; - phoneNumber: string; + zipCode: number; + phoneNumber: number; }; export async function getServerSideProps(context) { @@ -52,7 +57,6 @@ export async function getServerSideProps(context) { } } - const user = await prisma.user.findUnique({ where: { email: session?.user.email @@ -68,20 +72,12 @@ export async function getServerSideProps(context) { } }) - - return { props: { user: JSON.parse(JSON.stringify(user)), userGains, }, } - - - - - - } type Props = { @@ -92,6 +88,7 @@ type Props = { const MonCompte = (props: Props) => { const { data: session, status } = useSession(); + const [response, setResponse] = useState(null) const validationSchema = Yup.object().shape({ name: Yup.string() @@ -114,9 +111,59 @@ const MonCompte = (props: Props) => { const { register, setValue, handleSubmit, formState: { errors } } = useForm({ resolver: yupResolver(validationSchema) }); - const onSubmit: SubmitHandler = data => console.log(data); - + const onSubmit = async ({ + email, + name, + surname, + address, + zipCode, + phoneNumber }: FormData) => { + try { + const body = { + email, + name, + surname, + address, + zipCode: Number(zipCode), + phoneNumber: Number(phoneNumber), + }; + await fetch(`/api/user/`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }) + .then((res) => res.json()) + .then(async (data) => { + setResponse(data); + if (data.err) { + toast.error(data.err, { + position: "top-center", + autoClose: 2000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + }); + return; + } + toast.success(data.message, { + position: "top-center", + autoClose: 5000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + }); + }) + } catch (error) { + console.error('ERROR :', error); + } + }; return ( <> @@ -130,11 +177,11 @@ const MonCompte = (props: Props) => {
-

Mon Compte

-
+
-

Mes informations

+
+

Mes informations


@@ -149,7 +196,7 @@ const MonCompte = (props: Props) => {
- +
{errors.name?.message}
@@ -157,7 +204,7 @@ const MonCompte = (props: Props) => {
- +
{errors.surname?.message}
@@ -165,7 +212,7 @@ const MonCompte = (props: Props) => {
- +
{errors.address?.message}
@@ -173,7 +220,7 @@ const MonCompte = (props: Props) => {
- +
{errors.zipCode?.message}
@@ -181,42 +228,48 @@ const MonCompte = (props: Props) => {
- +
{errors.phoneNumber?.message}
- +
+ +
+
-

Mes cadeaux


- {!props.userGains &&

Pas encore de cadeau gagné

} - - - - - - - - - - {props.userGains.map((info: any) => { - return - - - +

Mes cadeaux

+
+ {props.userGains.length <= 0 &&

Pas encore de cadeau gagné

} + {props.userGains.length >= 1 && +
#Mes codes jouéMes cadeaux
{info.codeCadeau}{info.produit.nomProduit}
+ + + + + - })} - -
#Mes codes jouéMes cadeaux
+ + + {props.userGains.map((info: any) => { + return + + {info.codeCadeau} + {info.produit.nomProduit} + + })} + + + }
diff --git a/prisma/migrations/20221203122940_/migration.sql b/prisma/migrations/20221203122940_/migration.sql new file mode 100644 index 0000000..708e0c8 --- /dev/null +++ b/prisma/migrations/20221203122940_/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - The `phoneNumber` column on the `users` table would be dropped and recreated. This will lead to data loss if there is data in the column. + +*/ +-- AlterTable +ALTER TABLE "users" DROP COLUMN "phoneNumber", +ADD COLUMN "phoneNumber" INTEGER; diff --git a/prisma/migrations/20221203130315_/migration.sql b/prisma/migrations/20221203130315_/migration.sql new file mode 100644 index 0000000..02d54d3 --- /dev/null +++ b/prisma/migrations/20221203130315_/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - The `zipCode` column on the `users` table would be dropped and recreated. This will lead to data loss if there is data in the column. + +*/ +-- AlterTable +ALTER TABLE "users" DROP COLUMN "zipCode", +ADD COLUMN "zipCode" INTEGER; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 695cfa3..b2801ce 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -44,8 +44,8 @@ model User { name String? surname String? address String? - zipCode String? - phoneNumber String? + zipCode Int? + phoneNumber Int? email String? @unique emailVerified DateTime? @map("email_verified") image String?