import { useTheme } from '@/components/ThemeContext'; 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'; 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 | null; setSelectedCategories: Dispatch>; } interface HistoryItem { parentId: number | null; categories: Category[]; } export default function CategorySelect({ selectedCategories, setSelectedCategories }: Props) { const { isDark } = useTheme(); const [currentCategories, setCurrentCategories] = useState([]); const [currentParentId, setCurrentParentId] = useState(null); const [history, setHistory] = useState([]); // Root categories const { isLoading: rootLoading, error: rootError } = useQuery({ queryKey: ['categories'], queryFn: async () => products_api.getCategorys(), select(data) { setCurrentCategories(data.data.data); setCurrentParentId(null); setHistory([]); return data; }, }); // Child categories 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 category loading error:', err); }, }); const toggleCategory = (category: Category) => { if (category.is_leaf) { setSelectedCategories(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?.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()} scrollEnabled={false} showsVerticalScrollIndicator={true} ItemSeparatorComponent={() => } /> ); } const styles = StyleSheet.create({ container: { flex: 1, marginBottom: 20, }, darkBg: { backgroundColor: '#0f172a', }, lightBg: { backgroundColor: '#f8fafc', }, centerContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, header: { flexDirection: 'row', alignItems: 'center', gap: 12, marginBottom: 12, }, backButton: { paddingHorizontal: 12, paddingVertical: 6, borderRadius: 8, }, darkBackButton: { backgroundColor: '#1e293b', }, lightBackButton: { backgroundColor: '#ffffff', }, chip: { paddingHorizontal: 16, paddingVertical: 12, borderRadius: 20, borderWidth: 1, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.05, shadowRadius: 2, elevation: 1, }, darkChip: { backgroundColor: '#1e293b', borderColor: '#334155', }, lightChip: { backgroundColor: '#ffffff', borderColor: '#e2e8f0', }, chipSelected: { backgroundColor: '#3b82f6', borderColor: '#3b82f6', shadowColor: '#3b82f6', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.3, shadowRadius: 4, elevation: 3, }, chipText: { fontSize: 14, fontWeight: '500', flex: 1, }, darkChipText: { color: '#cbd5e1', }, lightChipText: { color: '#64748b', }, chipTextSelected: { color: '#ffffff', fontWeight: '600', }, darkText: { color: '#f1f5f9', }, lightText: { color: '#0f172a', }, });