Skip to content

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

feat: implement track detail page (#594)

Timon Jurschitscha1611cc3b484
diff --git a/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx b/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx
index 38f1fa76..84501822 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx
+++ b/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx
@@ -3,19 +3,17 @@
import { Check } from '@repo/ui/icons';
import clsx from 'clsx';
import { useIsMobile } from '~/utils/useIsMobile';
+
import type { Challenge } from '@repo/db/types';
interface TrackChallengeProps {
challenge: Challenge;
+ challengeInProgress?: boolean;
+ challengeCompleted?: boolean;
className?: string;
mock?: boolean;
}
-interface MockComponentProps {
- challenge: TrackChallengeProps['challenge'];
- mock?: TrackChallengeProps['mock'];
-}
-
const COLORS_BY_DIFFICULTY = {
BEGINNER: 'text-difficulty-beginner dark:text-difficulty-beginner-dark',
EASY: 'text-difficulty-easy dark:text-difficulty-easy-dark',
@@ -32,20 +30,13 @@ const BGS_BY_DIFFICULTY = {
EXTREME: 'to-difficulty-extreme/20 dark:to-difficulty-extreme-dark/20',
} as const;
-const MockComponent = ({ challenge, mock }: MockComponentProps) => {
- if (!mock) {
- return null;
- }
- return (
- <>
- <input className="peer hidden appearance-none" type="checkbox" id={challenge.id.toString()} />
- <div className="h-5 w-5 rounded-full border border-black/70 bg-black/10 duration-75 peer-checked:border-transparent peer-checked:bg-green-600/80 dark:border-white/50 dark:bg-white/10 peer-checked:dark:bg-green-300/80" />
- <Check className="absolute left-1 my-auto h-3 w-3 scale-0 stroke-[4] text-white duration-300 peer-checked:scale-100 dark:text-black" />
- </>
- );
-};
-
-export function TrackChallenge({ challenge, className }: TrackChallengeProps) {
+export function TrackChallenge({
+ challenge,
+ className,
+ mock,
+ challengeInProgress = false,
+ challengeCompleted = false,
+}: TrackChallengeProps) {
const isMobile = useIsMobile();
return (
@@ -65,9 +56,27 @@ export function TrackChallenge({ challenge, className }: TrackChallengeProps) {
'group-hover/challenge:scale-105 group-hover/challenge:rounded-xl group-hover/challenge:bg-neutral-500/20',
)}
>
- <div className="relative flex items-center gap-3 text-xs sm:text-base">
- <MockComponent challenge={challenge} mock />
- {challenge.name}
+ <div className="relative flex flex-col gap-3 text-xs sm:text-base">
+ <div className="relative flex items-center gap-3 text-xs sm:text-base">
+ {mock == true && (
+ <>
+ <input
+ className="peer hidden appearance-none"
+ type="checkbox"
+ id={challenge.id.toString()}
+ />
+ <div className="h-5 w-5 rounded-full border border-black/70 bg-black/10 duration-75 peer-checked:border-transparent peer-checked:bg-green-600/80 dark:border-white/50 dark:bg-white/10 peer-checked:dark:bg-green-300/80" />
+ <Check className="absolute left-1 my-auto h-3 w-3 scale-0 stroke-[4] text-white duration-300 peer-checked:scale-100 dark:text-black" />
+ </>
+ )}
+ {challenge.name}
+ {challengeCompleted ? (
+ <p className="text-xs">
+ {' '}
+ - <span className="text-green-600/80 dark:text-green-300/80">Done</span>
+ </p>
+ ) : null}
+ </div>
</div>
<div
className={`relative text-xs font-medium tracking-wide ${
@@ -79,8 +88,11 @@ export function TrackChallenge({ challenge, className }: TrackChallengeProps) {
BGS_BY_DIFFICULTY[challenge.difficulty]
}`}
/>
- {challenge.difficulty[0]}
- {challenge.difficulty.substring(1, challenge.difficulty.length).toLowerCase()}
+ {challengeInProgress
+ ? 'In Progress'
+ : `${challenge.difficulty[0]}${challenge.difficulty
+ .substring(1, challenge.difficulty.length)
+ .toLowerCase()}`}
</div>
</div>
</label>
diff --git a/apps/web/src/app/[locale]/tracks/_components/track-details.tsx b/apps/web/src/app/[locale]/tracks/_components/track-details.tsx
index 3a3aae44..7c6aed06 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track-details.tsx
+++ b/apps/web/src/app/[locale]/tracks/_components/track-details.tsx
@@ -1,5 +1,9 @@
import { getServerAuthSession } from '@repo/auth/server';
+import Link from 'next/link';
+
import { EnrollButton } from './enroll-button';
+import { TrackChallenge } from './track-challenge-card';
+import { TrackProgress } from './track-progress';
import { enrollUserInTrack, getTrackDetails, unenrollUserFromTrack } from './track.action';
interface TrackDetailProps {
@@ -10,32 +14,69 @@ interface TrackDetailProps {
export async function TrackDetail({ slug }: TrackDetailProps) {
const session = await getServerAuthSession();
const trackId = parseInt(slug);
- const trackChallenge = await getTrackDetails(trackId);
- const isEnrolled = trackChallenge?.enrolledUsers.findIndex(
- (user) => user.id === session?.user.id,
- );
+ const track = await getTrackDetails(trackId);
+ const trackChallenges = track?.trackChallenges ?? [];
+ const isEnrolled = track?.enrolledUsers.findIndex((user) => user.id === session?.user.id);
+ // Calculates the total number of successful challenges.
+ function calulcateCompletedChallenges(): number {
+ let completedChallenges = 0;
+ for (const trackChallenge of trackChallenges) {
+ for (const submission of trackChallenge.challenge.submission) {
+ if (submission.isSuccessful) {
+ completedChallenges++;
+ }
+ }
+ }
+ return completedChallenges;
+ }
return (
- <div className="container flex flex-col gap-8 py-5 md:gap-16 md:pb-20">
+ <div className="container flex min-h-screen flex-col items-center gap-8 py-5 md:gap-16 md:pb-20">
<div className="container">
<h3 className="mb-1 text-2xl font-bold tracking-wide text-neutral-900/40 dark:text-white/40">
Track
</h3>
<h1 className="mb-8 text-4xl font-bold tracking-tight text-neutral-900 dark:text-white">
- {trackChallenge?.title}
+ {track?.title}
</h1>
<p className=" max-w-[69ch] text-lg leading-10 text-neutral-600 dark:text-white/50">
- {trackChallenge?.description}
+ {track?.description}
</p>
- </div>
-
- <div className="flex flex-row">
+ {isEnrolled !== -1 && (
+ <div className="my-3">
+ <TrackProgress
+ completedChallenges={calulcateCompletedChallenges()}
+ totalChallenges={trackChallenges.length}
+ />
+ </div>
+ )}
{isEnrolled === -1 ? (
<EnrollButton action={enrollUserInTrack} trackId={trackId} text="Enroll" />
) : (
<EnrollButton action={unenrollUserFromTrack} trackId={trackId} text="Unenroll" />
)}
</div>
+ <div className="grid-col grid grid-cols-1 gap-x-4 self-stretch md:grid-cols-2">
+ {trackChallenges
+ .sort((a, b) => a.orderId - b.orderId)
+ .map((x) => x.challenge)
+ .map((challenge) => (
+ <Link key={challenge.id} href={`/challenge/${challenge.id}`}>
+ <TrackChallenge
+ className="max-w-lg"
+ challenge={challenge}
+ challengeCompleted={
+ challenge.submission.length > 0 &&
+ challenge.submission.some((submission) => submission.isSuccessful)
+ }
+ challengeInProgress={
+ challenge.submission.length > 0 &&
+ !challenge.submission.some((submission) => submission.isSuccessful)
+ }
+ />
+ </Link>
+ ))}
+ </div>
</div>
);
}
diff --git a/apps/web/src/app/[locale]/tracks/_components/track.action.ts b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
index 732573ec..fe95208b 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track.action.ts
+++ b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
@@ -61,7 +61,7 @@ export const getTrackDetails = cache(async (id: number) => {
enrolledUsers: true,
},
},
- trackChallenges: true,
+ trackChallenges: { include: { challenge: { include: { submission: true } } } },
enrolledUsers: {
where: {
id: session?.user.id,