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 { router } from "expo-router"; import { trpc } from '@/src/trpc-client'; function EditProfile() { const userDetails = useUserDetails(); const { logout, refetchUser } = useAuth(); const updateProfileMutation = trpc.user.auth.updateProfile.useMutation(); // 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: { name: string; email: string; mobile: string; password: string; imageKey?: string }) => { try { await updateProfileMutation.mutateAsync({ name: data.name, email: data.email, imageKey: data.imageKey, }); // Refetch user data to get updated values await refetchUser(); // Navigate back to profile/me page router.replace('/(drawer)/(tabs)/me'); } catch (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, // Password fields not needed for edit mode password: '', confirmPassword: '', termsAccepted: true, } : 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;