From 7b2c10e7bb979643eb8277119f1a7eb9ff8ced91 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Mon, 9 Feb 2026 02:10:13 +0530 Subject: [PATCH] feat: Store original profile data to enable reset functionality on cancel and update after save. --- .../settings/ProfileSettingsForm.tsx | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/components/settings/ProfileSettingsForm.tsx b/src/components/settings/ProfileSettingsForm.tsx index 66d84a1..a2449d8 100644 --- a/src/components/settings/ProfileSettingsForm.tsx +++ b/src/components/settings/ProfileSettingsForm.tsx @@ -52,6 +52,8 @@ export function ProfileSettingsForm({ const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const [successMessage, setSuccessMessage] = useState(null); + // Store original data for cancel/reset functionality + const [originalData, setOriginalData] = useState(null); // Fetch profile data on mount based on user role useEffect(() => { @@ -62,14 +64,16 @@ export function ProfileSettingsForm({ if (role === 'AGENT') { const profile = await agentsService.getMyProfile(); - setFormData({ + const profileData = { firstName: profile.firstName || '', lastName: profile.lastName || '', career: profile.agentType?.name || 'Real Estate Agent', email: profile.email || session?.user?.email || '', phone: profile.phone || '', location: profile.serviceAreas?.[0] || '', - }); + }; + setFormData(profileData); + setOriginalData(profileData); // Store original for cancel if (profile.avatar) { try { @@ -81,14 +85,16 @@ export function ProfileSettingsForm({ } } else if (role === 'USER') { const profile = await usersService.getMyProfile(); - setFormData({ + const profileData = { firstName: profile.firstName || '', lastName: profile.lastName || '', career: '', // Users don't have career/agent type email: profile.email || session?.user?.email || '', phone: profile.phone || '', location: [profile.city, profile.state, profile.country].filter(Boolean).join(', '), - }); + }; + setFormData(profileData); + setOriginalData(profileData); // Store original for cancel if (profile.avatar) { try { @@ -304,6 +310,8 @@ export function ProfileSettingsForm({ onSave(formData); } + // Update original data so Cancel will reset to the newly saved values + setOriginalData(formData); setSuccessMessage('Profile settings saved successfully!'); } catch (err) { console.error('Save failed:', err); @@ -314,8 +322,11 @@ export function ProfileSettingsForm({ }; const handleCancel = () => { - // Reset to initial data or session data - if (session?.user) { + // Reset to original fetched data (including phone) + if (originalData) { + setFormData(originalData); + } else if (session?.user) { + // Fallback to session data if original not available const nameParts = (session.user?.name || '').split(' '); setFormData(prev => ({ ...prev,