feat: add account deletion functionality with double confirmation, API call, loading state, and automatic sign-out to the PrivacyForm.

This commit is contained in:
pradeepkumar
2026-02-09 01:15:32 +05:30
parent 927c185f92
commit aed666f972

View File

@@ -1,7 +1,7 @@
'use client'; 'use client';
import { useState, useEffect, useCallback } from 'react'; import { useState, useEffect, useCallback } from 'react';
import { useSession } from 'next-auth/react'; import { useSession, signOut } from 'next-auth/react';
import api from '@/services/api'; import api from '@/services/api';
interface PrivacySetting { interface PrivacySetting {
@@ -71,6 +71,7 @@ export function PrivacyForm({ onSave, onDeleteAccount }: PrivacyFormProps) {
const { data: session } = useSession(); const { data: session } = useSession();
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false); const [isSaving, setIsSaving] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState<string | null>(null); const [success, setSuccess] = useState<string | null>(null);
@@ -165,13 +166,36 @@ export function PrivacyForm({ onSave, onDeleteAccount }: PrivacyFormProps) {
} }
}; };
const handleDeleteAccount = () => { const handleDeleteAccount = async () => {
if (confirm('Are you sure you want to delete your account? This action cannot be undone.')) { if (!confirm('Are you sure you want to delete your account? This action cannot be undone.')) {
return;
}
// Double confirmation for safety
if (!confirm('This will permanently delete all your data. Are you absolutely sure?')) {
return;
}
setIsDeleting(true);
setError(null);
try {
const role = (session?.user as any)?.role;
const endpoint = role === 'AGENT' ? '/agents/account' : '/users/account';
await api.delete(endpoint);
// Call the optional callback
if (onDeleteAccount) { if (onDeleteAccount) {
onDeleteAccount(); onDeleteAccount();
} else {
console.log('Deleting account');
} }
// Sign out and redirect to home page
await signOut({ callbackUrl: '/' });
} catch (err: unknown) {
const error = err as { response?: { data?: { message?: string } } };
setError(error.response?.data?.message || 'Failed to delete account. Please try again.');
setIsDeleting(false);
} }
}; };
@@ -344,9 +368,10 @@ export function PrivacyForm({ onSave, onDeleteAccount }: PrivacyFormProps) {
</div> </div>
<button <button
onClick={handleDeleteAccount} onClick={handleDeleteAccount}
className="px-6 py-2 border border-red-500 text-red-500 rounded-[15px] font-serif text-[12px] hover:bg-red-50 transition-colors" disabled={isDeleting}
className="px-6 py-2 border border-red-500 text-red-500 rounded-[15px] font-serif text-[12px] hover:bg-red-50 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
> >
Delete Account {isDeleting ? 'Deleting...' : 'Delete Account'}
</button> </button>
</div> </div>
</div> </div>