import { products_api } from '@/screens/home/lib/api'; import { useMutation, useQuery } from '@tanstack/react-query'; import { AxiosError } from 'axios'; import { ChevronLeft, ChevronRight } from 'lucide-react-native'; import React, { Dispatch, SetStateAction, useState } from 'react'; import { ActivityIndicator, FlatList, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import { useTheme } from '../ThemeContext'; interface Category { id: number; name: string; code: string; external_id: string | null; level: number; is_leaf: boolean; icon_name: string | null; } interface CategoryResponse { data: { data: Category[]; }; } interface Props { selectedCategories: Category[]; setSelectedCategories: Dispatch>; } export default function CategorySelection({ selectedCategories, setSelectedCategories }: Props) { const [currentCategories, setCurrentCategories] = useState([]); const [history, setHistory] = useState<{ parentId: number | null; categories: Category[] }[]>([]); const [currentParentId, setCurrentParentId] = useState(null); const { isDark } = useTheme(); const theme = { cardBg: isDark ? '#1e293b' : '#f8fafc', cardBorder: isDark ? '#334155' : '#e2e8f0', text: isDark ? '#cbd5e1' : '#334155', textSelected: '#ffffff', primary: '#2563eb', primaryBg: '#3b82f6', error: '#ef4444', backButtonBg: isDark ? '#1e293b' : '#e2e8f0', chevronColor: isDark ? '#94a3b8' : '#64748b', shadow: isDark ? '#000' : '#94a3b8', }; const { data: rootData, isLoading: rootLoading, error: rootError, } = useQuery({ queryKey: ['categories'], queryFn: async () => products_api.getCategorys(), select(data) { setCurrentCategories(data.data.data); setCurrentParentId(null); return data; }, }); const { mutate, isPending: mutatePending } = useMutation({ mutationFn: (id: number) => products_api.getCategorys({ parent: id }), onSuccess: (response: CategoryResponse, id) => { const childCategories = response.data.data; setHistory((prev) => [...prev, { parentId: currentParentId, categories: currentCategories }]); setCurrentCategories(childCategories); setCurrentParentId(id); }, onError: (err: AxiosError) => { console.error('Child yuklashda xato:', err); }, }); const toggleCategory = (category: Category) => { if (category.is_leaf) { setSelectedCategories((prev) => { const exists = prev.find((c) => c.id === category.id); if (exists) return prev.filter((c) => c.id !== category.id); return [...prev, category]; }); } else { mutate(category.id); } }; const goBack = () => { if (history.length > 0) { const previous = history[history.length - 1]; setCurrentCategories(previous.categories); setCurrentParentId(previous.parentId); setHistory((prev) => prev.slice(0, -1)); } }; const isLoading = rootLoading || mutatePending; const error = rootError; const renderCategory = ({ item: category }: { item: Category }) => { const isSelected = selectedCategories.some((c) => c.id === category.id); return ( toggleCategory(category)} > {category.name} {!category.is_leaf && ( )} ); }; if (isLoading && currentCategories.length === 0) { return ( ); } if (error && currentCategories.length === 0) { return ( Ma'lumot yuklashda xatolik yuz berdi ); } if (currentCategories.length === 0) { return ( Kategoriyalar topilmadi ); } return ( {history.length > 0 && ( )} item.id.toString()} showsVerticalScrollIndicator={true} ItemSeparatorComponent={() => } /> ); } const styles = StyleSheet.create({ container: { flex: 1, marginBottom: 20, }, centerContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, header: { flexDirection: 'row', alignItems: 'center', gap: 12, marginBottom: 12, paddingHorizontal: 12, }, backButton: { paddingHorizontal: 12, paddingVertical: 6, borderRadius: 8, }, chip: { paddingHorizontal: 16, paddingVertical: 12, borderRadius: 20, borderWidth: 1, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.05, shadowRadius: 2, elevation: 1, }, chipSelected: { shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.3, shadowRadius: 4, elevation: 3, }, chipText: { fontSize: 14, fontWeight: '500', flex: 1, }, chipTextSelected: { fontWeight: '600', }, });