Skip to content

Commit

Permalink
Complete the _hooks_ document
Browse files Browse the repository at this point in the history
  • Loading branch information
widoz committed Feb 13, 2024
1 parent 367d6fb commit 40180b7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ const [state, dispatch] = useEntitiesOptionsStorage<E, K>(
This hook represent the main api to get the entities records from the server.

This is different from the search api in the sense that, the search api is used to search for the entities belonging
to one or more kinds by a phrase. The records api is used to retrieve the entities belonging to a specific kind. Both solutions
to one or more kinds by a phrase. The records api is used to retrieve the entities belonging to a specific kind. Both
solutions
customize the query by additional arguments.

In the future the two api might be merged into one.

The current situation is due the fact the `useEntityRecords` does not allow more than one `kind` and the `useSearch` is not
The current situation is due the fact the `useEntityRecords` does not allow more than one `kind` and the `useSearch` is
not
capable of retrieving the entities **kinds**.

This hook is a decorator of the WordPress `useEntityRecords` hook, and it agument the original hook with some methods
This hook is a decorator of the WordPress `useEntityRecords` hook, and it augment the original hook with some methods
for an easy consumption.

- `records()` - The records of the entities.
Expand All @@ -50,7 +52,8 @@ for an easy consumption.
- `succeed()` - A boolean to check if the request succeed.

Regarding the parameters, along with the `kind` and the `name` (entity name) it accept additional arguments to
customize the query. The accepted arguments are the same arguments accepted by the respective WordPress Rest API endpoint.
customize the query. The accepted arguments are the same arguments accepted by the respective WordPress Rest API
endpoint.

The following example is retrieving the list of the `postType`s from the server.

Expand All @@ -72,8 +75,18 @@ This hook is a subset of the `useEntitiesRecords` hook. It is used to retrieve t
## `useQueryViewableTaxonomies`

This hook is a subset of the `useEntitiesRecords` hook. It is used to retrieve the list of the viewable taxonomies.
Differently than the post types the taxonomies does not have a `viewable` property, in this case we query
the `visibility.publicly_queryable` prop.

## `useSearch`

This hook will return a throttled function to search for the entities.
This hook will return a throttled function to search for the entities. In-fact it's more a _factory hook_. The returned
function get one single parameter the `phrase`. This is convenient because we do not have to pass multiple props down
the components three or use a context / state.

It requires a function `searchEntities` to perform the search to the server, the `kind` and `entities` to correctly
query for the new entities values. In the future there'll be the possibility to reuse the storage directly from within
the hook reducing the amount of parameters, and for last the `dispatch`, necessary to update the shared state.

When the state update fail for any reason an action `wp-entities-search.on-search.error` is fired. You can hook into it
and consume the given `error` instance. More on this in the [logging](./logging.md) documentation.

0 comments on commit 40180b7

Please sign in to comment.