Skip to content

Commit

Permalink
global: add external event listener
Browse files Browse the repository at this point in the history
* Adds an event listener so a search can be triggered externally.
* Removes `historyListen` prop which is redundant now.
  • Loading branch information
zzacharo committed Feb 19, 2020
1 parent 001592c commit 32cb8b5
Show file tree
Hide file tree
Showing 21 changed files with 2,010 additions and 1,944 deletions.
7 changes: 5 additions & 2 deletions docs/docs/components/react_search_kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ See the [complete guide](main_concepts.md) for detailed information.

A boolean to define a default `sort by` value when the query string is empty. This is useful when the results sorting should be different when the user inserts a query string or not (e.g. `most recent` or `most relevant` first).

* **history** `object` *optional*
* **appName** `string` *optional*

The [React Router history](https://github.com/ReactTraining/history) object to add a new listener to the browser history to perform searches on URL parameters changed.
A name identifier to distinguish uniquely the application. Default `RSK`.

* **eventListenerEnabled** `boolean` *optional*

If `true` the application listens to the `queryChanged` event else if `false` no listener is registered. When this event is emitted the application triggers a search based on the payload that is passed to the event at the emission time. Default `false`.
60 changes: 60 additions & 0 deletions docs/docs/main_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,66 @@ The concept around `SearchAPI` and `UrlHandlerApi` will be explained more in det

---

---

## Events

You can control the app state externally using available events in `React-SearchKit`:

* `queryChanged`: `React-SearchKit` listens to this event and updates the `query` state based on the payload of the event. `React-SearchKit` will ignore any field that is passed and is not part of the store's keys.

> Note: By default `React-SearchKit` is not registering any event. To enable this behaviour you need to pass in the root component the below variable:
```jsx
class MyReactSearchKit extends Component {
render() {
return (
<ReactSearchKit {...this.props} eventListenerEnabled={true}>
{this.props.children}
</ReactSearchKit>
);
}
}
```

In order to trigger the `queryChanged` event you can use the `onQueryChanged` emitter function that is part the `React-SearchKit` library. For example:


```jsx
import { onQueryChanged } from 'react-searchkit';

class MyExternalApp extends Component {
render() {
return (
<Button onClick={() => onQueryChanged({queryString: 'search'})}>Trigger Search</Button>
);
}
}

class MyReactSearchKit extends Component {
render() {
return (
<ReactSearchKit {...requiredProps} searchOnInit={false} eventListenerEnabled={true}>
{this.props.children}
</ReactSearchKit>
);
}
}

class MyApp extends Component {
render() {
return (
<>
<MyExternalApp />
<MyReactSearchKit />
</>
)
}
}
```

---

## TL;DR

* UI Components change the `query` state through Redux actions in response to user inputs. They receive the updated `results` state after a REST APIs search through props injected automatically by Redux.
Expand Down
36 changes: 0 additions & 36 deletions docs/docs/using_url_parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,6 @@ const myUrlParamsHandler = new MyUrlParamsHandler();

---

## React Router history integration

React-SearchKit can integrate with [React Router history](https://github.com/ReactTraining/history) library. This is useful when any React component in your app (external to React-SearchKit) changes URL search parameters and you expect that a search is triggered.

For example, if a component changes the query parameters to `?q=react`, then a new search with the query string value `react` should be triggered.

To achieve this, you can inject the `history` object to the main `<ReactSearchKit>` component. A new listener will be automatically added and from now on any change to the URL parameters will trigger a search.

> React-SearchKit will **not** listen to changes to `window.location` object. You will have to use `React Router history` in your app and inject it in React-SearchKit.
```jsx
import React, { Component } from 'react';
import { createBrowserHistory } from 'history';
import { ReactSearchKit, InvenioSearchApi } from 'react-searchkit';

const history = createBrowserHistory();

const searchApi = new InvenioSearchApi({
url: 'https://zenodo.org/api/records/',
timeout: 5000,
headers: { Accept: 'application/vnd.zenodo.v1+json' },
});

class App extends Component {
render() {
return (
<ReactSearchKit searchApi={searchApi} history={history}>
<h1>My search UI</h1>
</ReactSearchKit>
);
}
}
```

---

## TL;DR

* You can enabled or disable deep linking with a flag
Expand Down
Loading

0 comments on commit 32cb8b5

Please sign in to comment.