From f4757ff05a4eb2ba2cd0b030bba7879a35cdb319 Mon Sep 17 00:00:00 2001 From: Harry Huang Date: Mon, 15 Apr 2024 13:01:17 -0500 Subject: [PATCH] Smoothen error handling process --- components/Forms/CreateAdminPopupWindow.tsx | 79 ++++----------------- pages/api/admin/index.ts | 12 ++-- 2 files changed, 20 insertions(+), 71 deletions(-) diff --git a/components/Forms/CreateAdminPopupWindow.tsx b/components/Forms/CreateAdminPopupWindow.tsx index 4c85b7b..b0bd0ac 100644 --- a/components/Forms/CreateAdminPopupWindow.tsx +++ b/components/Forms/CreateAdminPopupWindow.tsx @@ -1,23 +1,8 @@ -import { - AboutEvent, - FormLogistics, - ShortFormInput, - LongFormInput, - LargeFormInput, - FormInput, - FormLabel, - CreateEventForm, - FormHeader, - CreateAdminContainer, -} from '@/styles/components/event.styles'; -import { - SubmitButton, - ButtonCenter, -} from '@/styles/components/windowFlow.styles'; -import React, { useState } from 'react'; +import { CreateAdminContainer } from '@/styles/components/event.styles'; +import { SubmitButton } from '@/styles/components/windowFlow.styles'; +import React from 'react'; import PopupWindow from '@/components/PopupWindow'; -import { useForm } from 'react-hook-form'; -import { Form, type FormProps, Input, Button, message } from 'antd'; +import { Form, Input } from 'antd'; import { Typography } from 'antd'; const { Title } = Typography; @@ -40,16 +25,19 @@ const CreateEventPopupWindow = ({ body: JSON.stringify(values), }); - if (res.ok) { - const resObj = await res.json(); + if (!res.ok) { + throw new Error('Failed to add new account'); + } + + const resObj = await res.json(); + if (resObj.status === 'error') { messageApi.open({ - type: 'success', + type: 'error', content: resObj.message, }); } else { - const resObj = await res.json(); messageApi.open({ - type: 'error', + type: 'success', content: resObj.message, }); } @@ -64,49 +52,6 @@ const CreateEventPopupWindow = ({ setShowPopup(false); }; - const { register, handleSubmit } = useForm({}); - - const onSubmit = (data: any) => { - const results = JSON.stringify({ - firstName: data.firstName, - lastName: data.lastName, - email: data.email, - phone: data.phone, - role: data.role, - password: data.password, - }); - console.log('Here are the results: ', results); - createAdmin(results); - }; - - const createAdmin = async (data: any) => { - const res = await fetch('/api/admin', { - method: 'POST', - headers: { - // Specify the content type in the headers - 'Content-Type': 'application/json', - }, - // Stringify the JSON body - body: data, - }); - - if (res.ok) { - const resObj = await res.json(); - messageApi.open({ - type: 'success', - content: resObj.message, - }); - } else { - const resObj = await res.json(); - messageApi.open({ - type: 'error', - content: resObj.message, - }); - } - - setShowPopup(false); - }; - return ( <> setShowPopup(false)}> diff --git a/pages/api/admin/index.ts b/pages/api/admin/index.ts index e9042e3..9ace6a9 100644 --- a/pages/api/admin/index.ts +++ b/pages/api/admin/index.ts @@ -33,8 +33,9 @@ export default async function handler( case 'POST': try { if (session.user.status !== 'superadmin') { - res.status(401).json({ + res.status(200).json({ message: 'Sorry, only super admin is allowed to create new admins', + status: 'error', }); throw new Error('Only super admin is allowed to create new admins'); } @@ -46,8 +47,9 @@ export default async function handler( // Check if the user's email and password are valid if (!email || !email.includes('@') || !password) { - res.status(422).json({ message: 'Invalid email or password' }); - throw new Error('Invalid email or password'); + res + .status(200) + .json({ message: 'Invalid email or password', status: 'error' }); } // Connect to the database @@ -60,7 +62,9 @@ export default async function handler( // If the user already exists, return an error if (checkExisting) { - res.status(422).json({ message: 'Admin email already exists' }); + res + .status(200) + .json({ message: 'Admin email already exists', status: 'error' }); throw new Error('Admin email already exists'); }