Skip to content

Commit

Permalink
Revert "Add converter to useDocument (#69)" (#70)
Browse files Browse the repository at this point in the history
This reverts commit 3259083.
  • Loading branch information
erayerdin authored Feb 10, 2024
1 parent 3259083 commit d17e041
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 90 deletions.
45 changes: 2 additions & 43 deletions docs/hooks/useDocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,22 @@ tags:

```typescript
const docRef = doc(firestore, "collectionName", "documentId");
const { loading, snapshot, error } = useDocument({
reference: docRef,
});
const { loading, snapshot, error } = useDocument(docRef);
```

By default, `useDocument` retrieves a document only once. If you need realtime updates, you can set `options.listen` to `true` as below:

```typescript
const { loading, snapshot, error } = useDocument({
reference: docRef,
options: { listen: true },
});
const { loading, snapshot, error } = useDocument(docRef, { listen: true });
```

`useDocument` also supports converting data from Firestore. The simples example would be:

```typescript
type Profile = {
id: string;
displayName: string;
bio: string;
profilePicture: string;
}

const {
loading,
data, // notice how we get `data` instead of `snapshot`
error
} = useDocument({
reference: docRef,
converter: (snapshot): Profile => {
const data = snapshot.data();

if (data) { // if data exists
return {
id: snapshot.id,
...data, // rest is what we got
}
}
}
});
```

!!! warning
`data` will always be `undefined` if no converter is provided.

## Input Parameters

Input parameters for `useDocument` hook is as follows:

| Name | Type | Description | Required | Default Value |
|---|---|---|---|---|
| `reference` | [`firebase/firestore/DocumentReference`][DocumentReferenceRefDoc] | Reference to a document in Firestore. || - |
| `converter` | `(snapshot: DocumentSnapshot) => O` where `O` is a generic type.[^refDocumentSnapshot] | Converter for data. || `() => undefined` |
| `options` | Object | Options for the hook. || `{ listen: false }` |
| `options.listen` | `boolean` | Whether to listen to realtime changes of the document or not. || `false` |

Expand All @@ -80,14 +42,11 @@ Input parameters for `useDocument` hook is as follows:
|---|---|---|
| `loading` | `boolean` | Whether the hook is loading the document or not. |
| `snapshot` | [`firebase/firestore/DocumentSnapshot`][DocumentSnapshotRefDoc] or `undefined` | Snapshot of the retrieved document. |
| `data` | `O` or `undefined` | Type-safe data if `converter` is defined in input parameters. |
| `error` | [`firebase/FirebaseError`][FirebaseErrorRefDoc] or `undefined` | The instance of error if any. |

!!! warning
Only [`firebase/FirebaseError`][FirebaseErrorRefDoc] is caught if any. `error` will not be an instance of another type.

[^refDocumentSnapshot]: Refer to [DocumentSnapshot][DocumentSnapshotRefDoc].

[DocumentReferenceRefDoc]: https://firebase.google.com/docs/reference/node/firebase.firestore.DocumentReference
[DocumentSnapshotRefDoc]: https://firebase.google.com/docs/reference/node/firebase.firestore.DocumentSnapshot
[FirebaseErrorRefDoc]: https://firebase.google.com/docs/reference/node/firebase.FirebaseError
34 changes: 3 additions & 31 deletions src/firestore/useDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("later useDocument hook", () => {
await deleteDoc(docRef);
});

it("should have a snapshot", async () => {
it("have a snapshot", async () => {
// setup
await deleteDoc(docRef);
await setDoc(docRef, docData);
Expand All @@ -87,35 +87,7 @@ describe("later useDocument hook", () => {
await deleteDoc(docRef);
});

it("should have data", async () => {
// setup
await deleteDoc(docRef);
await setDoc(docRef, docData);
type DocType = {
id: string;
displayName: string;
};

// test
const { result } = renderHook(() =>
useDocument({
reference: docRef,
converter: (snapshot): DocType => ({
id: snapshot.id,
displayName: snapshot.data()?.displayName,
}),
}),
);
await sleep(250);
const { data } = result.current;
expect(data?.id).not.toBeUndefined();
expect(data?.displayName).toBe("Use Document");

// teardown
await deleteDoc(docRef);
});

it("should have no error", async () => {
it("have no error", async () => {
// setup
await deleteDoc(docRef);
await setDoc(docRef, docData);
Expand Down Expand Up @@ -149,7 +121,7 @@ describe.skip("later listen useDocument hook", () => {
await deleteDoc(docRef);
});

it("should have no snapshot", async () => {
it("have no snapshot", async () => {
// setup
await deleteDoc(docRef);
await setDoc(docRef, docData);
Expand Down
21 changes: 5 additions & 16 deletions src/firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,29 @@ import {
} from "firebase/firestore";
import { useEffect, useState } from "react";

type UseDocumentParams<O> = {
type UseDocumentParams = {
reference: DocumentReference;
converter?: (snapshot: DocumentSnapshot) => O | undefined;
options?: {
listen: boolean;
};
};

type UseDocument<O> = {
type UseDocument = {
loading: boolean;
snapshot?: DocumentSnapshot;
data?: O;
error?: FirebaseError;
};

export const useDocument = <O>({
export const useDocument = ({
reference,
converter = () => undefined,
options = { listen: false },
}: UseDocumentParams<O>): UseDocument<O> => {
}: UseDocumentParams): UseDocument => {
const { listen } = options;

const [loading, setLoading] = useState<boolean>(true);
const [snapshot, setSnapshot] = useState<DocumentSnapshot | undefined>();
const [data, setData] = useState<O | undefined>();
const [error, setError] = useState<FirebaseError | undefined>();

useEffect(() => {
if (snapshot) {
const d = converter(snapshot);
setData(d);
}
}, [snapshot, converter]);

useEffect(() => {
setLoading(true);
if (listen) {
Expand Down Expand Up @@ -79,5 +68,5 @@ export const useDocument = <O>({
}
}, [listen, reference]);

return { loading, snapshot, data, error };
return { loading, snapshot, error };
};

0 comments on commit d17e041

Please sign in to comment.