import { SimplePagination } from '@components/common/SimplePagination.js'; import { Button } from '@components/common/ui/Button.js'; import { Input } from '@components/common/ui/Input.js'; import { Skeleton } from '@components/common/ui/Skeleton.js'; import { Check } from 'lucide-react'; import React from 'react'; import { useQuery } from 'urql'; import { AtLeastOne } from '../../types/atLeastOne.js'; const SearchQuery = ` query Query ($filters: [FilterInput!]) { categories(filters: $filters) { items { categoryId uuid name path { name } } total } } `; interface CategoryIdentifier { categoryId?: string | number; uuid?: string; } const CategoryListSkeleton: React.FC = () => { const skeletonItems = Array(5).fill(0); return (
{skeletonItems.map((_, index) => (
))}
); }; const isCategorySelected = ( category: CategoryIdentifier, selectedCategories: AtLeastOne[] ): boolean => { return selectedCategories.some( (selected) => (selected?.categoryId && selected.categoryId === category.categoryId) || (selected?.uuid && selected.uuid === category.uuid) ); }; const CategorySelector: React.FC<{ onSelect: (id: string | number, uuid: string, name: string) => void; onUnSelect: (id: string | number, uuid: string, name: string) => void; selectedCategories: AtLeastOne[]; }> = ({ onSelect, onUnSelect, selectedCategories }) => { const [internalSelectedCategories, setInternalSelectedCategories] = React.useState[]>(selectedCategories || []); const [loading, setLoading] = React.useState(false); const limit = 10; const [inputValue, setInputValue] = React.useState(''); const [page, setPage] = React.useState(1); const [result, reexecuteQuery] = useQuery({ query: SearchQuery, variables: { filters: inputValue ? [ { key: 'name', operation: 'like', value: inputValue }, { key: 'page', operation: 'eq', value: page.toString() }, { key: 'limit', operation: 'eq', value: limit.toString() } ] : [ { key: 'limit', operation: 'eq', value: limit.toString() }, { key: 'page', operation: 'eq', value: page.toString() } ] }, pause: true }); React.useEffect(() => { reexecuteQuery({ requestPolicy: 'network-only' }); }, [page]); React.useEffect(() => { const timer = setTimeout(() => { setLoading(false); if (inputValue !== '') { reexecuteQuery({ requestPolicy: 'network-only' }); } }, 1500); return () => clearTimeout(timer); }, [inputValue]); const { data, fetching, error } = result as { data: { categories: { items: Array<{ categoryId: string | number; uuid: string; name: string; path: Array<{ name: string }>; }>; total: number; }; }; fetching: boolean; error: Error | undefined; }; if (error) { return (

There was an error fetching categories. {error.message}

); } return (
) => { setInputValue(e.target.value); setLoading(true); }} />
{(fetching || loading) && } {!fetching && data && (
{data.categories.items.length === 0 && (
{inputValue ? (

No categories found for query "{inputValue}”

) : (

You have no categories to display

)}
)} {data.categories.items.map((cat) => (

{cat.path.map((item, index) => ( {item.name} {index < cat.path.length - 1 && ' > '} ))}

{!isCategorySelected(cat, internalSelectedCategories) && ( )} {isCategorySelected(cat, internalSelectedCategories) && ( )}
))}
)}
); }; export { CategorySelector };