import React, { useState, useEffect } from 'react'; import { View, Text, ScrollView, TextInput, TouchableOpacity, Switch, ActivityIndicator, Alert, Image, } from 'react-native'; import { useAppTheme } from '@theme'; import { ScreenHeader } from '@components'; import { ClipboardIcon, WalletIcon } from '@icons'; import { useAppDispatch, useAppSelector } from '@store'; import { useNavigation } from '@react-navigation/native'; import { pick, types, errorCodes } from '@react-native-documents/picker'; import { AddBankDetailsPayload } from '@interfaces'; import { addBankAccount } from './thunk'; import { resetAddBankState } from './reducer'; import { getStyles } from './addBankDetailsScreen.styles'; export const AddBankDetailsScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const navigation = useNavigation(); const { isLoading, error, success } = useAppSelector( state => state.addBankDetails, ); const [accountHolderName, setAccountHolderName] = useState(''); const [bankName, setBankName] = useState(''); const [accountNumber, setAccountNumber] = useState(''); const [ifscCode, setIfscCode] = useState(''); const [branchName, setBranchName] = useState(''); const [isDefault, setIsDefault] = useState(true); const [passbookImage, setPassbookImage] = useState<{ uri: string; name: string; type: string; } | null>(null); const [errors, setErrors] = useState>({}); useEffect(() => { return () => { dispatch(resetAddBankState()); }; }, [dispatch]); useEffect(() => { if (success) { Alert.alert( 'Bank Account Registered', 'Your bank account has been added successfully.', [ { text: 'OK', onPress: () => { dispatch(resetAddBankState()); navigation.goBack(); }, }, ], ); } }, [success, dispatch, navigation]); const handlePickPassbook = async () => { try { const [result] = await pick({ type: [types.images, types.pdf], allowMultiSelection: false, }); if (result) { setPassbookImage({ uri: result.uri, name: result.name ?? `passbook_${Date.now()}.jpg`, type: result.type ?? 'image/jpeg', }); } } catch (err: any) { if (err?.code === errorCodes.OPERATION_CANCELED) return; Alert.alert('File Picker Error', err?.message ?? 'Failed to pick file.'); } }; const handleRemovePassbook = () => { setPassbookImage(null); }; const handleSubmit = () => { const newErrors: Record = {}; if (!accountHolderName.trim()) { newErrors.accountHolderName = 'Account holder name is required'; } if (!bankName.trim()) { newErrors.bankName = 'Bank name is required'; } if (!accountNumber.trim()) { newErrors.accountNumber = 'Account number is required'; } else if (accountNumber.trim().length < 8) { newErrors.accountNumber = 'Enter a valid account number (min 8 digits)'; } if (!ifscCode.trim()) { newErrors.ifscCode = 'IFSC code is required'; } else if (ifscCode.trim().length < 4) { newErrors.ifscCode = 'Enter a valid IFSC code'; } setErrors(newErrors); if (Object.keys(newErrors).length > 0) { return; } const payload: AddBankDetailsPayload = { accountHolderName: accountHolderName.trim(), bankName: bankName.trim(), accountNumber: accountNumber.trim(), ifscCode: ifscCode.trim().toUpperCase(), branchName: branchName.trim() || undefined, isDefault, passbookImage: passbookImage ? (passbookImage as any) : undefined, }; dispatch(addBankAccount(payload)); }; return ( navigation.goBack()} /> {/* Info Header Banner */} Payout Account Details Enter your official bank details to receive weekly delivery earnings and automated payouts. {/* Error Banner */} {error && ( {error} )} {/* Form Fields Card */} {/* Account Holder Name */} Account Holder Name * { setAccountHolderName(text); if (errors.accountHolderName) { setErrors(prev => ({ ...prev, accountHolderName: '' })); } }} autoCapitalize="words" /> {errors.accountHolderName && ( {errors.accountHolderName} )} {/* Bank Name */} Bank Name * { setBankName(text); if (errors.bankName) { setErrors(prev => ({ ...prev, bankName: '' })); } }} autoCapitalize="words" /> {errors.bankName && ( {errors.bankName} )} {/* Account Number */} Account Number * { setAccountNumber(text); if (errors.accountNumber) { setErrors(prev => ({ ...prev, accountNumber: '' })); } }} keyboardType="number-pad" /> {errors.accountNumber && ( {errors.accountNumber} )} {/* IFSC Code */} IFSC Code * { setIfscCode(text.toUpperCase()); if (errors.ifscCode) { setErrors(prev => ({ ...prev, ifscCode: '' })); } }} autoCapitalize="characters" /> {errors.ifscCode && ( {errors.ifscCode} )} {/* Branch Name (Optional) */} Branch Name (Optional) {/* Default Account Toggle */} Default Payout Account Mark as default bank account for payouts {/* Passbook / Cancelled Cheque Image (Optional) */} Passbook / Cancelled Cheque (Optional) {passbookImage ? ( {passbookImage.type.startsWith('image/') && ( )} {passbookImage.name} Attached document Remove ) : ( <> Choose Passbook / Cheque File Supports JPG, PNG, or PDF format )} {/* Submit Button */} {isLoading ? ( ) : ( Save Bank Account )} ); }; export default AddBankDetailsScreen;