Skip to main content

useAdminCollections

Overview

useAdminCollections is a React hook for managing collections in the admin panel. It provides full CRUD operations, item assignment to collections, paginated listing with search and sorting, and automatic cache invalidation. Built on top of TanStack React Query and the internal serverClient API layer.

Source: template/hooks/use-admin-collections.ts

Signature / Parameters

function useAdminCollections(params?: CollectionListParams): UseAdminCollectionsReturn

CollectionListParams

ParameterTypeDefaultDescription
pagenumberundefinedCurrent page number for pagination
limitnumberundefinedNumber of collections per page
searchstringundefinedSearch term to filter collections by name
includeInactivebooleanundefinedWhether to include inactive collections
sortBy'name' | 'item_count' | 'created_at'undefinedField to sort results by
sortOrder'asc' | 'desc'undefinedSort direction

Return Values

The hook returns an object with the following shape:

Data

PropertyTypeDescription
collectionsCollection[]Array of collections for the current page
totalnumberTotal number of collections matching current filters
pagenumberCurrent page number (defaults to 1)
totalPagesnumberTotal number of pages (defaults to 1)
limitnumberCurrent page size (defaults to param limit or 10)

Loading States

PropertyTypeDescription
isLoadingbooleantrue only on initial load (no cached data)
isSubmittingbooleantrue when any mutation (create/update/delete/assign) is pending

Actions

MethodSignatureDescription
createCollection(data: CreateCollectionRequest) => Promise<boolean>Create a new collection. Returns true on success.
updateCollection(id: string, data: UpdateCollectionRequest) => Promise<boolean>Update an existing collection by ID.
deleteCollection(id: string) => Promise<boolean>Delete a collection by ID.
assignItems(id: string, itemSlugs: string[]) => Promise<boolean>Assign items to a collection by their slugs.
fetchAssignedItems(id: string) => Promise<Array<{ id: string; name: string; slug: string }>>Fetch items currently assigned to a collection.

Utility

MethodSignatureDescription
refetch() => voidRe-execute the collections list query
refreshData() => voidClear server cache and invalidate all collection queries

Implementation Details

  • Query caching: Uses TanStack React Query with a 5-minute staleTime and 10-minute gcTime.
  • Toast notifications: All mutation success and error states trigger sonner toast notifications automatically.
  • Cache invalidation: After every successful mutation, the server-side API cache is cleared via apiUtils.clearCache() and all queries under the ['admin', 'collections'] key are invalidated.
  • Error handling: All action handlers return boolean -- true on success, false on failure. Errors are caught internally and surfaced via toast.

Query Keys

const QUERY_KEYS = {
collections: ['admin', 'collections'],
collectionsList: (params) => ['admin', 'collections', 'list', params],
collection: (id) => ['admin', 'collections', 'detail', id],
collectionItems: (id) => ['admin', 'collections', 'items', id],
};

API Endpoints

OperationMethodEndpoint
ListGET/api/admin/collections
CreatePOST/api/admin/collections
UpdatePUT/api/admin/collections/:id
DeleteDELETE/api/admin/collections/:id
Fetch itemsGET/api/admin/collections/:id/items
Assign itemsPOST/api/admin/collections/:id/items

Usage Examples

Basic collection listing with pagination

import { useAdminCollections } from '@/hooks/use-admin-collections';

function CollectionsPage() {
const [page, setPage] = useState(1);

const {
collections,
total,
totalPages,
isLoading,
} = useAdminCollections({ page, limit: 20 });

if (isLoading) return <Spinner />;

return (
<div>
<p>{total} collections found</p>
<CollectionsTable collections={collections} />
<Pagination current={page} total={totalPages} onChange={setPage} />
</div>
);
}

Searching and sorting

const { collections, isLoading } = useAdminCollections({
page: 1,
limit: 10,
search: 'featured',
sortBy: 'item_count',
sortOrder: 'desc',
includeInactive: true,
});

Creating a collection

const { createCollection, isSubmitting } = useAdminCollections();

const handleCreate = async () => {
const success = await createCollection({
name: 'Top Picks',
description: 'Curated top picks for the homepage',
isActive: true,
});
if (success) {
// Collection created, data auto-refreshes
}
};

Assigning items to a collection

const { assignItems, fetchAssignedItems } = useAdminCollections();

// Fetch currently assigned items
const currentItems = await fetchAssignedItems('collection-id-123');

// Assign new items by slug
const success = await assignItems('collection-id-123', [
'my-item-slug',
'another-item-slug',
]);