From d6182f111d37c1c906130bc0f77c311867f777c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Lelong?= Date: Mon, 29 Jan 2024 15:07:31 +0100 Subject: [PATCH] fix: modal & handle 409 --- components/administration/UserCard.tsx | 35 ++---- pages/administration/bo/users/index.tsx | 136 +++++++++++++----------- pages/api/users/index.ts | 12 ++- 3 files changed, 91 insertions(+), 92 deletions(-) diff --git a/components/administration/UserCard.tsx b/components/administration/UserCard.tsx index b901476..287caa8 100644 --- a/components/administration/UserCard.tsx +++ b/components/administration/UserCard.tsx @@ -9,21 +9,10 @@ import { Dispatch, SetStateAction } from 'react'; type Props = { user: User; - modalProps: { onClick: () => void }; - setCurrentUser: Dispatch< - SetStateAction< - (Prisma.UserUpdateInput & { id: string }) | Prisma.UserCreateInput - > - >; onButtonClick: ({ type, user }: OnButtonClickUserParams) => void; }; -const UserCard = ({ - user, - onButtonClick, - modalProps, - setCurrentUser -}: Props) => { +const UserCard = ({ user, onButtonClick }: Props) => { const { data: session } = useSession({ required: true }); const { cx, classes } = useStyles(); @@ -66,21 +55,15 @@ const UserCard = ({ > Supprimer -
{ - setCurrentUser(user); - }} + -
+ Modifier + diff --git a/pages/administration/bo/users/index.tsx b/pages/administration/bo/users/index.tsx index 8d29c67..a21b454 100644 --- a/pages/administration/bo/users/index.tsx +++ b/pages/administration/bo/users/index.tsx @@ -1,5 +1,6 @@ import UserCard from '@/components/administration/UserCard'; import { Loader } from '@/components/generic/Loader'; +import { Modal } from '@/components/generic/Modal'; import { Pagination } from '@/components/generic/Pagination'; import { useUsers } from '@/utils/api'; import { getNbPages } from '@/utils/tools'; @@ -12,7 +13,7 @@ import { Prisma, User } from '@prisma/client'; import { useEffect, useRef, useState } from 'react'; export type OnButtonClickUserParams = - | { type: 'create'; user?: User } + | { type: 'update'; user: User } | { type: 'delete'; user: User }; type Props = { @@ -36,8 +37,10 @@ export default function Editions(props: Props) { const [currentPage, setCurrentPage] = useState(1); const [numberPerPage, _] = useState(10); + const [isModalOpen, setIsModalOpen] = useState(false); const [currentUser, setCurrentUser] = useState(defaultUserCreation); + const [alreadyExists, setAlreadyExists] = useState(false); const usernameInputRef = useRef(null); const emailInputRef = useRef(null); @@ -79,19 +82,6 @@ export default function Editions(props: Props) { if (!users) return
Aucun utilisateur
; - const { UserModal, userModalButtonProps } = createModal({ - name: 'user', - isOpenedByDefault: false - }); - - // WORKAROUND BESCAUSE THIS IS NOT POSSIBLE ON REACT-DSFR V1 - const closeModal = () => { - const button = document.getElementsByClassName( - 'fr-link--close' - )[0] as HTMLButtonElement; - if (button) button.click(); - }; - const handlePageChange = (pageNumber: number) => { setCurrentPage(pageNumber); }; @@ -116,8 +106,9 @@ export default function Editions(props: Props) { return; } + let response; if (currentUser.id) { - await fetch('/api/users', { + response = await fetch('/api/users', { method: 'PUT', body: JSON.stringify({ id: currentUser.id, @@ -130,7 +121,7 @@ export default function Editions(props: Props) { passwordInputRef?.current?.focus(); return; } - await fetch('/api/users', { + response = await fetch('/api/users', { method: 'POST', body: JSON.stringify({ username: usernameInputRef?.current?.value, @@ -140,8 +131,12 @@ export default function Editions(props: Props) { }); } - closeModal(); - refetchUsers(); + if (response.status === 409) { + setAlreadyExists(true); + } else if ([201, 200].includes(response.status)) { + setIsModalOpen(false); + refetchUsers(); + } }; const nbPages = getNbPages(usersCount, numberPerPage); @@ -162,7 +157,9 @@ export default function Editions(props: Props) { - - + {!currentUser.id && ( + + )} + + + + )} ); } diff --git a/pages/api/users/index.ts b/pages/api/users/index.ts index 5f22e4d..531eb02 100644 --- a/pages/api/users/index.ts +++ b/pages/api/users/index.ts @@ -39,6 +39,14 @@ export async function getUserById(id: string) { } export async function createUser(data: Omit) { + const alreadyExists = await prisma.user.findUnique({ + where: { + email: data.email + } + }); + + if (!!alreadyExists) return 409; + const user = await prisma.user.create({ data: { ...data, @@ -91,7 +99,9 @@ export default async function handler( } else if (req.method === 'POST') { const data = JSON.parse(req.body); const user = await createUser(data); - res.status(201).json(user); + + if (user === 409) res.status(409).json({ message: 'user already exists' }); + else res.status(201).json(user); } else if (req.method === 'PUT') { const { id, ...data } = JSON.parse(req.body); const user = await updateUser(id as string, data);