From d442bae300bff04cb2f2577374916c0115ca3b75 Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 15 Nov 2025 10:03:56 +0000 Subject: [PATCH] fix: validation on HTML input elements level --- .../frontend/src/components/PetitionForm.tsx | 57 ++++++++++--------- packages/frontend/src/state/index.tsx | 2 +- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/frontend/src/components/PetitionForm.tsx b/packages/frontend/src/components/PetitionForm.tsx index ee78a1a..31018ee 100644 --- a/packages/frontend/src/components/PetitionForm.tsx +++ b/packages/frontend/src/components/PetitionForm.tsx @@ -5,50 +5,49 @@ import { Textarea } from "@/components/ui/textarea"; import { Checkbox } from "@/components/ui/checkbox"; import { Label } from "@/components/ui/label"; import { usePetitions } from "@/state"; +import { useToast } from "@/hooks/use-toast"; interface PetitionFormProps { compact?: boolean; } -const setUndefinedOrValue = ( - setter: (value: string | undefined) => void, - value: string, -) => { - if (value.length === 0) { - setter(undefined); - } else { - setter(value); - } -}; - export const PetitionForm = ({ compact = false }: PetitionFormProps) => { - const [name, setName] = useState(); - const [comment, setComment] = useState(); - const [email, setEmail] = useState(); - + const [email, setEmail] = useState(""); + const [name, setName] = useState(""); + const [comment, setComment] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const [isAnonymous, setIsAnonymous] = useState(false); const { onSignPetition } = usePetitions(); + const { toast } = useToast(); - const handleSubmit = async (e: React.FormEvent) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (isSubmitting) { return; } + const optionalName = name.trim().length > 0 ? name.trim() : undefined; + const optionalComment = + comment.trim().length > 0 ? comment.trim() : undefined; + setIsSubmitting(true); await onSignPetition({ - email, - comment, - name, + email: email.trim(), + name: optionalName, + comment: optionalComment, }); - setName(undefined); - setEmail(undefined); - setComment(undefined); + toast({ + title: "Success!", + description: "Thank you for signing the petition.", + }); + + setName(""); + setEmail(""); + setComment(""); setIsSubmitting(false); }; @@ -76,9 +75,11 @@ export const PetitionForm = ({ compact = false }: PetitionFormProps) => {
setUndefinedOrValue(setName, e.target.value)} + value={name} + onChange={(e) => setName(e.target.value)} required + minLength={1} + maxLength={30} className="bg-background border-border" />
@@ -89,7 +90,7 @@ export const PetitionForm = ({ compact = false }: PetitionFormProps) => { type="email" placeholder="Your Email" value={email} - onChange={(e) => setUndefinedOrValue(setEmail, e.target.value)} + onChange={(e) => setEmail(e.target.value)} required className="bg-background border-border" /> @@ -98,8 +99,10 @@ export const PetitionForm = ({ compact = false }: PetitionFormProps) => {