Connect, collaborate, and grow with a community of TypeScript developers
feat: make view more button work (#345)
Ayushaab407596924
diff --git a/apps/web/src/app/challenge/[id]/page.tsx b/apps/web/src/app/challenge/[id]/page.tsxindex f7441764..49e06051 100644--- a/apps/web/src/app/challenge/[id]/page.tsx+++ b/apps/web/src/app/challenge/[id]/page.tsx@@ -1,4 +1,3 @@-import { notFound } from 'next/navigation';import { getServerAuthSession } from '@repo/auth/server';import { getChallengeRouteData } from './getChallengeRouteData';import { Comments } from '~/components/challenge/comments';diff --git a/apps/web/src/app/explore/[slug]/page.tsx b/apps/web/src/app/explore/[slug]/page.tsxnew file mode 100644index 00000000..2d509bd3--- /dev/null+++ b/apps/web/src/app/explore/[slug]/page.tsx@@ -0,0 +1,21 @@+import { Suspense } from 'react';+import { ExploreSlug } from '~/components/explore/explore-slug';+import { ExploreSlugSkeleton } from '~/components/explore/explore-slug-skeleton';++export const dynamic = 'force-dynamic';++interface Props {+ params: {+ slug: string;+ };+}++// accepts both difficulty & tags as slug.+// ex: `/explore/easy`, `explore/popular`+export default function Page({ params }: Props) {+ return (+ <Suspense fallback={<ExploreSlugSkeleton />}>+ <ExploreSlug slug={params.slug} />+ </Suspense>+ );+}diff --git a/apps/web/src/app/explore/error.tsx b/apps/web/src/app/explore/error.tsxnew file mode 100644index 00000000..224cd5cd--- /dev/null+++ b/apps/web/src/app/explore/error.tsx@@ -0,0 +1,17 @@+'use client'; // Error components must be Client Components++import { Button, Text } from '@repo/ui';+import Link from 'next/link';++export default function Error() {+ return (+ <div className="container flex h-full flex-col items-center justify-center">+ <Text className="mb-6" intent="h2">+ Uh oh! We couldn't find the challenge's you were looking for.+ </Text>+ <Link href="/explore">+ <Button>Explore Challenges</Button>+ </Link>+ </div>+ );+}diff --git a/apps/web/src/components/explore/explore-card.tsx b/apps/web/src/components/explore/explore-card.tsxindex 8bf6cf9b..dac3099b 100644--- a/apps/web/src/components/explore/explore-card.tsx+++ b/apps/web/src/components/explore/explore-card.tsx@@ -1,4 +1,3 @@-import { Circle, Diamond, MessageCircle, Plus, Sparkle, ThumbsUp, Triangle } from '@repo/ui/icons';import {Card,CardContent,@@ -7,12 +6,13 @@ import {CardTitle,DifficultyBadge,} from '@repo/ui';-import { type ExploreChallengeFetcher } from '.';+import { Circle, Diamond, MessageCircle, Plus, Sparkle, ThumbsUp, Triangle } from '@repo/ui/icons';+import type { ExploreChallengeData } from './explore.action';import { getRelativeTime } from '~/utils/relativeTime';interface ExploreCardProps {challenge: Pick<- Awaited<ReturnType<ExploreChallengeFetcher>>[0],+ Awaited<ExploreChallengeData>[0],'_count' | 'difficulty' | 'name' | 'shortDescription' | 'updatedAt' | 'user'>;}diff --git a/apps/web/src/components/explore/explore-slug-skeleton.tsx b/apps/web/src/components/explore/explore-slug-skeleton.tsxnew file mode 100644index 00000000..b019f16d--- /dev/null+++ b/apps/web/src/components/explore/explore-slug-skeleton.tsx@@ -0,0 +1,9 @@+import { ExploreSectionSkeleton } from './section-skeleton';++export function ExploreSlugSkeleton() {+ return (+ <div className="container flex flex-col items-center gap-8 py-5 md:gap-20 md:pb-20">+ <ExploreSectionSkeleton />+ </div>+ );+}diff --git a/apps/web/src/components/explore/explore-slug.tsx b/apps/web/src/components/explore/explore-slug.tsxnew file mode 100644index 00000000..6a520d74--- /dev/null+++ b/apps/web/src/components/explore/explore-slug.tsx@@ -0,0 +1,41 @@+import Link from 'next/link';+import { TypographyH3 } from '@repo/ui';+import { ExploreCard } from './explore-card';+import { getChallangesByTagOrDifficulty } from './explore.action';++interface ExploreSlugProps {+ slug: string;+}++/**+ * todo: paginate the challenges. also make em look nice.+ */+export async function ExploreSlug({ slug }: ExploreSlugProps) {+ const challenges = await getChallangesByTagOrDifficulty(slug);++ return (+ <div className="container flex flex-col items-center gap-8 py-5 md:gap-20 md:pb-20">+ <div className="flex space-y-2">+ <TypographyH3>{toPascalCase(slug)}</TypographyH3>+ </div>+ <div className="flex flex-wrap justify-center gap-6">+ {challenges.map((challenge) => (+ <Link+ className="group snap-center focus:outline-none sm:w-[330px] xl:w-[333px]"+ href={`/challenge/${challenge.id}`}+ key={challenge.id}+ >+ <ExploreCard key={`challenge-${challenge.id}`} challenge={challenge} />+ </Link>+ ))}+ </div>+ </div>+ );+}++/**+ * Converts the string to PascalCase.+ */+export function toPascalCase(str: string) {+ return `${str.charAt(0).toUpperCase()}${str.substring(1, str.length)}`;+}diff --git a/apps/web/src/components/explore/explore.action.ts b/apps/web/src/components/explore/explore.action.tsnew file mode 100644index 00000000..dbf391d3--- /dev/null+++ b/apps/web/src/components/explore/explore.action.ts@@ -0,0 +1,42 @@+'use server';++import { prisma } from '@repo/db';+import { Tags, type Difficulty } from '@repo/db/types';++export type ExploreChallengeData = ReturnType<typeof getChallangesByTagOrDifficulty>;+const allTags: Tags[] = Object.values(Tags);++/**+ * Fetches challenges either by tag or difficulty.+ */+export async function getChallangesByTagOrDifficulty(str: string, take?: number) {+ const formattedStr = str.trim().toUpperCase();++ return prisma.challenge.findMany({+ where: {+ status: 'ACTIVE',+ user: {+ NOT: {+ status: 'BANNED',+ },+ },+ // OR didn't work. so this workaround is fine because IT WORKS :3+ ...(allTags.includes(formattedStr as keyof typeof Tags)+ ? {+ tags: { every: { tag: formattedStr as Tags } },+ }+ : {+ difficulty: { in: [formattedStr as Difficulty] },+ }),+ },+ include: {+ _count: {+ select: { vote: true, comment: true },+ },+ user: true,+ },+ ...(take && {+ take,+ }),+ });+}diff --git a/apps/web/src/components/explore/index.tsx b/apps/web/src/components/explore/index.tsxindex 67644fdc..ac414a60 100644--- a/apps/web/src/components/explore/index.tsx+++ b/apps/web/src/components/explore/index.tsx@@ -1,5 +1,5 @@import { Suspense } from 'react';-import { prisma } from '@repo/db';+import { getChallangesByTagOrDifficulty } from './explore.action';import { ExploreSection } from './section';import { ExploreSectionSkeleton } from './section-skeleton';@@ -15,81 +15,41 @@ export async function Explore() {<span className="font-semibold dark:text-neutral-200">perfect</span> challenge!</p><Suspense fallback={<ExploreSectionSkeleton />}>- <ExploreSection fetcher={getEasyChallenges} title="Great for Beginners" />+ <ExploreSection+ title="Most Popular"+ fetcher={getChallangesByTagOrDifficulty}+ moreRoute="popular"+ />+ </Suspense>+ <Suspense fallback={<ExploreSectionSkeleton />}>+ <ExploreSection+ title="Newest"+ fetcher={getChallangesByTagOrDifficulty}+ moreRoute="newest"+ /></Suspense>-<Suspense fallback={<ExploreSectionSkeleton />}>- <ExploreSection fetcher={getMediumChallenges} title="Great for Enthusiasts" />+ <ExploreSection+ title="Great for Beginners"+ fetcher={getChallangesByTagOrDifficulty}+ moreRoute="easy"+ />+ </Suspense>+ <Suspense fallback={<ExploreSectionSkeleton />}>+ <ExploreSection+ title="Great for Enthusiasts"+ fetcher={getChallangesByTagOrDifficulty}+ moreRoute="medium"+ /></Suspense><Suspense fallback={<ExploreSectionSkeleton />}>- <ExploreSection fetcher={getHardChallenges} title="For the Experts" />+ <ExploreSection+ title="For the Experts"+ fetcher={getChallangesByTagOrDifficulty}+ moreRoute="hard"+ /></Suspense></div>);}--export type ExploreChallengeFetcher = typeof getEasyChallenges;-// TODO: this is trash-async function getEasyChallenges() {- return prisma.challenge.findMany({- where: {- status: 'ACTIVE',- difficulty: { in: ['EASY'] },- user: {- NOT: {- status: 'BANNED',- },- },- },- include: {- _count: {- select: { vote: true, comment: true },- },- user: true,- },- take: 6,- });-}--async function getMediumChallenges() {- return prisma.challenge.findMany({- where: {- status: 'ACTIVE',- difficulty: { in: ['MEDIUM'] },- user: {- NOT: {- status: 'BANNED',- },- },- },- include: {- _count: {- select: { vote: true, comment: true },- },- user: true,- },- take: 6,- });-}--async function getHardChallenges() {- return prisma.challenge.findMany({- where: {- status: 'ACTIVE',- difficulty: { in: ['HARD'] },- user: {- NOT: {- status: 'BANNED',- },- },- },- include: {- _count: {- select: { vote: true, comment: true },- },- user: true,- },- take: 6,- });-}diff --git a/apps/web/src/components/explore/section-carousel.tsx b/apps/web/src/components/explore/section-carousel.tsxindex 8cbc7f15..a106f927 100644--- a/apps/web/src/components/explore/section-carousel.tsx+++ b/apps/web/src/components/explore/section-carousel.tsx@@ -1,13 +1,13 @@'use client';-import Link from 'next/link';import type { Difficulty } from '@repo/db/types';-import { useEffect, useRef, useState } from 'react';import { ChevronRight } from '@repo/ui/icons';+import Link from 'next/link';+import { useEffect, useRef, useState } from 'react';import { ExploreCard } from './explore-card';-import type { ExploreChallengeFetcher } from '.';+import type { ExploreChallengeData } from './explore.action';interface Props {- challenges: Awaited<ReturnType<ExploreChallengeFetcher>>;+ challenges: Awaited<ExploreChallengeData>;}const difficultyToNumber: Record<Difficulty, number> = {diff --git a/apps/web/src/components/explore/section.tsx b/apps/web/src/components/explore/section.tsxindex 7e2072d4..7cb5baa1 100644--- a/apps/web/src/components/explore/section.tsx+++ b/apps/web/src/components/explore/section.tsx@@ -1,11 +1,17 @@-import { ChevronRight, Diamond } from '@repo/ui/icons';-import { Button } from '@repo/ui';+import { Diamond } from '@repo/ui/icons';+import type { ExploreChallengeData } from './explore.action';import { ExploreCarousel } from './section-carousel';-import type { ExploreChallengeFetcher } from '.';+import { ViewMoreButton } from './view-more-button';interface Props {title: string;- fetcher: ExploreChallengeFetcher;+ fetcher: (tagOrDifficulty: string, take: number) => ExploreChallengeData;+ /**+ * Slug for querying challenges on `/explore/<slug>`.+ * - Make sure it's either a `DIFFICULTY` or `TAGS`. Anything else will return in not found.+ * - Case doesn't matter since we trim & uppercase before querying db.+ */+ moreRoute: string;}const COLORS_BY_DIFFICULTY = {@@ -24,18 +30,8 @@ const TITLES_BY_DIFFICULTY = {EXTREME: 'bg-gradient-to-r from-orange-500 to-orange-500 dark:from-orange-500 dark:to-orange-200',};-const BUTTONS_BY_DIFFICULTY = {- BEGINNER:- 'bg-pink-500/10 text-pink-700 hover:text-pink-700 dark:text-pink-300 dark:bg-pink-300/10 hover:bg-pink-500/20 dark:hover:bg-pink-300/20',- EASY: 'bg-green-500/10 text-green-700 hover:text-green-700 dark:text-green-300 dark:bg-green-300/10 hover:bg-green-500/20 dark:hover:bg-green-300/20',- MEDIUM:- 'bg-yellow-500/10 text-yellow-700 hover:text-yellow-700 dark:text-yellow-300 dark:bg-yellow-300/10 hover:bg-yellow-500/20 dark:hover:bg-yellow-300/20',- HARD: 'bg-red-500/10 text-red-700 hover:text-red-700 dark:text-red-300 dark:bg-red-300/10 hover:bg-red-500/20 dark:hover:bg-red-300/20',- EXTREME:- 'bg-orange-500/10 text-orange-700 hover:text-orange-700 dark:text-orange-300 dark:bg-orange-300/10 hover:bg-orange-500/20 dark:hover:bg-orange-300/20',-};-export async function ExploreSection({ title, fetcher }: Props) {- const challenges = await fetcher();+export async function ExploreSection({ title, fetcher, moreRoute }: Props) {+ const challenges = await fetcher(moreRoute.trim().toUpperCase(), 6);return (<sectionclassName={`relative flex w-full flex-col overflow-hidden rounded-[2.5rem] ${@@ -72,14 +68,7 @@ export async function ExploreSection({ title, fetcher }: Props) {>{title}</h2>- <Button- className={`group items-center whitespace-nowrap rounded-full py-2 pl-4 pr-3 backdrop-blur-sm- ${BUTTONS_BY_DIFFICULTY[challenges[0]?.difficulty || 'BEGINNER']}`}- variant="ghost"- >- view more- <ChevronRight className="ml-2 h-4 w-4 stroke-[3] duration-300 group-hover:translate-x-1" />- </Button>+ <ViewMoreButton challenges={challenges} moreRoute={moreRoute} /></div><ExploreCarousel challenges={challenges} /></section>diff --git a/apps/web/src/components/explore/view-more-button.tsx b/apps/web/src/components/explore/view-more-button.tsxnew file mode 100644index 00000000..2d056fc9--- /dev/null+++ b/apps/web/src/components/explore/view-more-button.tsx@@ -0,0 +1,39 @@+'use client';++import { Button } from '@repo/ui';+import { ChevronRight } from 'lucide-react';+import { useRouter } from 'next/navigation';+import type { ExploreChallengeData } from './explore.action';++const BUTTONS_BY_DIFFICULTY = {+ BEGINNER:+ 'bg-pink-500/10 text-pink-700 hover:text-pink-700 dark:text-pink-300 dark:bg-pink-300/10 hover:bg-pink-500/20 dark:hover:bg-pink-300/20',+ EASY: 'bg-green-500/10 text-green-700 hover:text-green-700 dark:text-green-300 dark:bg-green-300/10 hover:bg-green-500/20 dark:hover:bg-green-300/20',+ MEDIUM:+ 'bg-yellow-500/10 text-yellow-700 hover:text-yellow-700 dark:text-yellow-300 dark:bg-yellow-300/10 hover:bg-yellow-500/20 dark:hover:bg-yellow-300/20',+ HARD: 'bg-red-500/10 text-red-700 hover:text-red-700 dark:text-red-300 dark:bg-red-300/10 hover:bg-red-500/20 dark:hover:bg-red-300/20',+ EXTREME:+ 'bg-orange-500/10 text-orange-700 hover:text-orange-700 dark:text-orange-300 dark:bg-orange-300/10 hover:bg-orange-500/20 dark:hover:bg-orange-300/20',+};++interface ViewMoreButtonProps {+ challenges: Awaited<ExploreChallengeData>;+ moreRoute: string;+}++export function ViewMoreButton({ challenges, moreRoute }: ViewMoreButtonProps) {+ const router = useRouter();+ return (+ <Button+ className={`group items-center whitespace-nowrap rounded-full py-2 pl-4 pr-3 backdrop-blur-sm+ ${BUTTONS_BY_DIFFICULTY[challenges[0]?.difficulty || 'BEGINNER']}`}+ onClick={() => {+ router.push(`/explore/${moreRoute}`);+ }}+ variant="ghost"+ >+ view more+ <ChevronRight className="ml-2 h-4 w-4 stroke-[3] duration-300 group-hover:translate-x-1" />+ </Button>+ );+}diff --git a/apps/web/src/components/ui/navigation.tsx b/apps/web/src/components/ui/navigation.tsxindex ee2c5ef8..71037808 100644--- a/apps/web/src/components/ui/navigation.tsx+++ b/apps/web/src/components/ui/navigation.tsx@@ -1,12 +1,7 @@'use client';-import { useTheme } from 'next-themes';-import { RoleTypes } from '@repo/db/types';-import { Loader2, LogIn, Moon, Plus, Settings, Settings2, Sun, User } from '@repo/ui/icons';import { signIn, signOut, useSession } from '@repo/auth/react';-import Link from 'next/link';-import { usePathname } from 'next/navigation';-import { useEffect, useState } from 'react';+import { RoleTypes } from '@repo/db/types';import {Button,DropdownMenu,@@ -16,6 +11,11 @@ import {DropdownMenuTrigger,navigationMenuTriggerStyle,} from '@repo/ui';+import { Loader2, LogIn, Moon, Plus, Settings, Settings2, Sun, User } from '@repo/ui/icons';+import { useTheme } from 'next-themes';+import Link from 'next/link';+import { usePathname } from 'next/navigation';+import { useEffect, useState } from 'react';export function Navigation() {const pathname = usePathname();diff --git a/apps/web/src/components/wizard/ChallengeCardEditor.tsx b/apps/web/src/components/wizard/ChallengeCardEditor.tsxindex 4b37999d..d61e2109 100644--- a/apps/web/src/components/wizard/ChallengeCardEditor.tsx+++ b/apps/web/src/components/wizard/ChallengeCardEditor.tsx@@ -1,6 +1,5 @@'use client';-import { useWatch } from 'react-hook-form';import {FormControl,FormField,@@ -16,8 +15,9 @@ import {Textarea,TypographyH3,} from '@repo/ui';+import { useWatch } from 'react-hook-form';import { ExploreCard } from '../explore/explore-card';-import type { ExploreChallengeFetcher } from '../explore';+import type { ExploreChallengeData } from '../explore/explore.action';import type { WizardForm } from '.';interface Props {@@ -30,7 +30,7 @@ export function ChallengeCardEditor({ form }: Props) {const shortDescription = useWatch({ control: form.control, name: 'shortDescription' });const data: Pick<- Awaited<ReturnType<ExploreChallengeFetcher>>[0],+ Awaited<ExploreChallengeData>[0],'_count' | 'difficulty' | 'name' | 'shortDescription' | 'updatedAt' | 'user'> = {difficulty,diff --git a/packages/db/schema.prisma b/packages/db/schema.prismaindex bef0a6b3..47ed7adf 100644--- a/packages/db/schema.prisma+++ b/packages/db/schema.prisma@@ -11,6 +11,11 @@ datasource db {relationMode = "prisma"}+enum Tags {+ POPULAR+ NEWEST+}+enum Difficulty {BEGINNEREASY@@ -134,6 +139,12 @@ model VerificationToken {@@unique([identifier, token])}+model Tag {+ id String @id @default(uuid())+ tag Tags @unique+ challenges Challenge[]+}+model Challenge {id Int @id @default(autoincrement())createdAt DateTime @default(now())@@ -151,6 +162,7 @@ model Challenge {user User @relation(fields: [userId], references: [id])userId String+ tags Tag[]vote Vote[]submission Submission[]comment Comment[]