Spaces:
Sleeping
Sleeping
| import { useState } from 'react'; | |
| import { UserProfile } from '../../interface'; | |
| import { GEODATA_ENDPOINT } from '../../endpoint'; | |
| interface UseProfileScreenOptions { | |
| onComplete: (userProfile: UserProfile) => void; | |
| } | |
| const useProfileScreen = ({ onComplete }: UseProfileScreenOptions) => { | |
| const [formData, setFormData] = useState({ | |
| zip_code: '', | |
| age: '', | |
| gender: '', | |
| household_size: '', | |
| income: '', | |
| employment_status: '', | |
| citizenship: '' | |
| }); | |
| const [userProfile, setUserProfile] = useState<UserProfile>({}); | |
| const [isValidatingZip, setIsValidatingZip] = useState(false); | |
| const [zipError, setZipError] = useState(''); | |
| const [isLoading, setIsLoading] = useState(false); | |
| // Fetch geodata for ZIP code | |
| const getGeodata = async (zipCode: string) => { | |
| setIsValidatingZip(true); | |
| setZipError(''); | |
| try { | |
| const response = await fetch(`${GEODATA_ENDPOINT}/${zipCode}`); | |
| if (!response.ok) throw new Error('Invalid ZIP code'); | |
| const data = await response.json(); | |
| setUserProfile(prev => ({ ...prev, ...data })); | |
| setFormData(prev => ({ ...prev, zip_code: zipCode })); | |
| return data; | |
| } catch (error) { | |
| setZipError('Invalid ZIP code or could not retrieve location data'); | |
| return null; | |
| } finally { | |
| setIsValidatingZip(false); | |
| } | |
| }; | |
| // Handle ZIP code input | |
| const handleZipChange = async (e: React.ChangeEvent<HTMLInputElement>) => { | |
| const value = e.target.value; | |
| const numericValue = value.replace(/\D/g, '').slice(0, 5); | |
| setFormData(prev => ({ ...prev, zip_code: numericValue })); | |
| if (numericValue.length === 5) { | |
| await getGeodata(numericValue); | |
| } | |
| }; | |
| // Handle other form input changes | |
| const handleFormChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => { | |
| const { name, value } = e.target; | |
| setFormData(prev => ({ ...prev, [name]: value })); | |
| }; | |
| // Handle form submission | |
| const handleSubmit = async () => { | |
| // Validate all fields | |
| const requiredFields = ['age', 'gender', 'household_size', 'income', 'employment_status', 'citizenship']; | |
| const missingFields = requiredFields.filter(field => !formData[field as keyof typeof formData]); | |
| if (missingFields.length > 0) { | |
| alert('Please fill out all fields to continue.'); | |
| return; | |
| } | |
| console.log('Submitting profile data:', formData); | |
| setIsLoading(true); | |
| // Build the final user profile | |
| const updatedProfile: UserProfile = { | |
| ...userProfile, | |
| age: +formData.age, | |
| gender: formData.gender, | |
| household_size: +formData.household_size, | |
| income: +formData.income, | |
| employment_status: formData.employment_status, | |
| citizenship: formData.citizenship, | |
| medical_history: null, | |
| medications: null, | |
| special_cases: null | |
| }; | |
| setUserProfile(updatedProfile); | |
| setIsLoading(false); | |
| // Notify parent (App) to proceed to chat phase | |
| onComplete(updatedProfile); | |
| }; | |
| return { | |
| formData, | |
| userProfile, | |
| isValidatingZip, | |
| zipError, | |
| isLoading, | |
| handleZipChange, | |
| handleFormChange, | |
| handleSubmit, | |
| }; | |
| }; | |
| export default useProfileScreen; |