Skip to content

Commit

Permalink
feat: Allow an initialValue to be passed as an option on data methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriceackel authored and Maurice Ackel committed Oct 10, 2022
1 parent 8dad1f2 commit fc87a3c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
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

0 comments on commit fc87a3c

Please sign in to comment.