Spaces:
Sleeping
Sleeping
File size: 3,184 Bytes
66f3298 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 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; |