From 504fa71a0a3d6276ef18c791fcd22237f5dca426 Mon Sep 17 00:00:00 2001 From: Rohitbels Date: Fri, 25 Nov 2022 16:03:54 +0530 Subject: [PATCH 1/3] [firestore] Supporting Query Aggregate Count with useCollectionCountOnce --- firestore/index.ts | 1 + firestore/types.ts | 10 +++++++++ firestore/useCollection.ts | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/firestore/index.ts b/firestore/index.ts index dde6ffe..090b412 100644 --- a/firestore/index.ts +++ b/firestore/index.ts @@ -3,6 +3,7 @@ export { useCollectionOnce, useCollectionData, useCollectionDataOnce, + useCollectioCountOnce } from './useCollection'; export { useDocument, diff --git a/firestore/types.ts b/firestore/types.ts index e28ab41..e76d1c5 100644 --- a/firestore/types.ts +++ b/firestore/types.ts @@ -34,6 +34,16 @@ export type CollectionOnceHook = [ ...CollectionHook, () => Promise ]; + +export type CollectionCountHook = LoadingHook< + number, + FirestoreError +>; + +export type CollectionCountOnceHook = [ + ...CollectionCountHook, + () => Promise +]; export type CollectionDataHook = [ ...LoadingHook, QuerySnapshot | undefined diff --git a/firestore/useCollection.ts b/firestore/useCollection.ts index 48084d0..7d235e1 100644 --- a/firestore/useCollection.ts +++ b/firestore/useCollection.ts @@ -1,6 +1,7 @@ import { DocumentData, FirestoreError, + getCountFromServer, getDocs, getDocsFromCache, getDocsFromServer, @@ -14,6 +15,7 @@ import { useLoadingValue } from '../util'; import useIsMounted from '../util/useIsMounted'; import { useIsFirestoreQueryEqual } from './helpers'; import { + CollectionCountOnceHook, CollectionDataHook, CollectionDataOnceHook, CollectionHook, @@ -136,6 +138,49 @@ export const useCollectionDataOnce = ( return [values, loading, error, snapshots, reloadData]; }; +export const useCollectioCountOnce = ( + query?: Query | null +): CollectionCountOnceHook => { + const { error, loading, reset, setError, setValue, value } = useLoadingValue< + number, + FirestoreError + >(); + const isMounted = useIsMounted(); + const ref = useIsFirestoreQueryEqual>(query, reset); + + const loadData = useCallback( + async (query?: Query | null) => { + if (!query) { + setValue(undefined); + return; + } + + try { + const result = await getCountFromServer(query); + if (isMounted) { + setValue(result?.data()?.count); + } + } catch (error) { + if (isMounted) { + setError(error as FirestoreError); + } + } + }, + [] + ); + + const reloadData = useCallback(() => loadData(ref.current), [ + loadData, + ref.current, + ]); + + useEffect(() => { + loadData(ref.current); + }, [ref.current]); + + return [value, loading, error, reloadData]; +}; + const getValuesFromSnapshots = ( snapshots: QuerySnapshot | undefined, options?: SnapshotOptions, From 55b5162fa900544d8bf4f2c33494425474d2fd32 Mon Sep 17 00:00:00 2001 From: Rohitbels Date: Sat, 14 Jan 2023 20:44:04 +0530 Subject: [PATCH 2/3] Update firestore/useCollection.ts Co-authored-by: Taylor Buckner <32392635+tabuckner@users.noreply.github.com> --- firestore/useCollection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/useCollection.ts b/firestore/useCollection.ts index 7d235e1..a2feb0c 100644 --- a/firestore/useCollection.ts +++ b/firestore/useCollection.ts @@ -138,7 +138,7 @@ export const useCollectionDataOnce = ( return [values, loading, error, snapshots, reloadData]; }; -export const useCollectioCountOnce = ( +export const useCollectionCountOnce = ( query?: Query | null ): CollectionCountOnceHook => { const { error, loading, reset, setError, setValue, value } = useLoadingValue< From 8bedc7bf6e806f42b224b806c8762570890401b9 Mon Sep 17 00:00:00 2001 From: Rohitbels Date: Sat, 14 Jan 2023 20:44:10 +0530 Subject: [PATCH 3/3] Update firestore/index.ts Co-authored-by: Taylor Buckner <32392635+tabuckner@users.noreply.github.com> --- firestore/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/index.ts b/firestore/index.ts index 090b412..85ec4e7 100644 --- a/firestore/index.ts +++ b/firestore/index.ts @@ -3,7 +3,7 @@ export { useCollectionOnce, useCollectionData, useCollectionDataOnce, - useCollectioCountOnce + useCollectionCountOnce } from './useCollection'; export { useDocument,