From 0270c2f2dc33ad5e40e6b4d0af557e5e2109f7b8 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 14:19:23 +0000 Subject: [PATCH] Refactor: Update petition form --- src/components/PetitionForm.tsx | 96 +++++++++++++++++++++++++-------- src/pages/Index.tsx | 28 +++++----- 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/src/components/PetitionForm.tsx b/src/components/PetitionForm.tsx index 18a6865..40d2baf 100644 --- a/src/components/PetitionForm.tsx +++ b/src/components/PetitionForm.tsx @@ -12,20 +12,33 @@ const petitionSchema = z.object({ comment: z.string().trim().max(1000, "Comment must be less than 1000 characters").optional(), }); -export const PetitionForm = () => { +const anonymousSchema = z.object({ + email: z.string().trim().email("Invalid email address").max(255, "Email must be less than 255 characters"), +}); + +interface PetitionFormProps { + compact?: boolean; +} + +export const PetitionForm = ({ compact = false }: PetitionFormProps) => { const [formData, setFormData] = useState({ name: "", email: "", comment: "", }); const [isSubmitting, setIsSubmitting] = useState(false); + const [isAnonymous, setIsAnonymous] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - // Validate input + // Validate input based on mode try { - petitionSchema.parse(formData); + if (isAnonymous) { + anonymousSchema.parse({ email: formData.email }); + } else { + petitionSchema.parse(formData); + } } catch (error) { if (error instanceof z.ZodError) { toast.error(error.errors[0].message); @@ -39,15 +52,16 @@ export const PetitionForm = () => { const { error } = await supabase .from('petition_signatures') .insert([{ - name: formData.name.trim(), + name: isAnonymous ? 'Anonymous' : formData.name.trim(), email: formData.email.trim(), - comment: formData.comment.trim() || null, + comment: isAnonymous ? null : (formData.comment.trim() || null), }]); if (error) throw error; toast.success("Thank you for signing! Your voice matters."); setFormData({ name: "", email: "", comment: "" }); + setIsAnonymous(false); } catch (error) { console.error('Error signing petition:', error); toast.error("Failed to submit signature. Please try again."); @@ -57,16 +71,42 @@ export const PetitionForm = () => { }; return ( -
-
- setFormData({ ...formData, name: e.target.value })} - required - className="bg-background border-border" - /> -
+ + {!compact && ( +
+ + +
+ )} + + {!isAnonymous && ( +
+ setFormData({ ...formData, name: e.target.value })} + required + className="bg-background border-border" + /> +
+ )} +
{ className="bg-background border-border" />
-
-