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!]) { attributeGroups(filters: $filters) { items { attributeGroupId uuid groupName } total } } `; interface AttributeGroupIdentifier { attributeGroupId?: string | number; uuid?: string; } const AttributeGroupListSkeleton: React.FC = () => { const skeletonItems = Array(5).fill(0); return (
{skeletonItems.map((_, index) => (
))}
); }; const isAttributeGroupSelected = ( attributeGroup: AttributeGroupIdentifier, selectedAttributeGroups: AtLeastOne[] ): boolean => { return selectedAttributeGroups.some( (selected) => (selected?.attributeGroupId && selected.attributeGroupId === attributeGroup.attributeGroupId) || (selected?.uuid && selected.uuid === attributeGroup.uuid) ); }; const AttributeGroupSelector: React.FC<{ onSelect: (id: string | number, uuid: string, name: string) => void; onUnSelect: (id: string | number, uuid: string, name: string) => void; selectedAttributeGroups: AtLeastOne[]; }> = ({ onSelect, onUnSelect, selectedAttributeGroups }) => { const [internalSelectedAttributeGroups, setInternalSelectedAttributeGroups] = React.useState[]>( selectedAttributeGroups || [] ); 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: { attributeGroups: { items: Array<{ attributeGroupId: string | number; uuid: string; groupName: string; }>; total: number; }; }; fetching: boolean; error: Error | undefined; }; if (error) { return (

There was an error fetching attribute groups. {error.message}

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

No attribute groups found for query "{inputValue} ”

) : (

You have no attribute groups to display

)}
)} {data.attributeGroups.items.map((a) => (

{a.groupName}

{!isAttributeGroupSelected( a, internalSelectedAttributeGroups ) && ( )} {isAttributeGroupSelected( a, internalSelectedAttributeGroups ) && ( )}
))}
)}
); }; export { AttributeGroupSelector };