Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Markusplay committed Feb 4, 2025
1 parent d4dba94 commit 12d0ab2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 76 deletions.
3 changes: 2 additions & 1 deletion src/actions/profile.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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('/');
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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>('user');

const handleAcceptCodeOfHonor = async () => {
const res = await acceptCodeOfHonor();

if (!res) {
errorToast();
return;
}
setUser(res);
router.push('/');
await acceptCodeOfHonor();
};

return (
Expand Down
42 changes: 3 additions & 39 deletions src/app/[locale]/(private)/profile/components/code-of-honor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>('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>('user');
return (
<div className="flex flex-col gap-3">
<Heading6>{t('codeOfHonor.title')}</Heading6>
Expand All @@ -45,22 +20,11 @@ export function CodeOfHonor() {
documentsLink: (chunks) => <Link href="/kpi-documents">{chunks}</Link>,
paragraph: (chunks) => <Paragraph className="m-0 text-lg">{chunks}</Paragraph>,
})}
{user?.codeOfHonorSignDate ? (
{user?.codeOfHonorSignDate && (
<div className="flex flex-col gap-1">
<Paragraph>{t('codeOfHonor.agreement')}</Paragraph>
<Paragraph className="m-0">{user?.codeOfHonorSignDate}</Paragraph>
</div>
) : (
<Button
className="ml-auto w-fit"
loading={loading}
onClick={handleAcceptCodeOfHonor}
size={isMobile ? 'medium' : 'big'}
icon={<Check />}
iconPosition="end"
>
{t('button.agree')}
</Button>
)}
</div>
);
Expand Down
36 changes: 17 additions & 19 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

0 comments on commit 12d0ab2

Please sign in to comment.