From 2f61427d2be494a2d53fbe55415c5005f74edf1e Mon Sep 17 00:00:00 2001 From: Ed Castro Date: Tue, 1 Oct 2024 09:58:27 -0300 Subject: [PATCH] refactor: Update report module form and types --- .../src/app/components/report-module.tsx | 40 ++++++++----------- apps/commune-validator/src/utils/types.ts | 4 ++ packages/api/src/router/module.ts | 7 ++++ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/apps/commune-validator/src/app/components/report-module.tsx b/apps/commune-validator/src/app/components/report-module.tsx index 00e57eb3..99f0a8f6 100644 --- a/apps/commune-validator/src/app/components/report-module.tsx +++ b/apps/commune-validator/src/app/components/report-module.tsx @@ -10,16 +10,14 @@ import { toast } from "@commune-ts/providers/use-toast"; import { api } from "~/trpc/react"; -enum ReportReason { - spam = "spam", - harassment = "harassment", - hateSpeech = "hateSpeech", - violence = "violence", - sexualContent = "sexualContent", -} - const reportSchema = z.object({ - reason: z.nativeEnum(ReportReason), + reason: z.enum([ + "SPAM", + "VIOLENCE", + "HARASSMENT", + "HATE_SPEECH", + "SEXUAL_CONTENT", + ] as const), content: z.string().min(10).max(500), }); @@ -32,7 +30,7 @@ interface ReportModuleProps { export function ReportModule({ moduleId }: ReportModuleProps) { const [modalOpen, setModalOpen] = useState(false); const [formData, setFormData] = useState({ - reason: ReportReason.spam, + reason: "SPAM", content: "", }); const [errors, setErrors] = useState>({}); @@ -42,7 +40,7 @@ export function ReportModule({ moduleId }: ReportModuleProps) { const reportModuleMutation = api.module.createModuleReport.useMutation({ onSuccess: () => { setModalOpen(false); - setFormData({ reason: ReportReason.spam, content: "" }); + setFormData({ reason: "SPAM", content: "" }); setErrors({}); }, }); @@ -55,11 +53,7 @@ export function ReportModule({ moduleId }: ReportModuleProps) { e: React.ChangeEvent, ) => { const { name, value } = e.target; - if (name === "reason") { - setFormData((prev) => ({ ...prev, [name]: value as ReportReason })); - } else { - setFormData((prev) => ({ ...prev, [name]: value })); - } + setFormData((prev) => ({ ...prev, [name]: value })); }; const validateForm = (): boolean => { @@ -128,15 +122,13 @@ export function ReportModule({ moduleId }: ReportModuleProps) { name="reason" value={formData.reason} onChange={handleInputChange} - className="w-full border border-gray-300 bg-black/40 p-2" + className="w-full border border-gray-300 bg-black/40 p-2" > - - - - - + + + + + {errors.reason && (

{errors.reason}

diff --git a/apps/commune-validator/src/utils/types.ts b/apps/commune-validator/src/utils/types.ts index 80f23d95..2858c224 100644 --- a/apps/commune-validator/src/utils/types.ts +++ b/apps/commune-validator/src/utils/types.ts @@ -5,3 +5,7 @@ import type { AppRouter } from "@commune-ts/api"; export type Subnet = NonNullable< inferProcedureOutput >; + +export type ReportReason = NonNullable< + inferProcedureOutput +>["reason"]; diff --git a/packages/api/src/router/module.ts b/packages/api/src/router/module.ts index eea7f4c6..d1438a5f 100644 --- a/packages/api/src/router/module.ts +++ b/packages/api/src/router/module.ts @@ -87,6 +87,13 @@ export const moduleRouter = { }, }; }), + byReport: publicProcedure + .input(z.object({ id: z.number() })) + .query(({ ctx, input }) => { + return ctx.db.query.moduleReport.findFirst({ + where: eq(moduleReport.id, input.id), + }); + }), // POST deleteUserModuleData: publicProcedure .input(z.object({ userKey: z.string() }))