import React, { useState, useMemo } from "react"; import { View, ScrollView, TextInput, Alert } from "react-native"; import { AppContainer, MyButton, MyText, tw , BottomDialog } from "common-ui"; import RegistrationForm from "@/components/registration-form"; import { useUserDetails, useAuth } from "@/src/contexts/AuthContext"; import { useUpdateProfile } from "@/src/api-hooks/auth.api"; import { router } from "expo-router"; import { trpc } from '@/src/trpc-client'; function EditProfile() { const userDetails = useUserDetails(); const { updateUserDetails, logout } = useAuth(); const updateProfileMutation = useUpdateProfile(); // State for mobile verification modal const [showDeleteModal, setShowDeleteModal] = useState(false); const [enteredMobile, setEnteredMobile] = useState(''); const deleteAccountMutation = trpc.user.auth.deleteAccount.useMutation(); // Prevent unnecessary re-renders const mobileInputValue = useMemo(() => enteredMobile, [enteredMobile]); const handleUpdate = async (data: FormData) => { try { const response = await updateProfileMutation.mutateAsync(data); // Update the context with new user details if (response.user) { updateUserDetails(response.user); } // Navigate back to profile/me page router.replace('/(drawer)/(tabs)/me'); } catch (error) { JSON.stringify(error); console.error('Update profile error:', error); throw error; } }; const handleDeleteAccount = () => { setShowDeleteModal(true); }; const confirmDeleteAccount = async () => { if (!enteredMobile.trim()) { Alert.alert('Error', 'Please enter your mobile number'); return; } try { await deleteAccountMutation.mutateAsync({ mobile: enteredMobile }); Alert.alert( 'Account Deleted', 'Your account and all data have been successfully deleted.', [ { text: 'OK', onPress: () => { setShowDeleteModal(false); logout(); } } ] ); } catch (error: any) { Alert.alert( 'Verification Failed', error?.message || 'Failed to delete account. Please check your mobile number.' ); } }; // Prepare initial values from user details const initialValues = userDetails ? { name: userDetails.name || '', email: userDetails.email || '', mobile: userDetails.mobile || '', profileImageUri: userDetails.profileImage || undefined, } : undefined; return ( Edit Profile Update your account details {showDeleteModal && ( setShowDeleteModal(false)} enableDismiss={false} > ⚠️ Delete Account Permanently This action cannot be undone. All your data will be permanently deleted. Enter your registered mobile number to confirm: setShowDeleteModal(false)} fillColor="gray1" textColor="gray700" fullWidth /> )} ); } export default EditProfile;