import React, { useCallback, useState } from 'react';
import {
View,
Text,
SafeAreaView,
FlatList,
TouchableOpacity,
Modal,
TextInput,
KeyboardAvoidingView,
Platform,
Pressable,
} from 'react-native';
// import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { Header } from '@components';
import { useAppTheme } from '@theme';
import { getStyles } from './walletScreen.styles';
import { WalletTransaction } from '@interfaces';
import { useAppDispatch, useAppSelector } from '@store';
import { getAllWalletTransactions, initializeWalletTopUpThunk } from './thunk';
import { useFocusEffect } from '@react-navigation/native';
import { getWalletBalanceThunk } from '../accountScreen';
import { useStripePayment } from '../checkoutPaymentScreen/hooks/useStripePayment';
import { useTopUp } from './hooks/useTopUp';
// import { addMoneyThunk } from './thunk'; // wire this up to your actual top-up API
const WalletScreen = () => {
const { colors } = useAppTheme();
const styles = getStyles(colors);
const dispatch = useAppDispatch();
const { walletTransactions } = useAppSelector(state => state.wallet);
const { wallet } = useAppSelector(state => state.account);
const [isModalVisible, setIsModalVisible] = useState(false);
const [amount, setAmount] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState('');
const { topUpCardPayment, isPaymentProcessing } = useTopUp();
useFocusEffect(
useCallback(() => {
dispatch(getAllWalletTransactions());
dispatch(getWalletBalanceThunk());
}, []),
);
const openModal = () => {
setAmount('');
setError('');
setIsModalVisible(true);
};
const closeModal = () => {
if (isSubmitting) return;
setIsModalVisible(false);
};
const handleAmountChange = (text: string) => {
// allow only digits and a single decimal point
const sanitized = text.replace(/[^0-9.]/g, '');
setAmount(sanitized);
if (error) setError('');
};
const handleTopUp = async () => {
const numericAmount = parseFloat(amount);
if (!amount || isNaN(numericAmount) || numericAmount <= 0) {
setError('Please enter a valid amount');
return;
}
try {
setIsSubmitting(true);
const payload = {
amount: numericAmount,
paymentMethod: 'CARD'
}
const response = await dispatch(initializeWalletTopUpThunk(payload)).unwrap();
const success = await topUpCardPayment(response);
setIsSubmitting(false);
setIsModalVisible(false);
setAmount('');
} catch (e) {
setIsSubmitting(false);
setError('Something went wrong. Please try again.');
}
};
const renderItem = ({ item }: { item: WalletTransaction }) => {
const isCredit = item.direction === 'IN';
return (
{/* */}
{item.type}
{item.transactionReference}
{new Date(item.createdAt).toLocaleString()}
{isCredit ? '+' : '-'}₹{item.amount}
Bal ₹{item.balanceAfter}
);
};
return (
Available Balance
₹{wallet?.balance.toFixed(2)}
{/* */}
Add Money
Recent Transactions
item.id}
renderItem={renderItem}
showsVerticalScrollIndicator={false}
contentContainerStyle={styles.listContainer}
ListEmptyComponent={
{/* */}
No Transactions Yet
}
/>
Add Money
Enter an amount to top up your wallet
₹
{error ? {error} : null}
{isSubmitting ? 'Processing...' : 'Add Top Up'}
Cancel
);
};
export default WalletScreen;