Skip to content

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

chore: refactor + fix comment deletion bug

Chris Bautista72e706a24af7
diff --git a/apps/web/src/app/explore/[slug]/page.tsx b/apps/web/src/app/explore/[slug]/page.tsx
index 2d509bd3..ccd65e5b 100644
--- a/apps/web/src/app/explore/[slug]/page.tsx
+++ b/apps/web/src/app/explore/[slug]/page.tsx
@@ -1,6 +1,6 @@
import { Suspense } from 'react';
-import { ExploreSlug } from '~/components/explore/explore-slug';
-import { ExploreSlugSkeleton } from '~/components/explore/explore-slug-skeleton';
+import { ExploreSlugSkeleton } from '../_components/explore-slug-skeleton';
+import { ExploreSlug } from '../_components/explore-slug';
export const dynamic = 'force-dynamic';
diff --git a/apps/web/src/components/explore/explore-card-skeleton.tsx b/apps/web/src/app/explore/_components/explore-card-skeleton.tsx
similarity index 100%
rename from apps/web/src/components/explore/explore-card-skeleton.tsx
rename to apps/web/src/app/explore/_components/explore-card-skeleton.tsx
diff --git a/apps/web/src/components/explore/explore-card.tsx b/apps/web/src/app/explore/_components/explore-card.tsx
similarity index 100%
rename from apps/web/src/components/explore/explore-card.tsx
rename to apps/web/src/app/explore/_components/explore-card.tsx
diff --git a/apps/web/src/components/explore/explore-slug-skeleton.tsx b/apps/web/src/app/explore/_components/explore-slug-skeleton.tsx
similarity index 100%
rename from apps/web/src/components/explore/explore-slug-skeleton.tsx
rename to apps/web/src/app/explore/_components/explore-slug-skeleton.tsx
diff --git a/apps/web/src/components/explore/explore-slug.tsx b/apps/web/src/app/explore/_components/explore-slug.tsx
similarity index 100%
rename from apps/web/src/components/explore/explore-slug.tsx
rename to apps/web/src/app/explore/_components/explore-slug.tsx
diff --git a/apps/web/src/components/explore/explore.action.ts b/apps/web/src/app/explore/_components/explore.action.ts
similarity index 100%
rename from apps/web/src/components/explore/explore.action.ts
rename to apps/web/src/app/explore/_components/explore.action.ts
diff --git a/apps/web/src/components/explore/index.tsx b/apps/web/src/app/explore/_components/index.tsx
similarity index 100%
rename from apps/web/src/components/explore/index.tsx
rename to apps/web/src/app/explore/_components/index.tsx
diff --git a/apps/web/src/components/explore/section-carousel.tsx b/apps/web/src/app/explore/_components/section-carousel.tsx
similarity index 100%
rename from apps/web/src/components/explore/section-carousel.tsx
rename to apps/web/src/app/explore/_components/section-carousel.tsx
diff --git a/apps/web/src/components/explore/section-skeleton.tsx b/apps/web/src/app/explore/_components/section-skeleton.tsx
similarity index 100%
rename from apps/web/src/components/explore/section-skeleton.tsx
rename to apps/web/src/app/explore/_components/section-skeleton.tsx
diff --git a/apps/web/src/components/explore/section.tsx b/apps/web/src/app/explore/_components/section.tsx
similarity index 100%
rename from apps/web/src/components/explore/section.tsx
rename to apps/web/src/app/explore/_components/section.tsx
diff --git a/apps/web/src/components/explore/view-more-button.tsx b/apps/web/src/app/explore/_components/view-more-button.tsx
similarity index 100%
rename from apps/web/src/components/explore/view-more-button.tsx
rename to apps/web/src/app/explore/_components/view-more-button.tsx
diff --git a/apps/web/src/app/explore/page.tsx b/apps/web/src/app/explore/page.tsx
index 42e3cd63..11f2d77d 100644
--- a/apps/web/src/app/explore/page.tsx
+++ b/apps/web/src/app/explore/page.tsx
@@ -1,4 +1,4 @@
// CI fails without this
export const dynamic = 'force-dynamic';
-export { Explore as default } from '~/components/explore';
+export { Explore as default } from './_components/';
diff --git a/apps/web/src/components/challenge/comments/comment.action.ts b/apps/web/src/components/challenge/comments/comment.action.ts
index c7b5e4d9..11da7740 100644
--- a/apps/web/src/components/challenge/comments/comment.action.ts
+++ b/apps/web/src/components/challenge/comments/comment.action.ts
@@ -1,8 +1,8 @@
'use server';
-import type { CommentRoot } from '@repo/db/types';
-import { prisma } from '@repo/db';
import { getServerAuthSession } from '@repo/auth/server';
+import { prisma } from '@repo/db';
+import type { Comment, CommentRoot, PrismaClient } from '@repo/db/types';
/**
*
@@ -89,12 +89,31 @@ export async function deleteComment(comment_id: number) {
if (!session?.user.id) return 'unauthorized';
if (!comment_id) return 'invalid_comment';
- return await prisma.comment.delete({
+ const rootComment = await prisma.comment.findFirstOrThrow({
where: {
- userId: session.user.id,
id: comment_id,
},
});
+
+ await deleteCommentWithChildren(prisma, rootComment);
+}
+
+async function deleteCommentWithChildren(prisma: PrismaClient, node: Comment) {
+ const children = await prisma.comment.findMany({
+ where: {
+ parentId: node.id,
+ },
+ });
+
+ for (const child of children) {
+ await deleteCommentWithChildren(prisma, child);
+ }
+
+ await prisma.comment.delete({
+ where: {
+ id: node.id,
+ },
+ });
}
export type CommentsByChallengeId = NonNullable<