mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Refactor BeerPostHeader, refactor login and register
- Updated BeerPostHeader in /id page to use semantic HTML tags - Removed the getServerSidesProps fn in /login and /register. Moved the redirect logic over to the client, will redirect on the client side. - Added delete BeerPost option on the edit page.
This commit is contained in:
@@ -6,20 +6,25 @@ import editBeerPostById from '@/services/BeerPost/editBeerPostById';
|
||||
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { createRouter, NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
interface EditBeerPostRequest extends UserExtendedNextApiRequest {
|
||||
interface BeerPostRequest extends UserExtendedNextApiRequest {
|
||||
query: { id: string };
|
||||
}
|
||||
|
||||
interface EditBeerPostRequest extends BeerPostRequest {
|
||||
body: z.infer<typeof EditBeerPostValidationSchema>;
|
||||
}
|
||||
|
||||
const editBeerPost = async (
|
||||
req: EditBeerPostRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
const checkIfBeerPostOwner = async (
|
||||
req: BeerPostRequest,
|
||||
res: NextApiResponse,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const { body, user, query } = req;
|
||||
const { user, query } = req;
|
||||
const { id } = query;
|
||||
|
||||
const beerPost = await getBeerPostById(id);
|
||||
@@ -32,6 +37,18 @@ const editBeerPost = async (
|
||||
throw new ServerError('You cannot edit that beer post.', 403);
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
const editBeerPost = async (
|
||||
req: EditBeerPostRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const {
|
||||
body,
|
||||
query: { id },
|
||||
} = req;
|
||||
|
||||
await editBeerPostById(id, body);
|
||||
|
||||
res.status(200).json({
|
||||
@@ -41,12 +58,32 @@ const editBeerPost = async (
|
||||
});
|
||||
};
|
||||
|
||||
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||
const {
|
||||
query: { id },
|
||||
} = req;
|
||||
|
||||
const deleted = await DBClient.instance.beerPost.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!deleted) {
|
||||
throw new ServerError('Beer post not found', 404);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer post deleted successfully',
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
const router = createRouter<
|
||||
EditBeerPostRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.put(getCurrentUser, editBeerPost);
|
||||
router.put(getCurrentUser, checkIfBeerPostOwner, editBeerPost);
|
||||
router.delete(getCurrentUser, checkIfBeerPostOwner, deleteBeerPost);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
|
||||
|
||||
@@ -6,9 +6,11 @@ import Image from 'next/image';
|
||||
import { FaUserCircle } from 'react-icons/fa';
|
||||
import Head from 'next/head';
|
||||
import Link from 'next/link';
|
||||
import redirectIfLoggedIn from '@/getServerSideProps/redirectIfLoggedIn';
|
||||
|
||||
import useRedirectWhenLoggedIn from '@/hooks/useRedirectIfLoggedIn';
|
||||
|
||||
const LoginPage: NextPage = () => {
|
||||
useRedirectWhenLoggedIn();
|
||||
return (
|
||||
<Layout>
|
||||
<Head>
|
||||
@@ -52,8 +54,3 @@ const LoginPage: NextPage = () => {
|
||||
};
|
||||
|
||||
export default LoginPage;
|
||||
|
||||
export const getServerSideProps = redirectIfLoggedIn({
|
||||
destination: '/',
|
||||
permanent: false,
|
||||
});
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import RegisterUserForm from '@/components/RegisterUserForm';
|
||||
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
||||
import Layout from '@/components/ui/Layout';
|
||||
import redirectIfLoggedIn from '@/getServerSideProps/redirectIfLoggedIn';
|
||||
|
||||
import useRedirectWhenLoggedIn from '@/hooks/useRedirectIfLoggedIn';
|
||||
import { NextPage } from 'next';
|
||||
import Head from 'next/head';
|
||||
import { BiUser } from 'react-icons/bi';
|
||||
|
||||
const RegisterUserPage: NextPage = () => {
|
||||
useRedirectWhenLoggedIn();
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<Head>
|
||||
@@ -26,8 +29,3 @@ const RegisterUserPage: NextPage = () => {
|
||||
};
|
||||
|
||||
export default RegisterUserPage;
|
||||
|
||||
export const getServerSideProps = redirectIfLoggedIn({
|
||||
destination: '/',
|
||||
permanent: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user