Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Fixed Enrol button bug and improved Error message formate #1571 (#1578)

Anurag Mishra3c5c3e4059a5
diff --git a/apps/web/src/app/tracks/_components/enroll-button.tsx b/apps/web/src/app/tracks/_components/enroll-button.tsx
index 09821f41..5a814435 100644
--- a/apps/web/src/app/tracks/_components/enroll-button.tsx
+++ b/apps/web/src/app/tracks/_components/enroll-button.tsx
@@ -5,6 +5,7 @@ import { toast } from '@repo/ui/components/use-toast';
import { useState } from 'react';
import type { enrollUserInTrack, unenrollUserFromTrack } from './track.action';
import Link from 'next/link';
+import { usePathname } from 'next/navigation';
interface EnrollButtonProps {
action: typeof enrollUserInTrack | typeof unenrollUserFromTrack;
@@ -14,7 +15,9 @@ interface EnrollButtonProps {
}
export function ActionButton({ action, text, trackId, slug }: EnrollButtonProps) {
+ const path = usePathname();
const [isLoading, setIsLoading] = useState(false);
+
return (
<Button
className="shadcnoverridetransition select-none rounded-3xl bg-black text-white shadow-[0px_16px_6px_-16px_#ff4,4px_2px_4px_-2px_#f4f,-4px_2px_4px_-2px_#4f4] hover:-translate-y-1 hover:bg-neutral-800 hover:shadow-[0px_16px_6px_-12px_#ff4,4px_6px_6px_-2px_#f4f,-4px_6px_6px_-2px_#4f4] active:translate-y-0 active:scale-[0.99] active:shadow-[0px_10px_3px_-16px_#ff4,8px_0px_2px_-2px_#f4f,-8px_0px_2px_-2px_#4f4] dark:bg-white dark:text-black dark:hover:bg-white dark:hover:brightness-125 dark:hover:saturate-150"
@@ -23,34 +26,49 @@ export function ActionButton({ action, text, trackId, slug }: EnrollButtonProps)
setIsLoading(true);
try {
- await action(trackId, slug);
+ //i hate this
+ const maybeAnError = await action(trackId, slug);
+ if (maybeAnError === 'User is not logged in') {
+ throw new Error('User is not logged in');
+ }
+
toast({
title: 'Success',
variant: 'success',
description: <p>You're now successfully {text.toLowerCase()}ed the track.</p>,
});
- setIsLoading(false);
- } catch (error) {
- error instanceof Error && error.message === 'User is not logged in'
- ? toast({
- title: `Please Login to continue`,
+ } catch (error: unknown) {
+ if (error instanceof Error) {
+ if (error.message === 'User is not logged in') {
+ toast({
+ title: 'Please Login to continue',
variant: 'destructive',
description: (
<p>
You must be logged in to enroll in a track.{' '}
- <Link href="/login" className="font-bold underline">
+ <Link href={`/login?redirectTo=${path}`} className="font-bold underline">
Login
</Link>
</p>
),
- })
- : toast({
- title: `Error`,
+ });
+ } else {
+ toast({
+ title: 'Error',
variant: 'destructive',
description: (
<p>There was an error trying to {text.toLowerCase()} you from the track.</p>
),
});
+ }
+ } else {
+ toast({
+ title: 'Unknown Error',
+ variant: 'destructive',
+ description: <p>Something went wrong</p>,
+ });
+ }
+ } finally {
setIsLoading(false);
}
}}
diff --git a/apps/web/src/app/tracks/_components/track.action.ts b/apps/web/src/app/tracks/_components/track.action.ts
index aa857268..895c15cc 100644
--- a/apps/web/src/app/tracks/_components/track.action.ts
+++ b/apps/web/src/app/tracks/_components/track.action.ts
@@ -14,7 +14,7 @@ import { auth } from '~/server/auth';
export async function enrollUserInTrack(id: number, slug: string) {
const session = await auth();
if (!session) {
- throw new Error('User is not logged in');
+ return 'User is not logged in';
}
await prisma.track.update({
@@ -41,7 +41,7 @@ export async function enrollUserInTrack(id: number, slug: string) {
export async function unenrollUserFromTrack(id: number, slug: string) {
const session = await auth();
if (!session) {
- throw new Error('User is not logged in');
+ return 'User is not logged in';
}
await prisma.track.update({