From 289e14ece45d0e0adc8c826361e7c02ceecdb09c Mon Sep 17 00:00:00 2001 From: nikkkkj <280947781+nikkkkj@users.noreply.github.com> Date: Fri, 15 May 2026 01:37:41 +0800 Subject: [PATCH] Add bounty form validation errors --- src/components/create-bounty-form.tsx | 54 +++++++++++++++++---------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/components/create-bounty-form.tsx b/src/components/create-bounty-form.tsx index 9fc27ac..8096cea 100644 --- a/src/components/create-bounty-form.tsx +++ b/src/components/create-bounty-form.tsx @@ -2,18 +2,22 @@ import { useRef, useState } from "react"; -// BUG 2: Form validation - allows negative numbers and empty titles (see validation below) - type CreateBountyFormProps = { onSubmit: (bounty: { title: string; reward: number; difficulty: string }) => void; }; +type FormErrors = { + title?: string; + reward?: string; +}; + export function CreateBountyForm({ onSubmit }: CreateBountyFormProps) { const [title, setTitle] = useState(""); const [reward, setReward] = useState(""); const [difficulty, setDifficulty] = useState("Easy"); const [submitting, setSubmitting] = useState(false); const [submissions, setSubmissions] = useState([]); + const [errors, setErrors] = useState({}); const isSubmittingRef = useRef(false); const handleSubmit = async (e: React.FormEvent) => { @@ -25,20 +29,23 @@ export function CreateBountyForm({ onSubmit }: CreateBountyFormProps) { setSubmitting(true); try { - // Validation: check for empty title and negative reward + const nextErrors: FormErrors = {}; if (!title.trim()) { - alert("Title is required"); - isSubmittingRef.current = false; - setSubmitting(false); - return; + nextErrors.title = "Title is required"; } + const rewardNum = Number(reward); if (isNaN(rewardNum) || rewardNum <= 0) { - alert("Reward must be a positive number"); + nextErrors.reward = "Reward must be a positive number"; + } + + if (Object.keys(nextErrors).length > 0) { + setErrors(nextErrors); isSubmittingRef.current = false; setSubmitting(false); return; } + setErrors({}); // Simulate API delay await new Promise((resolve) => setTimeout(resolve, 1000)); @@ -64,7 +71,7 @@ export function CreateBountyForm({ onSubmit }: CreateBountyFormProps) {

Create New Bounty

-
+
@@ -91,14 +102,19 @@ export function CreateBountyForm({ onSubmit }: CreateBountyFormProps) { setReward(e.target.value)} + onChange={(e) => { + setReward(e.target.value); + setErrors((prev) => ({ ...prev, reward: undefined })); + }} className="w-full rounded-lg border border-slate-200 px-3 py-2" placeholder="100" - min="1" + aria-invalid={Boolean(errors.reward)} + aria-describedby={errors.reward ? "reward-error" : undefined} /> - {/* FIX 2: Show validation error */} {errors.reward && ( -

{errors.reward}

+

+ {errors.reward} +

)}
@@ -141,4 +157,4 @@ export function CreateBountyForm({ onSubmit }: CreateBountyFormProps) { )} ); -} \ No newline at end of file +}