Skip to content

Commit

Permalink
refactor: Update report module form and types
Browse files Browse the repository at this point in the history
  • Loading branch information
EdSDR committed Oct 1, 2024
1 parent 3549098 commit 2f61427
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
40 changes: 16 additions & 24 deletions apps/commune-validator/src/app/components/report-module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand All @@ -32,7 +30,7 @@ interface ReportModuleProps {
export function ReportModule({ moduleId }: ReportModuleProps) {
const [modalOpen, setModalOpen] = useState(false);
const [formData, setFormData] = useState<ReportFormData>({
reason: ReportReason.spam,
reason: "SPAM",
content: "",
});
const [errors, setErrors] = useState<Partial<ReportFormData>>({});
Expand All @@ -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({});
},
});
Expand All @@ -55,11 +53,7 @@ export function ReportModule({ moduleId }: ReportModuleProps) {
e: React.ChangeEvent<HTMLSelectElement | HTMLTextAreaElement>,
) => {
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 => {
Expand Down Expand Up @@ -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"
>
<option value={ReportReason.spam}>Spam</option>
<option value={ReportReason.harassment}>Harassment</option>
<option value={ReportReason.hateSpeech}>Hate Speech</option>
<option value={ReportReason.violence}>Violence</option>
<option value={ReportReason.sexualContent}>
Sexual Content
</option>
<option value="SPAM">Spam</option>
<option value="VIOLENCE">Violence</option>
<option value="HARASSMENT">Harassment</option>
<option value="HATE_SPEECH">Hate Speech</option>
<option value="SEXUAL_CONTENT">Sexual Content</option>
</select>
{errors.reason && (
<p className="mt-1 text-xs text-red-500">{errors.reason}</p>
Expand Down
4 changes: 4 additions & 0 deletions apps/commune-validator/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import type { AppRouter } from "@commune-ts/api";
export type Subnet = NonNullable<
inferProcedureOutput<AppRouter["subnet"]["byId"]>
>;

export type ReportReason = NonNullable<
inferProcedureOutput<AppRouter["module"]["byReport"]>
>["reason"];
7 changes: 7 additions & 0 deletions packages/api/src/router/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() }))
Expand Down

0 comments on commit 2f61427

Please sign in to comment.