import { useEffect, useState } from "react"; import { Entity, EntityCollection, FilterValues, FireCMSContext, User } from "../../types"; import { useDataSource } from "./useDataSource"; import { useNavigationController } from "../useNavigationController"; import { useFireCMSContext } from "../useFireCMSContext"; /** * @group Hooks and utilities */ export interface CollectionFetchProps> { /** * Absolute collection path */ path: string; /** * collection of the entity displayed by this collection */ collection: EntityCollection /** * Number of entities to fetch */ itemCount?: number; /** * Filter the fetched data by the property */ filterValues?: FilterValues>; /** * Sort the results by */ sortBy?: [Extract, "asc" | "desc"]; /** * Search string */ searchString?: string; } /** * @group Hooks and utilities */ export interface CollectionFetchResult> { data: Entity[]; dataLoading: boolean; noMoreToLoad: boolean; dataLoadingError?: Error; } /** * This hook is used to fetch collections using a given collection * @param path * @param collection * @param filterValues * @param sortBy * @param itemCount * @param searchString * @group Hooks and utilities */ export function useCollectionFetch, UserType extends User>( { path: inputPath, collection, filterValues, sortBy, itemCount, searchString }: CollectionFetchProps): CollectionFetchResult { const dataSource = useDataSource(collection); const navigationController = useNavigationController(); const path = navigationController.resolveAliasesFrom(inputPath); const sortByProperty = sortBy ? sortBy[0] : undefined; const currentSort = sortBy ? sortBy[1] : undefined; const context: FireCMSContext = useFireCMSContext(); const [data, setData] = useState[]>([]); const [dataLoading, setDataLoading] = useState(false); const [dataLoadingError, setDataLoadingError] = useState(); const [noMoreToLoad, setNoMoreToLoad] = useState(false); useEffect(() => { setDataLoading(true); const onEntitiesUpdate = async (entities: Entity[]) => { if (collection.callbacks?.onFetch) { try { entities = await Promise.all( entities.map((entity) => collection.callbacks!.onFetch!({ collection, path, entity, context }))); } catch (e: any) { console.error(e); } } setDataLoading(false); setDataLoadingError(undefined); setData(entities.map(e => ({ ...e, // values: sanitizeData(e.values, resolvedCollection.properties) }))); setNoMoreToLoad(!itemCount || entities.length < itemCount); }; const onError = (error: Error) => { console.error("ERROR", error); setDataLoading(false); setData([]); setDataLoadingError(error); }; if (dataSource.listenCollection) { return dataSource.listenCollection({ path, collection, onUpdate: onEntitiesUpdate, onError, searchString, filter: filterValues, limit: itemCount, startAfter: undefined, orderBy: sortByProperty, order: currentSort }); } else { dataSource.fetchCollection({ path, collection, searchString, filter: filterValues, limit: itemCount, startAfter: undefined, orderBy: sortByProperty, order: currentSort }) .then(onEntitiesUpdate) .catch(onError); return () => { }; } }, [path, itemCount, currentSort, sortByProperty, filterValues, searchString]); return { data, dataLoading, dataLoadingError, noMoreToLoad }; }