aabroukh's picture
Add files using upload-large-folder tool
b136cbe verified
Raw History Blame Contribute Delete
10.5 kB
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 (
<div style={{ paddingLeft: "2rem" }}>
<form
onSubmit={handleSubmit}
style={{
display: "flex",
flexDirection: "column",
gap: "1rem",
maxWidth: "400px"
}}
>
<h2>Demographic Information</h2>
{error && <p style={{ color: "red" }}>{error}</p>}
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Age:
<input
type="number"
name="age"
value={formData.age}
onChange={handleChange}
required
/>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Gender:
<select name="gender" value={formData.gender} onChange={handleChange} required>
<option value="">Select</option>
<option value="Man">Man</option>
<option value="Woman">Woman</option>
<option value="Non-binary">Non-binary</option>
<option value="Other">Other</option>
</select>
</label>
{formData.gender === "Other" && (
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Please describe:
<input
type="text"
name="genderOther"
value={formData.genderOther}
onChange={handleChange}
required
/>
</label>
)}
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Race/Ethnicity:
<select name="race" value={formData.race} onChange={handleChange} required>
<option value="">Select</option>
{raceOptions.map((race) => (
<option key={race} value={race}>{race}</option>
))}
</select>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Ethnicity:
<select name="ethnicity" value={formData.ethnicity} onChange={handleChange} required>
<option value="">Select</option>
<option value="Hispanic or Latino">Hispanic or Latino</option>
<option value="Not Hispanic or Latino">Not Hispanic or Latino</option>
<option value="Prefer not to say">Prefer not to say</option>
</select>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Country of Birth:
<select name="birthCountry" value={formData.birthCountry} onChange={handleChange} required>
<option value="">Select</option>
{countryOptions.map((country) => (
<option key={country} value={country}>{country}</option>
))}
</select>
</label>
{formData.birthCountry === "Other" && (
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Please specify your country of birth:
<input
type="text"
name="birthCountryOther"
value={formData.birthCountryOther}
onChange={handleChange}
required
/>
</label>
)}
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Country of Residence:
<select name="country" value={formData.country} onChange={handleChange} required>
<option value="">Select</option>
{countryOptions.map((country) => (
<option key={country} value={country}>{country}</option>
))}
</select>
</label>
{formData.country === "Other" && (
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Please specify your country:
<input
type="text"
name="countryOther"
value={formData.countryOther}
onChange={handleChange}
required
/>
</label>
)}
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Educational Attainment:
<select name="education" value={formData.education} onChange={handleChange} required>
<option value="">Select</option>
{educationOptions.map((ed) => (
<option key={ed} value={ed}>{ed}</option>
))}
</select>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Employment Status:
<select name="employment" value={formData.employment} onChange={handleChange} required>
<option value="">Select</option>
{employmentOptions.map((emp) => (
<option key={emp} value={emp}>{emp}</option>
))}
</select>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Primary Language:
<input
type="text"
name="language"
value={formData.language}
onChange={handleChange}
required
/>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
English Proficiency:
<select
name="englishProficiency"
value={formData.englishProficiency}
onChange={handleChange}
required
>
<option value="">Select</option>
<option value="Native speaker">Native speaker</option>
<option value="Fluent">Fluent</option>
<option value="Conversational">Conversational</option>
<option value="Basic">Basic</option>
</select>
</label>
<label style={{ display: "flex", flexDirection: "column", gap: "0.25rem" }}>
Other Languages Spoken:
<input
type="text"
name="otherLanguages"
value={formData.otherLanguages}
onChange={handleChange}
/>
</label>
<p><strong>Detected Timezone:</strong> {formData.timezone}</p>
<button
type="submit"
disabled={isSubmitting}
style={{
width: "fit-content",
alignSelf: "flex-start",
padding: "0.5rem 1rem",
backgroundColor: isSubmitting ? '#ccc' : undefined,
cursor: isSubmitting ? 'not-allowed' : 'pointer'
}}
>
{isSubmitting ? 'Saving...' : 'Continue'}
</button>
</form>
</div>
);
};
export default DemographicForm;