import { useState } from "react"; import { db, auth } from "../firebase"; import { doc, setDoc } from "firebase/firestore"; const DemographicForm = ({ onComplete }) => { const [formData, setFormData] = useState({ age: "", gender: "", genderOther: "", race: "", ethnicity: "", birthCountry: "", birthCountryOther: "", country: "", countryOther: "", education: "", employment: "", language: "", englishProficiency: "", otherLanguages: "", timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, }); const [error, setError] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const raceOptions = [ "American Indian or Alaska Native", "Asian", "Black or African American", "Native Hawaiian or Other Pacific Islander", "White", "Two or More Races", "Prefer not to say" ]; const educationOptions = [ "Less than high school", "High school diploma or equivalent", "Some college, no degree", "Associate degree", "Bachelor’s degree", "Master’s degree", "Professional degree (e.g., MD, JD)", "Doctorate", "Prefer not to say" ]; const employmentOptions = [ "Employed full-time", "Employed part-time", "Self-employed", "Unemployed and looking for work", "Unemployed and not looking for work", "Student", "Retired", "Prefer not to say" ]; const countryOptions = [ "United States", "Canada", "United Kingdom", "Australia", "Germany", "France", "Spain", "Italy", "Netherlands", "Belgium", "Switzerland", "Austria", "Sweden", "Norway", "Denmark", "Finland", "Poland", "Czech Republic", "Greece", "Portugal", "Ireland", "New Zealand", "Japan", "South Korea", "China", "India", "Singapore", "Malaysia", "Indonesia", "Thailand", "Philippines", "Vietnam", "Mexico", "Brazil", "Argentina", "Chile", "Colombia", "Peru", "South Africa", "Nigeria", "Egypt", "Israel", "United Arab Emirates", "Saudi Arabia", "Turkey", "Russia", "Ukraine", "Other" ]; const handleChange = (e) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); }; const validate = () => { const age = parseInt(formData.age, 10); if (isNaN(age) || age < 18 || age > 100) { setError("You must be between 18 and 100 years old to participate."); return false; } if (!formData.gender) { setError("Please select or enter a gender."); return false; } return true; }; const handleSubmit = async (e) => { console.log("UID:", auth.currentUser?.uid); e.preventDefault(); // Prevent double submission if (isSubmitting) return; if (!validate()) return; setIsSubmitting(true); try { await setDoc(doc(db, "participants", auth.currentUser.uid), { demographics: { ...formData, gender: formData.gender === "Other" ? formData.genderOther || "Other" : formData.gender, birthCountry: formData.birthCountry === "Other" ? formData.birthCountryOther || "Other" : formData.birthCountry, country: formData.country === "Other" ? formData.countryOther || "Other" : formData.country, age: parseInt(formData.age, 10), }, demographicsCompleted: true, }, { merge: true }); onComplete(); } catch (err) { console.error("Error saving demographics:", err); setError("An error occurred. Please try again."); setIsSubmitting(false); } }; return (

Demographic Information

{error &&

{error}

} {formData.gender === "Other" && ( )} {formData.birthCountry === "Other" && ( )} {formData.country === "Other" && ( )}

Detected Timezone: {formData.timezone}

); }; export default DemographicForm;