import { useTheme } from '@/components/ThemeContext'; import { useGlobalRefresh } from '@/components/ui/RefreshContext'; import { useInfiniteQuery } from '@tanstack/react-query'; import { router } from 'expo-router'; import { ArrowLeft, Award, Percent } from 'lucide-react-native'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, FlatList, Pressable, StyleSheet, Text, View } from 'react-native'; import { RefreshControl } from 'react-native-gesture-handler'; import { SafeAreaView } from 'react-native-safe-area-context'; import { user_api } from '../lib/api'; const PAGE_SIZE = 10; export default function BonusesScreen() { const { onRefresh, refreshing } = useGlobalRefresh(); const { t } = useTranslation(); const { isDark } = useTheme(); const formatAmount = (amount: number) => { return new Intl.NumberFormat('uz-UZ').format(amount) + " so'm"; }; const { data, isLoading, isError, fetchNextPage, hasNextPage } = useInfiniteQuery({ queryKey: ['my_bonuses'], queryFn: async ({ pageParam = 1 }) => { const res = await user_api.my_bonuses({ page: pageParam, page_size: PAGE_SIZE, }); const d = res?.data?.data; return { results: d?.results ?? [], current_page: d?.current_page ?? 1, total_pages: d?.total_pages ?? 1, }; }, getNextPageParam: (lastPage) => lastPage.current_page < lastPage.total_pages ? lastPage.current_page + 1 : undefined, initialPageParam: 1, }); const allBonuses = data?.pages.flatMap((p) => p.results) ?? []; if (isLoading) { return ( ); } if (isError) { return ( {t('Xatolik yuz berdi')} ); } return ( router.push('/profile')}> {t('Bonuslar')} item.id.toString()} onEndReached={() => hasNextPage && fetchNextPage()} refreshControl={ } contentContainerStyle={styles.list} renderItem={({ item }) => ( {item.percent} {item.ad.title} {item.ad.description} {t('Bonus miqdori')} {formatAmount(item.amount)} {t('Yaratilgan sana')} {new Date(item.created_at).toLocaleDateString('uz-UZ')} )} ListEmptyComponent={ {t("Hozircha bonuslar yo'q")} } /> ); } const styles = StyleSheet.create({ container: { flex: 1, }, list: { padding: 16, gap: 16, }, card: { borderRadius: 20, padding: 20, gap: 16, borderWidth: 1.5, }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, iconContainer: { width: 64, height: 64, borderRadius: 32, alignItems: 'center', justifyContent: 'center', }, percentageBadge: { flexDirection: 'row', alignItems: 'center', gap: 4, paddingHorizontal: 16, paddingVertical: 10, borderRadius: 20, }, percentageText: { fontSize: 17, fontWeight: '700' as const, }, title: { fontSize: 20, fontWeight: '700' as const, lineHeight: 26, }, description: { fontSize: 15, lineHeight: 22, }, footer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-start', paddingTop: 12, borderTopWidth: 1, }, amountContainer: { gap: 6, }, amountLabel: { fontSize: 12, fontWeight: '500' as const, }, amount: { fontSize: 22, fontWeight: '700' as const, color: '#3b82f6', }, dateContainer: { gap: 6, alignItems: 'flex-end', }, dateLabel: { fontSize: 12, fontWeight: '500' as const, }, date: { fontSize: 14, fontWeight: '600' as const, }, emptyContainer: { alignItems: 'center', justifyContent: 'center', paddingVertical: 80, gap: 16, }, emptyText: { fontSize: 17, fontWeight: '600' as const, }, topHeader: { flexDirection: 'row', gap: 10, padding: 16, alignItems: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 3, }, headerTitle: { fontSize: 18, fontWeight: '700', flex: 1, }, themeToggle: { padding: 8, }, center: { flex: 1, justifyContent: 'center', alignItems: 'center', }, loading: { fontSize: 16, fontWeight: '500', }, error: { color: '#ef4444', fontSize: 16, fontWeight: '500', }, });