diff --git a/src/hooks/useFirestoreCollectionWithAtom.ts b/src/hooks/useFirestoreCollectionWithAtom.ts index 048ece4c..10dc8aab 100644 --- a/src/hooks/useFirestoreCollectionWithAtom.ts +++ b/src/hooks/useFirestoreCollectionWithAtom.ts @@ -130,7 +130,8 @@ export function useFirestoreCollectionWithAtom( page, pageSize, filters, - orders + orders, + onError ), (next, prev) => { // If filters are not equal, update the query @@ -152,6 +153,7 @@ export function useFirestoreCollectionWithAtom( // Create listener useEffect(() => { + console.log("memoizedQuery", memoizedQuery); // If path is invalid and no memoizedQuery was created, don’t continue if (!memoizedQuery) return; @@ -290,43 +292,49 @@ const getQuery = ( page: number, pageSize: number, filters: IUseFirestoreCollectionWithAtomOptions["filters"], - orders: IUseFirestoreCollectionWithAtomOptions["orders"] + orders: IUseFirestoreCollectionWithAtomOptions["orders"], + onError: IUseFirestoreCollectionWithAtomOptions["onError"] ) => { if (!path || (Array.isArray(pathSegments) && pathSegments.some((x) => !x))) return null; - let collectionRef: Query; + try { + let collectionRef: Query; - if (collectionGroup) { - collectionRef = queryCollectionGroup( - firebaseDb, - [path, ...((pathSegments as string[]) || [])].join("/") - ) as Query; - } else { - collectionRef = collection( - firebaseDb, - path, - ...((pathSegments as string[]) || []) - ) as CollectionReference; + if (collectionGroup) { + collectionRef = queryCollectionGroup( + firebaseDb, + [path, ...((pathSegments as string[]) || [])].join("/") + ) as Query; + } else { + collectionRef = collection( + firebaseDb, + path, + ...((pathSegments as string[]) || []) + ) as CollectionReference; + } + + if (!collectionRef) return null; + + const limit = (page + 1) * pageSize; + const firestoreFilters = tableFiltersToFirestoreFilters(filters || []); + + return { + query: query( + collectionRef, + queryLimit((page + 1) * pageSize), + ...firestoreFilters, + ...(orders?.map((order) => orderBy(order.key, order.direction)) || []) + ), + page, + limit, + firestoreFilters, + orders, + }; + } catch (e) { + if (onError) onError(e as FirestoreError); + return null; } - - if (!collectionRef) return null; - - const limit = (page + 1) * pageSize; - const firestoreFilters = tableFiltersToFirestoreFilters(filters || []); - - return { - query: query( - collectionRef, - queryLimit((page + 1) * pageSize), - ...firestoreFilters, - ...(orders?.map((order) => orderBy(order.key, order.direction)) || []) - ), - page, - limit, - firestoreFilters, - orders, - }; }; /** diff --git a/src/sources/TableSourceFirestore/handleFirestoreError.tsx b/src/sources/TableSourceFirestore/handleFirestoreError.tsx index 01698269..0dda67cb 100644 --- a/src/sources/TableSourceFirestore/handleFirestoreError.tsx +++ b/src/sources/TableSourceFirestore/handleFirestoreError.tsx @@ -44,5 +44,12 @@ export const handleFirestoreError = ( return; } + if (error.code === "invalid-argument") { + enqueueSnackbar("Cannot sort by this column with the current set filters", { + variant: "error", + }); + return; + } + elevateError(error); };