Skip to content

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

feat: ability to report challenge comments. (#167)

Ayushd0b02b5e867e
diff --git a/src/components/challenge/comments/comment.action.ts b/src/components/challenge/comments/comment.action.ts
index 274ee21b..e20e1105 100644
--- a/src/components/challenge/comments/comment.action.ts
+++ b/src/components/challenge/comments/comment.action.ts
@@ -1,15 +1,24 @@
'use server';
import { getServerSession } from 'next-auth';
+import { CommentReportSchemaType } from '~/components/challenge/comments/comment';
import { authOptions } from '~/server/auth';
import { prisma } from '~/server/db';
+/**
+ * The function creates a comments with given
+ * text and challenge id.
+ * @param challengeId The challenge Id.
+ * @param text The comment text.
+ */
export async function addChallengeComment(challengeId: number, text: string) {
const session = await getServerSession(authOptions);
+ // 1. Checks.
if (!session?.user.id) return 'unauthorized';
if (text.length === 0) return 'text_is_empty';
+ // 2. Create a new comment.
return await prisma.challengeComment.create({
data: {
challengeId,
@@ -18,3 +27,42 @@ export async function addChallengeComment(challengeId: number, text: string) {
},
});
}
+
+/**
+ * Creates a new report on a challenge comment with a
+ * few parameters describing the report.
+ * @param formValues Accepts {@link CommentReportSchemaType}.
+ * @param commentId The comment id.
+ * @returns
+ */
+export async function reportChallengeComment(
+ formValues: CommentReportSchemaType,
+ commentId: number,
+) {
+ const session = await getServerSession(authOptions);
+ const { bullying, hate_speech, spam, text, threat } = formValues;
+
+ // 1. Checks.
+ if (!session?.user.id) return 'unauthorized';
+
+ // 2. If report already exist's return.
+ const report = await prisma.commentReport.findFirst({
+ where: {
+ challengeCommentId: commentId,
+ },
+ });
+ if (report) return 'already_exists';
+
+ // 3. Create new report.
+ return await prisma.commentReport.create({
+ data: {
+ authorId: session.user.id,
+ bullying: bullying,
+ hateSpeech: hate_speech,
+ spam: spam,
+ threats: threat,
+ text: text,
+ challengeCommentId: commentId,
+ },
+ });
+}
diff --git a/src/components/challenge/comments/comment.tsx b/src/components/challenge/comments/comment.tsx
new file mode 100644
index 00000000..99da799d
--- /dev/null
+++ b/src/components/challenge/comments/comment.tsx
@@ -0,0 +1,272 @@
+import { zodResolver } from '@hookform/resolvers/zod';
+import { Delete } from 'lucide-react';
+import { useState } from 'react';
+import { useForm } from 'react-hook-form';
+import { z } from 'zod';
+import { ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';
+import { Dialog, DialogContent, DialogHeader, DialogTitle } from '~/components/ui/dialog';
+import { getRelativeTime } from '~/utils/relativeTime';
+import { Button } from '../../ui/button';
+import { Checkbox } from '../../ui/checkbox';
+import { Form, FormField, FormItem } from '../../ui/form';
+import { Textarea } from '../../ui/textarea';
+import { TypographyLarge } from '../../ui/typography/large';
+import { toast } from '../../ui/use-toast';
+import { reportChallengeComment } from './comment.action';
+
+interface CommentProps {
+ comment: ChallengeRouteData['comment'][number];
+}
+
+const commentReportSchema = z
+ .object({
+ spam: z.boolean().optional(),
+ threat: z.boolean().optional(),
+ hate_speech: z.boolean().optional(),
+ bullying: z.boolean().optional(),
+ text: z.string().optional(),
+ })
+ .refine(
+ (obj) => {
+ const { spam, threat, hate_speech, bullying, text } = obj;
+ return spam || threat || hate_speech || bullying || (text !== undefined && text !== '');
+ },
+ {
+ path: ['text'],
+ message: 'Your report should include an issue or a reason.',
+ },
+ );
+
+export type CommentReportSchemaType = z.infer<typeof commentReportSchema>;
+
+const Comment = ({ comment }: CommentProps) => {
+ // State
+ const [dialogOpen, setDialogOpen] = useState(false);
+
+ // Hooks
+ const form = useForm<CommentReportSchemaType>({
+ resolver: zodResolver(commentReportSchema),
+ mode: 'onChange',
+ defaultValues: {
+ bullying: false,
+ hate_speech: false,
+ spam: false,
+ text: '',
+ threat: false,
+ },
+ });
+
+ // Functions
+ async function handleCommentReport(data: CommentReportSchemaType) {
+ try {
+ const res = await reportChallengeComment(data, comment.id);
+ if (res === 'unauthorized') {
+ toast({
+ title: 'Unauthorized',
+ variant: 'destructive',
+ description: <p>You&apos;re not authorized to perform the action.</p>,
+ });
+ } else if (res === 'already_exists') {
+ toast({
+ title: 'Already Reported',
+ description: <p>The comment you&apos;re trying to report has already been reported.</p>,
+ });
+ } else {
+ toast({
+ title: 'Reported',
+ variant: 'success',
+ description: <p>The comment has successfully been reported.</p>,
+ });
+ setDialogOpen(false);
+ form.reset();
+ }
+ } catch (e) {
+ toast({
+ title: 'Uh Oh!',
+ variant: 'destructive',
+ description: <p>There was an error while trying to report the comment.</p>,
+ });
+ }
+ }
+
+ const text = form.watch('text');
+
+ return (
+ <div className="flex cursor-pointer flex-col gap-2 p-4 pt-2 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-zinc-700/50">
+ <div className="flex justify-between">
+ <div className="flex items-center gap-2">
+ <span className="my-auto max-w-fit rounded-full bg-neutral-200 p-1 px-2 text-xs font-bold text-neutral-500 dark:bg-zinc-700 dark:text-neutral-400">
+ @&nbsp;{comment.user.name}
+ </span>
+ <span className="text-sm text-neutral-500">{getRelativeTime(comment.createdAt)}</span>
+ </div>
+ <button
+ onClick={() => {
+ setDialogOpen(true);
+ }}
+ className="flex text-sm text-neutral-400 hover:text-neutral-400 hover:underline dark:text-neutral-600"
+ >
+ Report
+ </button>
+ </div>
+ <p>{comment.text}</p>
+ <Dialog
+ open={dialogOpen}
+ onOpenChange={() => {
+ setDialogOpen(!dialogOpen);
+ }}
+ >
+ <DialogContent className="max-w-lg space-y-4">
+ <DialogHeader>
+ <DialogTitle>Report Comment</DialogTitle>
+ </DialogHeader>
+ <div className="space-y-2">
+ <div className="flex flex-col space-y-2 rounded-md border-2 px-2 py-3">
+ <div className="flex items-center gap-2">
+ <span className="my-auto max-w-fit rounded-full bg-neutral-200 p-1 px-2 text-xs font-bold text-neutral-500 dark:bg-zinc-700 dark:text-neutral-400">
+ @&nbsp;{comment.user.name}
+ </span>
+ <span className="text-sm text-neutral-500">
+ {getRelativeTime(comment.createdAt)}
+ </span>
+ </div>
+ <p>{comment.text}</p>
+ </div>
+ <Form {...form}>
+ {/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
+ <form onSubmit={form.handleSubmit(handleCommentReport)}>
+ <div className="flex flex-col space-y-2">
+ <TypographyLarge>Issues</TypographyLarge>
+ <FormField
+ control={form.control}
+ name="bullying"
+ render={({ field }) => {
+ return (
+ <FormItem>
+ <div className="flex items-center gap-4">
+ <Checkbox
+ id="bullying"
+ checked={field.value as boolean}
+ onCheckedChange={(e) => {
+ if (typeof e === 'boolean') {
+ field.onChange(e);
+ }
+ }}
+ />
+ <label htmlFor="bullying">The comment suggests bullying.</label>
+ </div>
+ </FormItem>
+ );
+ }}
+ />
+ <FormField
+ control={form.control}
+ name="hate_speech"
+ render={({ field }) => {
+ return (
+ <FormItem>
+ <div className="flex items-center gap-4">
+ <Checkbox
+ id="hate_speech"
+ checked={field.value as boolean}
+ onCheckedChange={(e) => {
+ if (typeof e === 'boolean') {
+ field.onChange(e);
+ }
+ }}
+ />
+ <label htmlFor="hate_speech">The comment suggests hate speech.</label>
+ </div>
+ </FormItem>
+ );
+ }}
+ />
+ <FormField
+ control={form.control}
+ name="spam"
+ render={({ field }) => {
+ return (
+ <FormItem>
+ <div className="flex items-center gap-4">
+ <Checkbox
+ id="spam"
+ checked={field.value as boolean}
+ onCheckedChange={(e) => {
+ if (typeof e === 'boolean') {
+ field.onChange(e);
+ }
+ }}
+ />
+ <label htmlFor="spam">The comment suggests spam.</label>
+ </div>
+ </FormItem>
+ );
+ }}
+ />
+ <FormField
+ control={form.control}
+ name="threat"
+ render={({ field }) => {
+ return (
+ <FormItem>
+ <div className="flex items-center gap-4">
+ <Checkbox
+ id="threat"
+ checked={field.value as boolean}
+ onCheckedChange={(e) => {
+ if (typeof e === 'boolean') {
+ field.onChange(e);
+ }
+ }}
+ />
+ <label htmlFor="threat">The comment suggests threat/s.</label>
+ </div>
+ </FormItem>
+ );
+ }}
+ />
+ <FormField
+ control={form.control}
+ name="text"
+ render={({ field }) => {
+ return (
+ <FormItem>
+ <div className="flex flex-row items-center gap-2">
+ <label htmlFor="text">
+ <TypographyLarge>Other</TypographyLarge>
+ </label>
+ {text !== undefined && text.length > 0 && (
+ <Delete
+ className="h-5 w-5 hover:cursor-pointer"
+ onClick={() => {
+ form.setValue('text', '');
+ }}
+ />
+ )}
+ </div>
+ <Textarea value={field.value} onChange={field.onChange} />
+ </FormItem>
+ );
+ }}
+ />
+ {form.formState.errors.text?.message && (
+ <p className={'text-sm font-medium text-destructive'}>
+ {form.formState.errors.text.message}
+ </p>
+ )}
+ </div>
+ <div className="flex pt-4">
+ <Button type="submit" className="w-full">
+ Report
+ </Button>
+ </div>
+ </form>
+ </Form>
+ </div>
+ </DialogContent>
+ </Dialog>
+ </div>
+ );
+};
+
+export default Comment;
diff --git a/src/components/challenge/comments/index.tsx b/src/components/challenge/comments/index.tsx
index 13efa859..a6486b96 100644
--- a/src/components/challenge/comments/index.tsx
+++ b/src/components/challenge/comments/index.tsx
@@ -3,8 +3,8 @@ import { ChevronDown, MessageCircle } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { type ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';
+import Comment from '~/components/challenge/comments/comment';
import { Button } from '~/components/ui/button';
-import Comment from '~/components/ui/comment';
import { toast } from '~/components/ui/use-toast';
import { Input } from '../../ui/input';
import NoComments from '../nocomments';
@@ -75,19 +75,14 @@ const Comments = ({ challenge }: Props) => {
></ChevronDown>
</button>
<div
- className={clsx('flex flex-col flex-col-reverse overscroll-contain duration-300', {
+ className={clsx('flex flex-col-reverse overscroll-contain duration-300', {
'h-64 overflow-y-auto': showComments,
'h-0 overflow-y-hidden': !showComments,
})}
>
{challenge.comment.length === 0 && <NoComments></NoComments>}
{challenge.comment.map((comment) => (
- <Comment
- key={comment.id}
- userId={comment.user.name ?? ''}
- text={comment.text}
- createdAt={comment.createdAt}
- />
+ <Comment key={comment.id} comment={comment} />
))}
</div>
<div className="m-2 mt-0 flex items-center justify-between gap-2 rounded-xl bg-background/90 bg-neutral-100 p-1 pr-2 backdrop-blur-sm dark:border-zinc-700 dark:bg-zinc-700/90">
diff --git a/src/components/ui/comment.tsx b/src/components/ui/comment.tsx
deleted file mode 100644
index d506517f..00000000
--- a/src/components/ui/comment.tsx
+++ /dev/null
@@ -1,23 +0,0 @@
-import { getRelativeTime } from '~/utils/relativeTime';
-
-type Props = {
- userId: string;
- text: string;
- createdAt: Date;
-};
-
-const Comment = ({ userId, text, createdAt }: Props) => {
- return (
- <div className="flex cursor-pointer flex-col gap-2 p-4 pt-2 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-zinc-700/50">
- <div className="flex items-center gap-2">
- <span className="my-auto max-w-fit rounded-full bg-neutral-200 p-1 px-2 text-xs font-bold text-neutral-500 dark:bg-zinc-700 dark:text-neutral-400">
- @&nbsp;{userId}
- </span>
- <span className="text-sm text-neutral-500">{getRelativeTime(createdAt)}</span>
- </div>
- <p>{text}</p>
- </div>
- );
-};
-
-export default Comment;