mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Update next-connect middleware to return next() to fix error handling
This commit is contained in:
@@ -38,7 +38,7 @@ const checkIfCommentOwner = async (
|
||||
throw new ServerError('You are not authorized to modify this comment', 403);
|
||||
}
|
||||
|
||||
await next();
|
||||
return next();
|
||||
};
|
||||
|
||||
const editComment = async (
|
||||
@@ -53,7 +53,7 @@ const editComment = async (
|
||||
id,
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
statusCode: 200,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import editBeerPostById from '@/services/BeerPost/editBeerPostById';
|
||||
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
@@ -10,6 +9,8 @@ import { createRouter, NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
|
||||
interface BeerPostRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
@@ -37,7 +38,7 @@ const checkIfBeerPostOwner = async (
|
||||
throw new ServerError('You cannot edit that beer post.', 403);
|
||||
}
|
||||
|
||||
next();
|
||||
return next();
|
||||
};
|
||||
|
||||
const editBeerPost = async (
|
||||
@@ -82,8 +83,21 @@ const router = createRouter<
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.put(getCurrentUser, checkIfBeerPostOwner, editBeerPost);
|
||||
router.delete(getCurrentUser, checkIfBeerPostOwner, deleteBeerPost);
|
||||
router.put(
|
||||
validateRequest({
|
||||
bodySchema: EditBeerPostValidationSchema,
|
||||
querySchema: z.object({ id: z.string() }),
|
||||
}),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
editBeerPost,
|
||||
);
|
||||
router.delete(
|
||||
validateRequest({ querySchema: z.object({ id: z.string() }) }),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
deleteBeerPost,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ const checkIfBreweryPostOwner = async (
|
||||
throw new ServerError('You are not the owner of this brewery post', 403);
|
||||
}
|
||||
|
||||
next();
|
||||
return next();
|
||||
};
|
||||
|
||||
const editBreweryPost = async (
|
||||
@@ -64,9 +64,7 @@ const deleteBreweryPost = async (req: BreweryPostRequest, res: NextApiResponse)
|
||||
query: { id },
|
||||
} = req;
|
||||
|
||||
const deleted = await DBClient.instance.beerPost.delete({
|
||||
where: { id },
|
||||
});
|
||||
const deleted = await DBClient.instance.breweryPost.delete({ where: { id } });
|
||||
|
||||
if (!deleted) {
|
||||
throw new ServerError('Brewery post not found', 404);
|
||||
|
||||
@@ -39,7 +39,7 @@ const checkIfCommentOwner = async (
|
||||
throw new ServerError('You are not authorized to modify this comment', 403);
|
||||
}
|
||||
|
||||
await next();
|
||||
return next();
|
||||
};
|
||||
|
||||
const editComment = async (
|
||||
|
||||
@@ -46,7 +46,7 @@ const checkIfUserCanEditUser = async (
|
||||
throw new ServerError('You are not permitted to modify this user', 403);
|
||||
}
|
||||
|
||||
await next();
|
||||
return next();
|
||||
};
|
||||
|
||||
const editUser = async (
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
import FormError from '@/components/ui/forms/FormError';
|
||||
import FormInfo from '@/components/ui/forms/FormInfo';
|
||||
import FormLabel from '@/components/ui/forms/FormLabel';
|
||||
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
||||
import FormSegment from '@/components/ui/forms/FormSegment';
|
||||
import FormTextArea from '@/components/ui/forms/FormTextArea';
|
||||
import FormTextInput from '@/components/ui/forms/FormTextInput';
|
||||
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/schema/BreweryPostQueryResult';
|
||||
import EditBreweryPostValidationSchema from '@/services/BreweryPost/schema/EditBreweryPostValidationSchema';
|
||||
import withPageAuthRequired from '@/util/withPageAuthRequired';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { NextPage } from 'next';
|
||||
import Head from 'next/head';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { BiBeer } from 'react-icons/bi';
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -14,6 +24,49 @@ interface EditPageProps {
|
||||
const EditBreweryPostPage: NextPage<EditPageProps> = ({ breweryPost }) => {
|
||||
const pageTitle = `Edit \u201c${breweryPost.name}\u201d`;
|
||||
|
||||
const router = useRouter();
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<z.infer<typeof EditBreweryPostValidationSchema>>({
|
||||
resolver: zodResolver(EditBreweryPostValidationSchema),
|
||||
defaultValues: {
|
||||
name: breweryPost.name,
|
||||
description: breweryPost.description,
|
||||
id: breweryPost.id,
|
||||
dateEstablished: breweryPost.dateEstablished,
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmit = async (data: z.infer<typeof EditBreweryPostValidationSchema>) => {
|
||||
const response = await fetch(`/api/breweries/${breweryPost.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Something went wrong');
|
||||
}
|
||||
|
||||
router.push(`/breweries/${breweryPost.id}`);
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
const response = await fetch(`/api/breweries/${breweryPost.id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Something went wrong');
|
||||
}
|
||||
|
||||
router.push('/breweries');
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@@ -27,7 +80,59 @@ const EditBreweryPostPage: NextPage<EditPageProps> = ({ breweryPost }) => {
|
||||
backLink={`/breweries/${breweryPost.id}`}
|
||||
backLinkText={`Back to "${breweryPost.name}"`}
|
||||
>
|
||||
<></>
|
||||
<>
|
||||
<form className="form-control space-y-4" onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="w-full">
|
||||
<FormInfo>
|
||||
<FormLabel htmlFor="name">Name</FormLabel>
|
||||
<FormError>{errors.name?.message}</FormError>
|
||||
</FormInfo>
|
||||
<FormSegment>
|
||||
<FormTextInput
|
||||
id="name"
|
||||
type="text"
|
||||
placeholder="Name"
|
||||
formValidationSchema={register('name')}
|
||||
error={!!errors.name}
|
||||
disabled={isSubmitting}
|
||||
/>
|
||||
</FormSegment>
|
||||
|
||||
<FormInfo>
|
||||
<FormLabel htmlFor="description">Description</FormLabel>
|
||||
<FormError>{errors.description?.message}</FormError>
|
||||
</FormInfo>
|
||||
<FormSegment>
|
||||
<FormTextArea
|
||||
disabled={isSubmitting}
|
||||
placeholder="Ratione cumque quas quia aut impedit ea culpa facere. Ut in sit et quas reiciendis itaque."
|
||||
error={!!errors.description}
|
||||
formValidationSchema={register('description')}
|
||||
id="description"
|
||||
rows={8}
|
||||
/>
|
||||
</FormSegment>
|
||||
</div>
|
||||
|
||||
<div className="w-full space-y-3">
|
||||
<button
|
||||
disabled={isSubmitting}
|
||||
className="btn-primary btn w-full"
|
||||
type="submit"
|
||||
>
|
||||
{isSubmitting ? 'Saving...' : 'Save'}
|
||||
</button>
|
||||
|
||||
<button
|
||||
className="btn-primary btn w-full"
|
||||
type="button"
|
||||
onClick={handleDelete}
|
||||
>
|
||||
Delete Brewery
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</>
|
||||
</FormPageLayout>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user