Connect, collaborate, and grow with a community of TypeScript developers
fix: challenges count of logged in user is visible everywhere (#1136)
Ayush Kumar Yadav1cb9138e6165
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/_actions.ts b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/_actions.tsindex 5990303a..18f93a88 100644--- a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/_actions.ts+++ b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/_actions.ts@@ -67,12 +67,11 @@ export async function getChallengeHistoryByCategory(type: HistoryType, userId: s);}-export async function getSolvedChallenges() {- const session = await auth();-+export async function getSolvedChallenges(userId: string) {+ // Get all successful submissions for the userconst successfulSubmissions = await prisma.submission.findMany({where: {- userId: session?.user.id,+ userId,isSuccessful: true,},select: {@@ -85,10 +84,12 @@ export async function getSolvedChallenges() {distinct: ['challengeId'],});+ // Get all challenges solved and group by difficultyconst challengesSolved = await prisma.challenge.groupBy({by: ['difficulty'],where: {id: {+ // if the user has solved challenge, it will be in the successfulSubmissions arrayin: successfulSubmissions.map((challenge) => challenge.challenge.id),},},@@ -104,6 +105,7 @@ export async function getSolvedChallenges() {},});+ // Calculate percentage, total solved and total challengesconst totalSolved = challengesSolved.reduce((acc, challenge) => acc + challenge._count._all, 0);const totalChallenges = allChallenges.reduce((acc, challenge) => acc + challenge._count._all, 0);const percentage = ((totalSolved / totalChallenges) * 100).toFixed(1);@@ -137,7 +139,7 @@ export async function getSolvedChallenges() {},};- // assign values+ // assign values to the challenges objectallChallenges.forEach((challenge) => {const difficulty = challenge.difficulty as (typeof DIFFICULTIES)[number];challenges[difficulty].total = challenge._count._all;diff --git a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/challenges-progress.tsx b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/challenges-progress.tsxindex b28f308b..a35af1b0 100644--- a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/challenges-progress.tsx+++ b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/challenges-progress.tsx@@ -5,8 +5,13 @@ import { getSolvedChallenges } from './_actions';export const DIFFICULTIES = ['BEGINNER', 'EASY', 'MEDIUM', 'HARD', 'EXTREME'] as const;-export async function ChallengesProgress() {- const { challenges, percentage, totalChallenges, totalSolved } = await getSolvedChallenges();+interface ChallengesProgressProps {+ userId: string;+}++export async function ChallengesProgress({ userId }: ChallengesProgressProps) {+ const { challenges, percentage, totalChallenges, totalSolved } =+ await getSolvedChallenges(userId);return (<Card className="flex-grow"><CardHeader>diff --git a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/overview-tab.tsx b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/overview-tab.tsxindex a6edff96..b957566f 100644--- a/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/overview-tab.tsx+++ b/apps/web/src/app/[locale]/(profile)/[username]/_components/dashboard/overview-tab.tsx@@ -16,7 +16,7 @@ export async function OverviewTab({ user }: Props) {return (<div className="col-span-4 flex flex-col gap-6 md:min-h-[calc(100vh_-_56px_-_6rem)]"><div className="flex flex-col md:flex-row md:gap-6">- <ChallengesProgress />+ <ChallengesProgress userId={user.id} />{/* //Todo: Filling with void for now, may put contributions / something else */}<div className="max-w-sm flex-grow" /></div>