From 12d0ab2cd7edeaa59852a08214a27ddb7604b480 Mon Sep 17 00:00:00 2001 From: Markusplay Date: Wed, 5 Feb 2025 00:25:02 +0200 Subject: [PATCH] refactor --- src/actions/profile.actions.ts | 3 +- .../page.tsx | 18 +------- .../profile/components/code-of-honor.tsx | 42 ++----------------- src/middleware.ts | 36 ++++++++-------- 4 files changed, 23 insertions(+), 76 deletions(-) rename src/app/[locale]/(private)/{accept-honor => accept-code-of-honor}/page.tsx (74%) diff --git a/src/actions/profile.actions.ts b/src/actions/profile.actions.ts index 6656bd59..0ead43c1 100644 --- a/src/actions/profile.actions.ts +++ b/src/actions/profile.actions.ts @@ -4,6 +4,7 @@ import { Contact, ContactType } from '@/types/contact'; import { campusFetch } from '@/lib/client'; import { revalidatePath } from 'next/cache'; import { getUserDetails } from '@/actions/auth.actions'; +import { redirect } from 'next/navigation'; export async function getContacts() { try { @@ -101,8 +102,8 @@ export async function acceptCodeOfHonor() { await campusFetch('profile/code-of-honor', { method: 'PUT', }); - return getUserDetails(); } catch (error) { throw new Error('Error while accepting code of honor'); } + redirect('/'); } diff --git a/src/app/[locale]/(private)/accept-honor/page.tsx b/src/app/[locale]/(private)/accept-code-of-honor/page.tsx similarity index 74% rename from src/app/[locale]/(private)/accept-honor/page.tsx rename to src/app/[locale]/(private)/accept-code-of-honor/page.tsx index 677924ff..3dcb5cf4 100644 --- a/src/app/[locale]/(private)/accept-honor/page.tsx +++ b/src/app/[locale]/(private)/accept-code-of-honor/page.tsx @@ -1,10 +1,7 @@ 'use client'; import { useTranslations } from 'next-intl'; -import { useServerErrorToast } from '@/hooks/use-server-error-toast'; import React from 'react'; -import { useLocalStorage } from '@/hooks/use-storage'; -import { User } from '@/types/user'; import { acceptCodeOfHonor } from '@/actions/profile.actions'; import { AlertDialog, @@ -17,25 +14,12 @@ import { } from '@/components/ui/alert-dialog'; import { Link } from '@/i18n/routing'; import { Paragraph } from '@/components/typography/paragraph'; -import { useRouter } from 'next/navigation'; export default function CodeOfHonorAlert() { - const router = useRouter(); const t = useTranslations('private.profile'); - const { errorToast } = useServerErrorToast(); - - const [, setUser] = useLocalStorage('user'); - const handleAcceptCodeOfHonor = async () => { - const res = await acceptCodeOfHonor(); - - if (!res) { - errorToast(); - return; - } - setUser(res); - router.push('/'); + await acceptCodeOfHonor(); }; return ( diff --git a/src/app/[locale]/(private)/profile/components/code-of-honor.tsx b/src/app/[locale]/(private)/profile/components/code-of-honor.tsx index c203ff69..fccae710 100644 --- a/src/app/[locale]/(private)/profile/components/code-of-honor.tsx +++ b/src/app/[locale]/(private)/profile/components/code-of-honor.tsx @@ -3,40 +3,15 @@ import { Heading6 } from '@/components/typography/headers'; import { Separator } from '@/components/ui/separator'; import { Paragraph } from '@/components/typography/paragraph'; -import { Button } from '@/components/ui/button'; -import { useIsMobile } from '@/hooks/use-mobile'; import { useLocalStorage } from '@/hooks/use-storage'; import { User } from '@/types/user'; -import { acceptCodeOfHonor } from '@/actions/profile.actions'; -import React, { useState } from 'react'; +import React from 'react'; import { useTranslations } from 'next-intl'; -import { Check } from '@/app/images'; -import { useServerErrorToast } from '@/hooks/use-server-error-toast'; import { Link } from '@/i18n/routing'; export function CodeOfHonor() { - const { errorToast } = useServerErrorToast(); - - const isMobile = useIsMobile(); - - const [user, setUser] = useLocalStorage('user'); - - const [loading, setLoading] = useState(false); - const t = useTranslations('private.profile'); - - const handleAcceptCodeOfHonor = async () => { - setLoading(true); - const res = await acceptCodeOfHonor(); - setLoading(false); - - if (!res) { - errorToast(); - return; - } - setUser(res); - }; - + const [user] = useLocalStorage('user'); return (
{t('codeOfHonor.title')} @@ -45,22 +20,11 @@ export function CodeOfHonor() { documentsLink: (chunks) => {chunks}, paragraph: (chunks) => {chunks}, })} - {user?.codeOfHonorSignDate ? ( + {user?.codeOfHonorSignDate && (
{t('codeOfHonor.agreement')} {user?.codeOfHonorSignDate}
- ) : ( - )}
); diff --git a/src/middleware.ts b/src/middleware.ts index 85da6a36..85f9db95 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -17,7 +17,7 @@ const composePathsRegExp = (paths: string[]) => RegExp(`^(/(${LOCALES.join('|')} const rootRegExp = new RegExp('^\/?$', 'i'); -const honorPageRegExp = composePathsRegExp(['/accept-honor']); +const codeOfHonorPathRegExp = composePathsRegExp(['/accept-code-of-honor']); const authPathRegExp = composePathsRegExp(['/login', '/password-reset/success', '/password-reset']); @@ -35,7 +35,7 @@ const publicPathRegExp = composePathsRegExp([ const isRoot = (request: NextRequest) => rootRegExp.test(request.nextUrl.pathname); const isPublicPath = (request: NextRequest) => publicPathRegExp.test(request.nextUrl.pathname); const isAuthPath = (request: NextRequest) => authPathRegExp.test(request.nextUrl.pathname); -const isAcceptHonorPath = (request: NextRequest) => honorPageRegExp.test(request.nextUrl.pathname); +const isAcceptCodeOfHonorPath = (request: NextRequest) => codeOfHonorPathRegExp.test(request.nextUrl.pathname); const isAuthenticated = (request: NextRequest) => { const cookie = request.cookies.get('token'); @@ -77,25 +77,28 @@ const authMiddleware = (request: NextRequest) => { return redirectWithIntl(request, '/'); } - return intlMiddleware(request); + return null; }; -const CoHMiddleware = async (request: NextRequest) => { +const codeOfHonorMiddleware = async (request: NextRequest) => { try { const user = await getUserDetails(); - const hasAcceptedCoH = !!user?.codeOfHonorSignDate; - - if ((!user?.studentProfile || hasAcceptedCoH) && isAcceptHonorPath(request)) { - return redirectWithIntl(request, '/'); + const hasAcceptedCodeOfHonor = !!user?.codeOfHonorSignDate; + + if (isAcceptCodeOfHonorPath(request)) { + if (!user?.studentProfile || hasAcceptedCodeOfHonor) { + return redirectWithIntl(request, '/'); + } + } else { + if (!!user?.studentProfile && !hasAcceptedCodeOfHonor) { + return redirectWithIntl(request, '/accept-code-of-honor'); + } } - if (!hasAcceptedCoH && !isAcceptHonorPath(request)) { - return redirectWithIntl(request, '/accept-honor'); - } + return intlMiddleware(request); } catch (error) { - return null; + return intlMiddleware(request); } - return intlMiddleware(request); }; export async function middleware(request: NextRequest) { @@ -112,10 +115,5 @@ export async function middleware(request: NextRequest) { return intlMiddleware(request); } - const honorRedirect = await CoHMiddleware(request); - if (honorRedirect) { - return honorRedirect; - } - - return (authMiddleware as any)(request); + return authMiddleware(request) || codeOfHonorMiddleware(request); }