Skip to content

Commit

Permalink
Merge pull request #66 from ChangePlusPlusVandy/admin-table-error-han…
Browse files Browse the repository at this point in the history
…dling

Smoothen error handling process
  • Loading branch information
JiashuHarryHuang authored Apr 15, 2024
2 parents e01c9da + f4757ff commit 9f64c99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 71 deletions.
79 changes: 12 additions & 67 deletions components/Forms/CreateAdminPopupWindow.tsx
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
});
}
Expand All @@ -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 (
<>
<PopupWindow hidePopup={() => setShowPopup(false)}>
Expand Down
12 changes: 8 additions & 4 deletions pages/api/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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
Expand All @@ -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');
}

Expand Down

0 comments on commit 9f64c99

Please sign in to comment.