import { useTheme } from '@/components/ThemeContext'; import { useGlobalRefresh } from '@/components/ui/RefreshContext'; import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Image as ExpoImage } from 'expo-image'; import { useRouter } from 'expo-router'; import { ArrowLeft, Edit2, Package, Plus, Trash2 } from 'lucide-react-native'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, Alert, 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 = 5; export default function MyServicesScreen() { const router = useRouter(); const { onRefresh, refreshing } = useGlobalRefresh(); const queryClient = useQueryClient(); const { isDark } = useTheme(); const { t } = useTranslation(); /* ================= QUERY ================= */ const { data, isLoading, isError, fetchNextPage, hasNextPage } = useInfiniteQuery({ queryKey: ['my_services'], queryFn: async ({ pageParam = 1 }) => { const res = await user_api.my_sevices({ page: pageParam, my: true, 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 allServices = data?.pages.flatMap((p) => p.results) ?? []; const { mutate } = useMutation({ mutationFn: (id: number) => user_api.delete_service(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['my_services'] }); }, }); const handleDelete = (id: number) => { Alert.alert(t("Xizmatni o'chirish"), t("Rostdan ham bu xizmatni o'chirmoqchimisiz?"), [ { text: t('Bekor qilish'), style: 'cancel' }, { text: t("O'chirish"), style: 'destructive', onPress: () => mutate(id), }, ]); }; if (isLoading) { return ( router.push('/profile')}> {t('Xizmatlar')} router.push('/profile/products/add')}> ); } if (isError) { return ( {t('Xatolik yuz berdi')} ); } return ( {/* HEADER */} router.push('/profile')}> {t('Xizmatlar')} router.push('/profile/products/add')}> {/* LIST */} item.id.toString()} contentContainerStyle={styles.list} onEndReached={() => hasNextPage && fetchNextPage()} refreshControl={ } renderItem={({ item }) => { const fileUrl = item.files?.length > 0 ? item.files[0]?.file : null; return ( router.push({ pathname: `/profile/products/edit/[id]`, params: { id: item.id } }) } > {/* MEDIA */} {fileUrl ? ( ) : ( {t("Media yo'q")} )} {/* CONTENT */} { e.stopPropagation(); router.push({ pathname: `/profile/products/edit/[id]`, params: { id: item.id }, }); }} > { e.stopPropagation(); handleDelete(item.id); }} > {item.title} {item.description} {item.category.map((category, index) => ( {category.name} ))} ); }} ListEmptyComponent={ {t("Hozircha xizmatlar yo'q")} router.push('/profile/products/add')} > {t("Xizmat qo'shish")} } /> ); } /* ================= STYLES ================= */ const styles = StyleSheet.create({ container: { flex: 1 }, topHeader: { flexDirection: 'row', justifyContent: 'space-between', padding: 16, alignItems: 'center', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 3, }, headerTitle: { fontSize: 18, fontWeight: '700', flex: 1, marginLeft: 10 }, list: { padding: 16, gap: 16 }, card: { borderRadius: 20, overflow: 'hidden' }, mediaContainer: { width: '100%', height: 200 }, media: { width: '100%', height: '100%' }, videoPlaceholder: { flex: 1, justifyContent: 'center', alignItems: 'center' }, contentContainer: { padding: 20, gap: 12 }, title: { fontSize: 18, fontWeight: '700' as const }, description: { fontSize: 15, lineHeight: 22 }, categoriesContainer: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 }, categoryChip: { paddingHorizontal: 12, paddingVertical: 6, borderRadius: 12, }, categoryText: { fontSize: 13, fontWeight: '500' as const }, emptyContainer: { alignItems: 'center', justifyContent: 'center', paddingVertical: 80, gap: 16 }, emptyText: { fontSize: 17, fontWeight: '600' as const }, emptyButton: { flexDirection: 'row', alignItems: 'center', gap: 8, backgroundColor: '#3b82f6', paddingHorizontal: 24, paddingVertical: 12, borderRadius: 12, marginTop: 8, }, emptyButtonText: { fontSize: 16, fontWeight: '600' as const, color: '#ffffff' }, center: { flex: 1, justifyContent: 'center', alignItems: 'center' }, loading: { fontSize: 16, fontWeight: '500' }, error: { color: '#ef4444', fontSize: 16, fontWeight: '500' }, actions: { flexDirection: 'row', position: 'absolute', right: 5, top: 5, gap: 8, }, actionButton: { width: 36, height: 36, borderRadius: 18, backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center', }, });