import { useQuery } from '@tanstack/react-query';
import packetsApi from 'api/packets';
import LoadingScreen from 'components/LoadingScreen';
import Navbar from 'components/Navbar';
import Navigation from 'components/Navigation';
import NoResult from 'components/NoResult';
import Pagination from 'components/Pagination';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
RefreshControl,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Payment from './Payment';
const Wallet = () => {
const { t } = useTranslation();
const [refreshing, setRefreshing] = useState(false);
const [page, setPage] = useState(0);
const [pageAvia, setPageAvia] = useState(0);
const [selectedType, setSelectedType] = useState<'AVIA' | 'AUTO'>('AVIA');
const {
data: packets,
isFetching,
isLoading,
isError,
refetch,
} = useQuery({
queryKey: ['packets', page],
queryFn: () =>
packetsApi.getPackets({
page,
size: 10,
cargoType: 'AUTO',
direction: 'ASC',
sort: 'id',
}),
});
const {
data: packetsAvia,
isFetching: isFetchingAvia,
isLoading: isLoadingAvia,
isError: isErrorAvia,
refetch: refetchAvia,
} = useQuery({
queryKey: ['packetsAvia', pageAvia],
queryFn: () =>
packetsApi.getPackets({
page: pageAvia,
size: 10,
cargoType: 'AVIA',
direction: 'ASC',
sort: 'id',
}),
});
useEffect(() => {
const loadInitialData = async () => {
try {
await new Promise(resolve => setTimeout(resolve, 1000));
refetch();
refetchAvia();
} catch (error) {
console.error('Wallet loading error:', error);
refetch();
refetchAvia();
}
};
loadInitialData();
}, []);
const onRefresh = useCallback(async () => {
try {
setRefreshing(true);
await refetch();
} catch (error) {
console.error('Refresh error:', error);
} finally {
setRefreshing(false);
}
}, [refetch]);
const refreshControl = useMemo(
() => ,
[refreshing, onRefresh],
);
if (isLoading || isFetching || isLoadingAvia || isFetchingAvia) {
return (
);
}
if (isError || isErrorAvia) {
return (
);
}
if (
(packets && !(packets?.data.length > 0)) ||
(packetsAvia && !(packetsAvia?.data.length > 0))
) {
return (
{t("To'lov")}
);
}
return (
{t("To'lov")}
setSelectedType('AVIA')}
>
AVIA
setSelectedType('AUTO')}
>
AUTO
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
paddingHorizontal: 20,
paddingTop: 5,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#333',
},
cards: {
flexDirection: 'row',
justifyContent: 'space-around',
paddingHorizontal: 20,
paddingBottom: 10,
},
card: {
alignItems: 'center',
justifyContent: 'center',
width: 100,
height: 100,
backgroundColor: '#f0f0f0',
borderRadius: 10,
padding: 10,
},
cardText: {
marginTop: 5,
fontSize: 12,
color: '#555',
textAlign: 'center',
},
});
export default Wallet;