import React, { useState, useEffect } from 'react'; import { View, StyleSheet, ScrollView, Alert, KeyboardAvoidingView, Platform } from 'react-native'; import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { RouteProp } from '@react-navigation/native'; import { RootStackParamList } from '../navigation/AppNavigator'; import { Header } from '../components/ui/Header'; import { Input } from '../components/ui/Input'; import { Button } from '../components/ui/Button'; import { Typography } from '../components/ui/Typography'; import { profileService, PaymentProfile } from '../services/profileService'; import { THEME } from '../constants/theme'; import { CreditCard } from 'lucide-react-native'; import { useSnackbar } from '../context/SnackbarContext'; interface AddEditPaymentProfileScreenProps { navigation: NativeStackNavigationProp; route: RouteProp; } export const AddEditPaymentProfileScreen: React.FC = ({ navigation, route }) => { const { profile } = route.params || {}; const isEditing = !!profile; const [isSaving, setIsSaving] = useState(false); const insets = useSafeAreaInsets(); const { showSnackbar } = useSnackbar(); const [form, setForm] = useState>({ payment_method: profile?.payment_method || 'bank', account_holder_name: profile?.account_holder_name || '', account_number: profile?.account_number || '', ifsc_code: profile?.ifsc_code || '', bank_name: profile?.bank_name || '', branch_name: profile?.branch_name || '', account_type: profile?.account_type || 'savings', }); const [errors, setErrors] = useState>({}); const handleChange = (field: keyof PaymentProfile, value: string) => { setForm(prev => ({ ...prev, [field]: value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; const validate = () => { const newErrors: Record = {}; if (!form.account_holder_name) newErrors.account_holder_name = 'Account holder name is required'; if (!form.account_number) newErrors.account_number = 'Account number is required'; if (!form.ifsc_code) newErrors.ifsc_code = 'IFSC code is required'; if (!form.bank_name) newErrors.bank_name = 'Bank name is required'; setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSave = async () => { if (!validate()) { Alert.alert('Validation Error', 'Please fill in all required fields.'); return; } try { setIsSaving(true); const payload = form as PaymentProfile; if (isEditing && profile?.id) { payload.id = profile.id; await profileService.updatePaymentProfile(payload); showSnackbar('Payment profile updated successfully.'); } else { await profileService.addPaymentProfile(payload); showSnackbar('Payment profile added successfully.'); } navigation.goBack(); } catch (error: any) { console.error('Error saving payment profile:', error); Alert.alert('Error', error?.response?.data?.message || 'Failed to save payment profile. Please try again.'); } finally { setIsSaving(false); } }; return ( // @ts-ignore - TS types for SafeAreaView in this version incorrectly omit the style prop
navigation.goBack()} /> Add your bank details to receive payments. handleChange('account_holder_name', val)} error={errors.account_holder_name} /> handleChange('account_number', val)} error={errors.account_number} /> handleChange('ifsc_code', val)} error={errors.ifsc_code} /> handleChange('bank_name', val)} error={errors.bank_name} /> handleChange('branch_name', val)} /> handleChange('account_type', val)} />