Skip to content

Commit

Permalink
Merge branch 'main' into typos
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor authored Feb 17, 2024
2 parents e1fdc16 + dbad586 commit f4b551d
Show file tree
Hide file tree
Showing 82 changed files with 6,833 additions and 4,804 deletions.
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
vendor
node_modules
build

.psalm
.github

./coverage
./sources/server
19 changes: 10 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const baseConfiguration = require('@wordpress/scripts/config/.eslintrc.js');

module.exports = {
...baseConfiguration,
ignorePatterns: [
...(baseConfiguration.ignorePatterns ?? []),
'**/sources/server/**/*.js',
],
extends: [ 'plugin:@wordpress/eslint-plugin/recommended' ],
ignorePatterns: [ '**/sources/server/**/*.js' ],
rules: {
...baseConfiguration.rules,
'@typescript-eslint/array-type': ['error', { default: 'generic' }],
'@wordpress/dependency-group': 'error',
'@wordpress/i18n-text-domain': [
'error',
{
allowedTextDomain: 'wp-entities-search',
},
],
'@typescript-eslint/array-type': [ 'error', { default: 'generic' } ],
},
settings: {
'import/resolver': {
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/js-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ jobs:
- name: Install
run: yarn install

- name: Coding Style
run: yarn cs

- name: Linting JavaScript
run: yarn lint:js

Expand Down
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@
/vendor/
/node_modules/

composer.lock
/composer.lock
/.pnp.*
/.yarn/*
!/.yarn/patches
!/.yarn/plugins
!/.yarn/releases
!/.yarn/sdks
!/.yarn/versions
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
./vendor
./node_modules
./build
./sources/server
16 changes: 0 additions & 16 deletions .prettierrc.js

This file was deleted.

Binary file removed .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ declare namespace EntitiesSearch {
interface SearchControl
extends Readonly<{
id?: string;
label?: string;
onChange(phrase: string | React.ChangeEvent<HTMLInputElement>);
}> {}

Expand Down
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# WordPress Entities Search

This package is a library exposing reusable Components and Utilities to help you build Entities searching and storage.

At it's core it permits to build components and reuse the WordPress Rest API to search for Posts associated to one or
more Post Types but also, to search for Terms belonging to one or more Taxonomies.

## Table of Content

1. [Installation](./docs/installation.md)
2. [Development](./docs/development.md)
3. [Api](./docs/api.md)
4. [Components](./docs/components.md)
5. [Hooks](./docs/hooks.md)
6. [Logging](./docs/logging.md)
7. [Storage](./docs/storage.md)

## License

This software is released under the ["GNU General Public License v2.0 or later"](./LICENSE) license.
68 changes: 68 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Api

The `api` define a `fetch` function which is there to hide the WordPress Fetch API. The reason behind this decision lies on the fact that we might want to add middlewares to the request in the future or introducing some global data manipulation.

The package provide then the following functions to interact with the WordPress API:

## `searchEntities`

This function is a wrapper for the Search Endpoint of the WordPress API. It will return a `Set` of entities matching the search query.

The possible query arguments are specified by the `EntitiesSearch.QueryArguments` type, an interface you can expand to add more arguments.

This function is not directly consumed by the components, and it is not intended to be used internally. The idea is to follow the Tell don't Ask principle and let the consumer specify how to search the Entities.

Below a possible example of its usage related to a component:

```tsx
// my-component.tsx
type Search = EntitiesSearch.SearchEntitiesFunction<string, string>;
type Props = {
search: Search
}

const MyComponent (props: Props) => {
const [entities, setEntities] = useState<Set>(new Set());

useEffect(() => {
props
.search(
'my search phrase',
'post',
new Set(['page']),
{
per_page: 10,
_fields: ['slug', 'title']
}
)
.then(setEntities);
}, []);

// Do something with the entities
}

// index.tsx
import {searchEntities} from 'wp-entities-search';

root.render(<MyComponent search={searchEntities} />);
```

### Parameters

The function accept four parameters

- `type` - The root type. We have two built-int supported types `post`, `term`.
- `subtype` - The subtype of the entity. For example, if the `type` is `post`, the `subtype` can be `page`, `post`, etc. WordPress exclude automatically the `attachment` subtype.
- `phrase` - The search phrase. This becomes the `s` parameter for the Search Endpoint.
- `queryArguments` - All the supported and extra query arguments. The `EntitiesSearch.QueryArguments` type is an interface you can expand to add more arguments.

### Return

The function return an immutable `Set` of entities.

NOTE: The function does not handle possible request exceptions.

### Aborting Requests

The `searchEntities` will automatically abort the requests with the same parameters if a new request is made before the previous one is completed.

201 changes: 201 additions & 0 deletions docs/components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# Components

The Wp Entities Search provides a set of components you can use to build your search UI.

We have to distinguish between two types of components:

- **Base Components**: These are the basic building blocks of the search UI. They are the smallest components and are
used to build more complex components.
- **Composite Components**: These are more complex components that are built using the base ones.

The _Composite Component_ does not have a direct dependency on the _Base Components_, indeed a composite component
encapsulate the logic
that deal with the state management of the data and pass it down to the base components in form of properties via
the `children` function.

In a nutshell, a _Composite Component_ is configured from the external via props, dialog with the Rest API and pass the
data down to the _Base Components_.

## Base Components

- `PluralSelectControl` - A select control that allows multiple selections.
- `SingleSelectControl` - A select control that allows a single selection.
- `SearchControl` - An input control that allows the user to enter a search query.
- `ToggleControl` - A multi selection control that allows the user to toggle between multiple options.
- `RadioControl` - A single selection control that allows the user to select a single option.

## Composite Components

- `CompositeEntitiesByKind` - A composite component that displays a list of entities by kind. In this context _kind_
is `post` or `term` and _entity_ is the entity type of the content e.g. a `page` or a `category` term.

## Composite Entities by Kind

The `CompositeEntitiesByKind` is a _generic_ component that can be used to display a list of entities by kind. It acts
as a controller that fetches the data from the Rest API using a given function and pass it down to one or more _Base
Components_.
It is up to you how to consume the data given to the children. Note, even if you consume just one part of the given
data,
the component will fetch all the data necessary to render the UI.

This component is intended to fulfill a scenario where the user is looking for some entities belonging to a certain
_kind_,
for instance, selecting the `page` _kind_ will return a list of the available pages and to filter the result further it
is
possible to use a search function in conjunction with a `search` field.

An example of its usage is:

```jsx
import { CompositeEntitiesByKind } from 'wp-entities-search';

export function MyComponent(props) {
return <CompositeEntitiesByKind
searchEntities={
async (phrase, kind, queryArguments) => convertEntitiesToControlOptions(
await searchEntities('post', kind, phrase, queryArguments),
'title',
'id'
)
}
entities={{
value: new Set([13, 24, 55]),
onChange: (value) => {
// Do something with the selected entities
// Notice, here you'll receive the value as a string Set.
}
}}
kind={{
value: new Set(['page']),
options: [
{ label: 'Pages', value: 'page' },
{ label: 'Posts', value: 'post' },
],
onChange={(value) => {
// Do something with the selected kind
}}
}}
>
{(entities, kind, search) => (
<>
<ToggleControl
value={kind.value}
options={kind.options}
onChange={kind.onChange}
/>
<SearchControl
onChange={search.onChange}
/>
<ToggleControl
value={entities.value}
onChange={entities.onChange}
/>
</>
)}
</CompositeEntitiesByKind>;
}
```

In the example above we are passing a `searchEntities` function that will be used to fetch the entities from the Rest
API. This function is documented in the [api](./api.md) section.

What's important to know is that the `queryArguments` do not include the `_fields` property because it's not a concern
of the component decide which fields to retrieve, it's up to the consumer to decide which fields to retrieve. Anyhow,
you can still override the given configuration, but you have to be aware this might produce an incorrect result if not
done correctly. The component might pass arguments necessary to guarantee the consistency of the Set of the entities and
the kind.

In the following example we require `title` and `slug` fields to be retrieved from the Rest API.

```typescript
async (phrase, kind, queryArguments) => {
const fields = ['title', 'slug'];
return convertEntitiesToControlOptions(
await searchEntities('post', kind, phrase, {...queryArguments, fields}),
...fields
)
}
```

The `entities` and `kind` properties are the initial configuration, they'll change depending on what happen within the
respective `onChange` functions.

The `children` function will receive the `entities`, `kind` and `search` properties; notice the `search` is a function,
and it's up to the `SearchControl` to maintain the status of the value, the composite does hold the search value
internally, but it does not share it outside.

You're not forced to consume all the properties given to the `children`, you can remove the `SearchControl`
and let the user select only the retrieved entities. Moreover, you can also not have the Kind control at all and just
allow the search for one single kind.

In the example below we only allow to select the entities belonging to the `page` or `landing-page` kind not permitting
the user to switch between them.

```jsx
import { CompositeEntitiesByKind } from 'wp-entities-search';

export function MyComponent(props) {
return <CompositeEntitiesByKind
searchEntities={
async (phrase, kind, queryArguments) => convertEntitiesToControlOptions(
await searchEntities('post', kind, phrase, queryArguments),
'title',
'id'
)
}
entities={{
value: new Set([13, 24, 55]),
onChange: (value) => {
// Do something with the selected entities
}
}}
kind={{
value: new Set(['page', 'landing-page']),
options: Set([]),
onChange={() => {}}
}}
>
{(entities, _, search) => (
<>
<SearchControl
onChange={search.onChange}
/>
<ToggleControl
value={entities.value}
onChange={entities.onChange}
/>
</>
)}
</CompositeEntitiesByKind>;
}
```

Obviously depending on what you want to achieve you can use different _Base Components_ or create new ones, as mentioned
above the package comes with a set of _Base Components_ that can be used out of the box.

## About Singular Base Components

The _Composite Component_ always give a collection of Entities and Kind even though you are consuming a Single* _Base Component_.

The Singular Components always get a single value, therefore you have to consider to extract the first element out of the `Set`.

```jsx
import { CompositeEntitiesByKind } from 'wp-entities-search';

export function MyComponent(props) {
return <CompositeEntitiesByKind
/* ... */
>
{(entities, kind, search) => (
<>
<RadioControl
value={kind.value.first()}
options={Array.from(kind.options)}
onChange={kind.onChange}
/>
/* ... */
</>
)}
</CompositeEntitiesByKind>;
}
```
Loading

0 comments on commit f4b551d

Please sign in to comment.