Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoothen error handling process #66

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading