Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/initial value #270

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ The `useCollectionData` hook takes the following parameters:
- `options`: (optional) `Object` with the following parameters:
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand All @@ -154,6 +155,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
- `getOptions`: (optional) `Object` to customise how the collection is loaded
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cache.
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand Down Expand Up @@ -247,6 +249,7 @@ The `useDocumentData` hook takes the following parameters:
- `options`: (optional) `Object` with the following parameters:
- `snapshotListenOptions`: (optional) `firestore.SnapshotListenOptions` to customise how the collection is loaded
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand All @@ -273,6 +276,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
- `getOptions`: (optional) `Object` to customise how the collection is loaded
- `source`: (optional): `'default' | 'server' | 'cache'` Describes whether we should get from server or cach
- `snapshotOptions`: (optional) `firestore.SnapshotOptions` to customise how data is retrieved from snapshots
- `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded

Returns:

Expand Down
3 changes: 3 additions & 0 deletions firestore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export type IDOptions<T> = {
export type Options = {
snapshotListenOptions?: SnapshotListenOptions;
};
export type InitialValueOptions<T> = {
initialValue?: T;
};
export type DataOptions<T> = Options & IDOptions<T>;
export type OnceOptions = {
getOptions?: GetOptions;
Expand Down
41 changes: 30 additions & 11 deletions firestore/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
CollectionOnceHook,
DataOptions,
GetOptions,
InitialValueOptions,
OnceDataOptions,
OnceOptions,
Options,
Expand Down Expand Up @@ -113,25 +114,39 @@ export const useCollectionOnce = <T = DocumentData>(

export const useCollectionData = <T = DocumentData>(
query?: Query<T> | null,
options?: DataOptions<T>
options?: DataOptions<T> & InitialValueOptions<T[]>
): CollectionDataHook<T> => {
const snapshotOptions = options?.snapshotOptions;
const [snapshots, loading, error] = useCollection<T>(query, options);
const values = getValuesFromSnapshots<T>(snapshots, snapshotOptions);

const initialValue = options?.initialValue;
const values = getValuesFromSnapshots<T>(
snapshots,
snapshotOptions,
initialValue
);

const resArray: CollectionDataHook<T> = [values, loading, error, snapshots];
return useMemo(() => resArray, resArray);
};

export const useCollectionDataOnce = <T = DocumentData>(
query?: Query<T> | null,
options?: OnceDataOptions<T>
options?: OnceDataOptions<T> & InitialValueOptions<T[]>
): CollectionDataOnceHook<T> => {
const snapshotOptions = options?.snapshotOptions;
const [snapshots, loading, error, loadData] = useCollectionOnce<T>(
query,
options
);
const values = getValuesFromSnapshots<T>(snapshots, snapshotOptions);

const initialValue = options?.initialValue;
const values = getValuesFromSnapshots<T>(
snapshots,
snapshotOptions,
initialValue
);

const resArray: CollectionDataOnceHook<T> = [
values,
loading,
Expand All @@ -143,13 +158,17 @@ export const useCollectionDataOnce = <T = DocumentData>(
};

const getValuesFromSnapshots = <T>(
snapshots?: QuerySnapshot<T>,
options?: SnapshotOptions
) => {
return useMemo(() => snapshots?.docs.map((doc) => doc.data(options)) as T[], [
snapshots,
options,
]);
snapshots: QuerySnapshot<T> | undefined,
options?: SnapshotOptions,
initialValue?: T[]
): T[] | undefined => {
return useMemo(
() =>
(snapshots?.docs.map((doc) => doc.data(options)) ?? initialValue) as
| T[]
| undefined,
[snapshots, options]
);
};

const getDocsFnFromGetOptions = (
Expand Down
25 changes: 15 additions & 10 deletions firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DocumentHook,
DocumentOnceHook,
GetOptions,
InitialValueOptions,
OnceDataOptions,
OnceOptions,
Options,
Expand Down Expand Up @@ -117,32 +118,36 @@ export const useDocumentOnce = <T = DocumentData>(

export const useDocumentData = <T = DocumentData>(
docRef?: DocumentReference<T> | null,
options?: DataOptions<T>
options?: DataOptions<T> & InitialValueOptions<T>
): DocumentDataHook<T> => {
const snapshotOptions = options?.snapshotOptions;
const [snapshot, loading, error] = useDocument<T>(docRef, options);
const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [
snapshot,
snapshotOptions,
]);

const initialValue = options?.initialValue;
const value = useMemo(
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
[snapshot, snapshotOptions, initialValue]
);

const resArray: DocumentDataHook<T> = [value, loading, error, snapshot];
return useMemo(() => resArray, resArray);
};

export const useDocumentDataOnce = <T = DocumentData>(
docRef?: DocumentReference<T> | null,
options?: OnceDataOptions<T>
options?: OnceDataOptions<T> & InitialValueOptions<T>
): DocumentDataOnceHook<T> => {
const snapshotOptions = options?.snapshotOptions;
const [snapshot, loading, error, loadData] = useDocumentOnce<T>(
docRef,
options
);
const value = useMemo(() => snapshot?.data(snapshotOptions) as T, [
snapshot,
snapshotOptions,
]);

const initialValue = options?.initialValue;
const value = useMemo(
() => (snapshot?.data(snapshotOptions) ?? initialValue) as T | undefined,
[snapshot, snapshotOptions, initialValue]
);

const resArray: DocumentDataOnceHook<T> = [
value,
Expand Down