Spaces:
Sleeping
Sleeping
Upload 5 files
#3
by
AnjanaNAshokan - opened
src/screens/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
src/screens/ChatInterfaceScreen/ChatInterfaceScreen.tsx
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useEffect } from 'react';
|
| 2 |
+
import { Send, User, Bot } from 'lucide-react';
|
| 3 |
+
import InsuCompassLogo from '../../assets/InsuCompass_Logo.png';
|
| 4 |
+
import ReactMarkdown from 'react-markdown';
|
| 5 |
+
import { useChatInterface } from './useChatInterface';
|
| 6 |
+
import { Plan, UserProfile } from '../../interface';
|
| 7 |
+
|
| 8 |
+
const PlanCard: React.FC<{ plan: Plan }> = ({ plan }) => (
|
| 9 |
+
<div className="bg-white border border-gray-200 rounded-2xl shadow-sm p-6 hover:shadow-lg transition">
|
| 10 |
+
<h3 className="text-xl font-semibold text-gray-900">{plan.plan_name}</h3>
|
| 11 |
+
<p className="text-sm text-gray-600 mb-2">{plan.plan_type}</p>
|
| 12 |
+
<p className="text-sm italic text-gray-700 mb-4">{plan.reasoning}</p>
|
| 13 |
+
<p className="text-sm font-medium text-gray-800 mb-2">
|
| 14 |
+
Estimated Premium: {plan.estimated_premium}
|
| 15 |
+
</p>
|
| 16 |
+
<ul className="list-disc list-inside space-y-1 text-sm text-gray-700">
|
| 17 |
+
{plan.key_features.map((feat, i) => (
|
| 18 |
+
<li key={i}>{feat}</li>
|
| 19 |
+
))}
|
| 20 |
+
</ul>
|
| 21 |
+
</div>
|
| 22 |
+
);
|
| 23 |
+
|
| 24 |
+
interface ChatInterfaceScreenProps {
|
| 25 |
+
userProfile: UserProfile;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
const ChatInterfaceScreen: React.FC<ChatInterfaceScreenProps> = ({ userProfile }) => {
|
| 29 |
+
const {
|
| 30 |
+
chatHistory,
|
| 31 |
+
isLoading,
|
| 32 |
+
currentMessage,
|
| 33 |
+
setCurrentMessage,
|
| 34 |
+
handleSendMessage,
|
| 35 |
+
showPlanRecs,
|
| 36 |
+
chatEndRef,
|
| 37 |
+
sendChatMessage
|
| 38 |
+
} = useChatInterface(userProfile);
|
| 39 |
+
|
| 40 |
+
useEffect(() => {
|
| 41 |
+
if (chatHistory.length === 0) {
|
| 42 |
+
sendChatMessage('START_PROFILE_BUILDING', userProfile);
|
| 43 |
+
}
|
| 44 |
+
}, [userProfile])
|
| 45 |
+
return (
|
| 46 |
+
<div className="min-h-screen bg-gradient-to-br from-blue-50 via-white to-indigo-50 flex flex-col">
|
| 47 |
+
{/* Header */}
|
| 48 |
+
<div className="fixed top-0 left-0 w-full bg-white shadow-sm border-b z-50">
|
| 49 |
+
<div className="max-w-10xl mx-auto px-6 py-4">
|
| 50 |
+
<div className="flex items-center space-x-3">
|
| 51 |
+
<img src={InsuCompassLogo} alt="InsuCompass Logo" className="h-12 w-auto" />
|
| 52 |
+
<div>
|
| 53 |
+
<h1 className="text-2xl font-bold text-gray-900">InsuCompass</h1>
|
| 54 |
+
<p className="text-sm text-gray-600">Chat with your AI insurance advisor</p>
|
| 55 |
+
</div>
|
| 56 |
+
</div>
|
| 57 |
+
</div>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
{/* Chat Messages */}
|
| 61 |
+
<div className="flex-1 overflow-y-auto pt-20">
|
| 62 |
+
<div className="max-w-4xl mx-auto px-6 py-8">
|
| 63 |
+
<div className="space-y-6">
|
| 64 |
+
{chatHistory.map((message, index) => (
|
| 65 |
+
<div
|
| 66 |
+
key={index}
|
| 67 |
+
className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
|
| 68 |
+
>
|
| 69 |
+
<div className={`flex items-start space-x-3 max-w-3xl ${message.role === 'user' ? 'flex-row-reverse space-x-reverse' : ''}`}>
|
| 70 |
+
{/* Avatar */}
|
| 71 |
+
<div className={`w-10 h-10 rounded-full flex items-center justify-center flex-shrink-0 ${
|
| 72 |
+
message.role === 'user'
|
| 73 |
+
? 'bg-blue-500 text-white'
|
| 74 |
+
: 'bg-gradient-to-r from-purple-500 to-pink-500 text-white'
|
| 75 |
+
}`}>
|
| 76 |
+
{message.role === 'user' ? (
|
| 77 |
+
<User className="w-5 h-5" />
|
| 78 |
+
) : (
|
| 79 |
+
<Bot className="w-5 h-5" />
|
| 80 |
+
)}
|
| 81 |
+
</div>
|
| 82 |
+
{/* Message Bubble */}
|
| 83 |
+
<div className={`px-6 py-4 rounded-2xl shadow-sm ${
|
| 84 |
+
message.role === 'user'
|
| 85 |
+
? 'bg-blue-500 text-white rounded-tr-sm'
|
| 86 |
+
: 'bg-white text-gray-900 rounded-tl-sm border border-gray-200'
|
| 87 |
+
}`}>
|
| 88 |
+
<div className="text-sm font-medium mb-1 opacity-75">
|
| 89 |
+
{message.role === 'user' ? 'You' : 'InsuCompass AI'}
|
| 90 |
+
</div>
|
| 91 |
+
<ReactMarkdown className="prose prose-sm max-w-none prose-table:border prose-table:border-gray-300 prose-th:bg-gray-100 prose-th:p-2 prose-td:p-2 prose-td:border prose-td:border-gray-300">
|
| 92 |
+
{message.content}
|
| 93 |
+
</ReactMarkdown>
|
| 94 |
+
{showPlanRecs && message.plans?.length && (
|
| 95 |
+
<>
|
| 96 |
+
<h2 className="text-2xl font-bold text-gray-900">Recommended Plans for You</h2>
|
| 97 |
+
<div className="grid md:grid-cols-2 gap-6">
|
| 98 |
+
{message.plans?.map((plan, idx) => (
|
| 99 |
+
<PlanCard key={idx} plan={plan} />
|
| 100 |
+
))}
|
| 101 |
+
</div>
|
| 102 |
+
</>
|
| 103 |
+
)}
|
| 104 |
+
</div>
|
| 105 |
+
</div>
|
| 106 |
+
</div>
|
| 107 |
+
))}
|
| 108 |
+
{/* Loading indicator */}
|
| 109 |
+
{isLoading && (
|
| 110 |
+
<div className="flex justify-start">
|
| 111 |
+
<div className="flex items-start space-x-3">
|
| 112 |
+
<div className="w-10 h-10 rounded-full bg-gradient-to-r from-purple-500 to-pink-500 text-white flex items-center justify-center">
|
| 113 |
+
<Bot className="w-5 h-5" />
|
| 114 |
+
</div>
|
| 115 |
+
<div className="bg-white px-6 py-4 rounded-2xl rounded-tl-sm border border-gray-200">
|
| 116 |
+
<div className="flex space-x-2">
|
| 117 |
+
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce"></div>
|
| 118 |
+
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.1s' }}></div>
|
| 119 |
+
<div className="w-2 h-2 bg-gray-400 rounded-full animate-bounce" style={{ animationDelay: '0.2s' }}></div>
|
| 120 |
+
</div>
|
| 121 |
+
</div>
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
)}
|
| 125 |
+
<div ref={chatEndRef} />
|
| 126 |
+
</div>
|
| 127 |
+
</div>
|
| 128 |
+
</div>
|
| 129 |
+
|
| 130 |
+
{/* Message Input */}
|
| 131 |
+
<div className="bg-white border-t border-gray-200 p-4">
|
| 132 |
+
<div className="max-w-4xl mx-auto">
|
| 133 |
+
<div className="flex items-center space-x-4">
|
| 134 |
+
<div className="flex-1 relative">
|
| 135 |
+
<input
|
| 136 |
+
type="text"
|
| 137 |
+
value={currentMessage}
|
| 138 |
+
onChange={(e) => setCurrentMessage(e.target.value)}
|
| 139 |
+
onKeyDown={(e) => e.key === 'Enter' && handleSendMessage()}
|
| 140 |
+
placeholder="Type your message..."
|
| 141 |
+
className="w-full px-4 py-3 pr-12 border border-gray-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors"
|
| 142 |
+
disabled={isLoading}
|
| 143 |
+
/>
|
| 144 |
+
<button
|
| 145 |
+
onClick={handleSendMessage}
|
| 146 |
+
disabled={isLoading || !currentMessage.trim()}
|
| 147 |
+
className="absolute right-2 top-1/2 transform -translate-y-1/2 p-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
| 148 |
+
>
|
| 149 |
+
<Send className="w-4 h-4" />
|
| 150 |
+
</button>
|
| 151 |
+
</div>
|
| 152 |
+
</div>
|
| 153 |
+
</div>
|
| 154 |
+
</div>
|
| 155 |
+
</div>
|
| 156 |
+
);
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
export default ChatInterfaceScreen;
|
src/screens/ChatInterfaceScreen/useChatInterface.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState, useEffect, useRef } from 'react';
|
| 2 |
+
import { ApiResponse, ChatMessage, UserProfile } from '../../interface';
|
| 3 |
+
import { CHAT_ENDPOINT } from '../../endpoint';
|
| 4 |
+
|
| 5 |
+
export function useChatInterface(userProfile: UserProfile, isProfileComplete?: boolean) {
|
| 6 |
+
const [chatHistory, setChatHistory] = useState<ChatMessage[]>([]);
|
| 7 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 8 |
+
const [currentMessage, setCurrentMessage] = useState('');
|
| 9 |
+
const [showPlanRecs, setShowPlanRecs] = useState<boolean>(false);
|
| 10 |
+
const chatEndRef = useRef<HTMLDivElement>(null);
|
| 11 |
+
// threadId is generated once per chat session and persists for the hook's lifetime
|
| 12 |
+
const [threadId] = useState(() => crypto.randomUUID());
|
| 13 |
+
|
| 14 |
+
useEffect(() => {
|
| 15 |
+
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
| 16 |
+
}, [chatHistory]);
|
| 17 |
+
|
| 18 |
+
const sendChatMessage = async (
|
| 19 |
+
message: string,
|
| 20 |
+
profileOverride?: UserProfile
|
| 21 |
+
) => {
|
| 22 |
+
setIsLoading(true);
|
| 23 |
+
try {
|
| 24 |
+
const payload = {
|
| 25 |
+
thread_id: threadId,
|
| 26 |
+
user_profile: profileOverride ?? userProfile,
|
| 27 |
+
message,
|
| 28 |
+
is_profile_complete: isProfileComplete ?? false,
|
| 29 |
+
conversation_history: chatHistory.map(m => `${m.role}: ${m.content}`)
|
| 30 |
+
};
|
| 31 |
+
const response = await fetch(CHAT_ENDPOINT, {
|
| 32 |
+
method: 'POST',
|
| 33 |
+
headers: { 'Content-Type': 'application/json' },
|
| 34 |
+
body: JSON.stringify(payload)
|
| 35 |
+
});
|
| 36 |
+
if (!response.ok) throw new Error('Failed to send message');
|
| 37 |
+
const data: ApiResponse = await response.json();
|
| 38 |
+
// Update state from backend response
|
| 39 |
+
// let newHistory: ChatMessage[] = data.updated_history.map(msg => {
|
| 40 |
+
// const [role, ...contentParts] = msg.split(':');
|
| 41 |
+
// return {
|
| 42 |
+
// role: role.toLowerCase() as 'user' | 'agent',
|
| 43 |
+
// content: contentParts.join(':').trim(),
|
| 44 |
+
// timestamp: Date.now()
|
| 45 |
+
// };
|
| 46 |
+
// });
|
| 47 |
+
let newHistory: ChatMessage[] = [...chatHistory, {
|
| 48 |
+
role: 'agent',
|
| 49 |
+
content: data.updated_history[data.updated_history.length - 1],
|
| 50 |
+
timestamp: Date.now(),
|
| 51 |
+
plans: !showPlanRecs && data.plan_recommendations?.recommendations?.length && data.plan_recommendations?.recommendations?.length > 0 ? data.plan_recommendations.recommendations : undefined
|
| 52 |
+
}];
|
| 53 |
+
setChatHistory(newHistory);
|
| 54 |
+
if (!showPlanRecs && data.plan_recommendations?.recommendations?.length && data.plan_recommendations?.recommendations?.length > 0) {
|
| 55 |
+
setShowPlanRecs(true);
|
| 56 |
+
}
|
| 57 |
+
return data;
|
| 58 |
+
} catch (error) {
|
| 59 |
+
console.error('Error sending message:', error);
|
| 60 |
+
} finally {
|
| 61 |
+
setIsLoading(false);
|
| 62 |
+
}
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const handleSendMessage = async () => {
|
| 66 |
+
if (!currentMessage.trim()) return;
|
| 67 |
+
// Add user message to chat immediately
|
| 68 |
+
const userMessage: ChatMessage = {
|
| 69 |
+
role: 'user',
|
| 70 |
+
content: currentMessage,
|
| 71 |
+
timestamp: Date.now()
|
| 72 |
+
};
|
| 73 |
+
setChatHistory(prev => [...prev, userMessage]);
|
| 74 |
+
const messageToSend = currentMessage;
|
| 75 |
+
setCurrentMessage('');
|
| 76 |
+
await sendChatMessage(messageToSend);
|
| 77 |
+
};
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
chatHistory,
|
| 81 |
+
isLoading,
|
| 82 |
+
currentMessage,
|
| 83 |
+
setCurrentMessage,
|
| 84 |
+
handleSendMessage,
|
| 85 |
+
showPlanRecs,
|
| 86 |
+
chatEndRef,
|
| 87 |
+
sendChatMessage
|
| 88 |
+
};
|
| 89 |
+
}
|
src/screens/ProfileScreen/ProfilingScreen.tsx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import InsuCompassLogo from '../../assets/InsuCompass_Logo.png';
|
| 3 |
+
import LocationSection from '../../components/LocationSection';
|
| 4 |
+
import PersonalInfoSection from '../../components/PersonalInfoSection';
|
| 5 |
+
import HouseholdInfoSection from '../../components/HouseholdInfoSection';
|
| 6 |
+
import EmploymentSection from '../../components/EmploymentSection';
|
| 7 |
+
import { UserProfile } from '../../interface';
|
| 8 |
+
import useProfileScreen from './useProfileScreen';
|
| 9 |
+
|
| 10 |
+
interface ProfilingScreenProps {
|
| 11 |
+
onComplete: (userProfile: UserProfile) => void;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
const ProfilingScreen: React.FC<ProfilingScreenProps> = ({ onComplete }) => {
|
| 15 |
+
const {
|
| 16 |
+
formData,
|
| 17 |
+
userProfile,
|
| 18 |
+
isValidatingZip,
|
| 19 |
+
zipError,
|
| 20 |
+
isLoading,
|
| 21 |
+
handleZipChange,
|
| 22 |
+
handleFormChange,
|
| 23 |
+
handleSubmit,
|
| 24 |
+
} = useProfileScreen({
|
| 25 |
+
onComplete,
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
return (
|
| 29 |
+
<div className="min-h-screen bg-gradient-to-br from-blue-50 via-white to-indigo-50">
|
| 30 |
+
{/* Header */}
|
| 31 |
+
<div className="fixed top-0 left-0 w-full bg-white shadow-sm border-b z-50">
|
| 32 |
+
<div className="max-w-10xl mx-auto px-6 py-4">
|
| 33 |
+
<div className="flex items-center space-x-3">
|
| 34 |
+
<img src={InsuCompassLogo} alt="InsuCompass Logo" className="h-12 w-auto" />
|
| 35 |
+
<div>
|
| 36 |
+
<h1 className="text-2xl font-bold text-gray-900">InsuCompass</h1>
|
| 37 |
+
<p className="text-sm text-gray-600">Your AI guide to U.S. Health Insurance</p>
|
| 38 |
+
</div>
|
| 39 |
+
</div>
|
| 40 |
+
</div>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
{/* Main Content */}
|
| 44 |
+
<div className="flex-1 overflow-y-auto pt-20">
|
| 45 |
+
<div className="max-w-2xl mx-auto px-6 py-12">
|
| 46 |
+
<div className="text-center mb-8">
|
| 47 |
+
<h2 className="text-3xl font-bold text-gray-900 mb-4">Let's Get Started</h2>
|
| 48 |
+
<p className="text-lg text-gray-600">Tell us about yourself to receive personalized insurance guidance</p>
|
| 49 |
+
</div>
|
| 50 |
+
<div className="space-y-6">
|
| 51 |
+
{/* ZIP Code Section */}
|
| 52 |
+
<LocationSection
|
| 53 |
+
formData={formData}
|
| 54 |
+
onChange={handleZipChange}
|
| 55 |
+
isValidatingZip={isValidatingZip}
|
| 56 |
+
zipError={zipError}
|
| 57 |
+
userProfile={userProfile}
|
| 58 |
+
/>
|
| 59 |
+
{/* Personal Information */}
|
| 60 |
+
{userProfile.city && (
|
| 61 |
+
<PersonalInfoSection formData={formData} onChange={handleFormChange} />
|
| 62 |
+
)}
|
| 63 |
+
{/* Household Information */}
|
| 64 |
+
{userProfile.city && (
|
| 65 |
+
<HouseholdInfoSection formData={formData} onChange={handleFormChange} />
|
| 66 |
+
)}
|
| 67 |
+
{/* Employment & Citizenship */}
|
| 68 |
+
{userProfile.city && (
|
| 69 |
+
<EmploymentSection formData={formData} onChange={handleFormChange} />
|
| 70 |
+
)}
|
| 71 |
+
{/* Submit Button */}
|
| 72 |
+
{userProfile.city && (
|
| 73 |
+
<button
|
| 74 |
+
onClick={handleSubmit}
|
| 75 |
+
disabled={isLoading}
|
| 76 |
+
className="w-full bg-gradient-to-r from-blue-500 to-indigo-600 text-white py-4 px-8 rounded-xl font-semibold text-lg shadow-lg hover:shadow-xl transform hover:scale-[1.02] transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
|
| 77 |
+
>
|
| 78 |
+
{isLoading ? 'Starting Session...' : 'Start My Personalized Session'}
|
| 79 |
+
</button>
|
| 80 |
+
)}
|
| 81 |
+
</div>
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
</div>
|
| 85 |
+
);
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
export default ProfilingScreen;
|
src/screens/ProfileScreen/useProfileScreen.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from 'react';
|
| 2 |
+
import { UserProfile } from '../../interface';
|
| 3 |
+
import { GEODATA_ENDPOINT } from '../../endpoint';
|
| 4 |
+
|
| 5 |
+
interface UseProfileScreenOptions {
|
| 6 |
+
onComplete: (userProfile: UserProfile) => void;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
const useProfileScreen = ({ onComplete }: UseProfileScreenOptions) => {
|
| 10 |
+
const [formData, setFormData] = useState({
|
| 11 |
+
zip_code: '',
|
| 12 |
+
age: '',
|
| 13 |
+
gender: '',
|
| 14 |
+
household_size: '',
|
| 15 |
+
income: '',
|
| 16 |
+
employment_status: '',
|
| 17 |
+
citizenship: ''
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
const [userProfile, setUserProfile] = useState<UserProfile>({});
|
| 21 |
+
const [isValidatingZip, setIsValidatingZip] = useState(false);
|
| 22 |
+
const [zipError, setZipError] = useState('');
|
| 23 |
+
const [isLoading, setIsLoading] = useState(false);
|
| 24 |
+
|
| 25 |
+
// Fetch geodata for ZIP code
|
| 26 |
+
const getGeodata = async (zipCode: string) => {
|
| 27 |
+
setIsValidatingZip(true);
|
| 28 |
+
setZipError('');
|
| 29 |
+
try {
|
| 30 |
+
const response = await fetch(`${GEODATA_ENDPOINT}/${zipCode}`);
|
| 31 |
+
if (!response.ok) throw new Error('Invalid ZIP code');
|
| 32 |
+
const data = await response.json();
|
| 33 |
+
setUserProfile(prev => ({ ...prev, ...data }));
|
| 34 |
+
setFormData(prev => ({ ...prev, zip_code: zipCode }));
|
| 35 |
+
return data;
|
| 36 |
+
} catch (error) {
|
| 37 |
+
setZipError('Invalid ZIP code or could not retrieve location data');
|
| 38 |
+
return null;
|
| 39 |
+
} finally {
|
| 40 |
+
setIsValidatingZip(false);
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
// Handle ZIP code input
|
| 45 |
+
const handleZipChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 46 |
+
const value = e.target.value;
|
| 47 |
+
const numericValue = value.replace(/\D/g, '').slice(0, 5);
|
| 48 |
+
setFormData(prev => ({ ...prev, zip_code: numericValue }));
|
| 49 |
+
if (numericValue.length === 5) {
|
| 50 |
+
await getGeodata(numericValue);
|
| 51 |
+
}
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
// Handle other form input changes
|
| 55 |
+
const handleFormChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
|
| 56 |
+
const { name, value } = e.target;
|
| 57 |
+
setFormData(prev => ({ ...prev, [name]: value }));
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
// Handle form submission
|
| 61 |
+
const handleSubmit = async () => {
|
| 62 |
+
// Validate all fields
|
| 63 |
+
const requiredFields = ['age', 'gender', 'household_size', 'income', 'employment_status', 'citizenship'];
|
| 64 |
+
const missingFields = requiredFields.filter(field => !formData[field as keyof typeof formData]);
|
| 65 |
+
if (missingFields.length > 0) {
|
| 66 |
+
alert('Please fill out all fields to continue.');
|
| 67 |
+
return;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
console.log('Submitting profile data:', formData);
|
| 71 |
+
setIsLoading(true);
|
| 72 |
+
|
| 73 |
+
// Build the final user profile
|
| 74 |
+
const updatedProfile: UserProfile = {
|
| 75 |
+
...userProfile,
|
| 76 |
+
age: +formData.age,
|
| 77 |
+
gender: formData.gender,
|
| 78 |
+
household_size: +formData.household_size,
|
| 79 |
+
income: +formData.income,
|
| 80 |
+
employment_status: formData.employment_status,
|
| 81 |
+
citizenship: formData.citizenship,
|
| 82 |
+
medical_history: null,
|
| 83 |
+
medications: null,
|
| 84 |
+
special_cases: null
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
setUserProfile(updatedProfile);
|
| 88 |
+
setIsLoading(false);
|
| 89 |
+
|
| 90 |
+
// Notify parent (App) to proceed to chat phase
|
| 91 |
+
onComplete(updatedProfile);
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
return {
|
| 95 |
+
formData,
|
| 96 |
+
userProfile,
|
| 97 |
+
isValidatingZip,
|
| 98 |
+
zipError,
|
| 99 |
+
isLoading,
|
| 100 |
+
handleZipChange,
|
| 101 |
+
handleFormChange,
|
| 102 |
+
handleSubmit,
|
| 103 |
+
};
|
| 104 |
+
};
|
| 105 |
+
|
| 106 |
+
export default useProfileScreen;
|