feat: Store original profile data to enable reset functionality on cancel and update after save.

This commit is contained in:
pradeepkumar
2026-02-09 02:10:13 +05:30
parent 7e8576c006
commit 7b2c10e7bb

View File

@@ -52,6 +52,8 @@ export function ProfileSettingsForm({
const [isSaving, setIsSaving] = useState(false); const [isSaving, setIsSaving] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [successMessage, setSuccessMessage] = useState<string | null>(null); const [successMessage, setSuccessMessage] = useState<string | null>(null);
// Store original data for cancel/reset functionality
const [originalData, setOriginalData] = useState<typeof formData | null>(null);
// Fetch profile data on mount based on user role // Fetch profile data on mount based on user role
useEffect(() => { useEffect(() => {
@@ -62,14 +64,16 @@ export function ProfileSettingsForm({
if (role === 'AGENT') { if (role === 'AGENT') {
const profile = await agentsService.getMyProfile(); const profile = await agentsService.getMyProfile();
setFormData({ const profileData = {
firstName: profile.firstName || '', firstName: profile.firstName || '',
lastName: profile.lastName || '', lastName: profile.lastName || '',
career: profile.agentType?.name || 'Real Estate Agent', career: profile.agentType?.name || 'Real Estate Agent',
email: profile.email || session?.user?.email || '', email: profile.email || session?.user?.email || '',
phone: profile.phone || '', phone: profile.phone || '',
location: profile.serviceAreas?.[0] || '', location: profile.serviceAreas?.[0] || '',
}); };
setFormData(profileData);
setOriginalData(profileData); // Store original for cancel
if (profile.avatar) { if (profile.avatar) {
try { try {
@@ -81,14 +85,16 @@ export function ProfileSettingsForm({
} }
} else if (role === 'USER') { } else if (role === 'USER') {
const profile = await usersService.getMyProfile(); const profile = await usersService.getMyProfile();
setFormData({ const profileData = {
firstName: profile.firstName || '', firstName: profile.firstName || '',
lastName: profile.lastName || '', lastName: profile.lastName || '',
career: '', // Users don't have career/agent type career: '', // Users don't have career/agent type
email: profile.email || session?.user?.email || '', email: profile.email || session?.user?.email || '',
phone: profile.phone || '', phone: profile.phone || '',
location: [profile.city, profile.state, profile.country].filter(Boolean).join(', '), location: [profile.city, profile.state, profile.country].filter(Boolean).join(', '),
}); };
setFormData(profileData);
setOriginalData(profileData); // Store original for cancel
if (profile.avatar) { if (profile.avatar) {
try { try {
@@ -304,6 +310,8 @@ export function ProfileSettingsForm({
onSave(formData); onSave(formData);
} }
// Update original data so Cancel will reset to the newly saved values
setOriginalData(formData);
setSuccessMessage('Profile settings saved successfully!'); setSuccessMessage('Profile settings saved successfully!');
} catch (err) { } catch (err) {
console.error('Save failed:', err); console.error('Save failed:', err);
@@ -314,8 +322,11 @@ export function ProfileSettingsForm({
}; };
const handleCancel = () => { const handleCancel = () => {
// Reset to initial data or session data // Reset to original fetched data (including phone)
if (session?.user) { if (originalData) {
setFormData(originalData);
} else if (session?.user) {
// Fallback to session data if original not available
const nameParts = (session.user?.name || '').split(' '); const nameParts = (session.user?.name || '').split(' ');
setFormData(prev => ({ setFormData(prev => ({
...prev, ...prev,