Skip to content

Commit

Permalink
Merge 98e8a26 into 91fbf9a
Browse files Browse the repository at this point in the history
  • Loading branch information
ntarocco committed Nov 1, 2020
2 parents 91fbf9a + 98e8a26 commit 83b6fbf
Show file tree
Hide file tree
Showing 17 changed files with 705 additions and 173 deletions.
20 changes: 18 additions & 2 deletions docs/docs/components/react_search_kit.md
Expand Up @@ -38,14 +38,30 @@ See the [complete guide](main_concepts.md) for detailed information.

- **customHandler** `object`: override entirely the default class `UrlHandlerApi`.

- **searchOnInit** `object` _optional_
- **searchOnInit** `boolean` _optional_

A boolean to perform a search when the application is mounted. Default `true`.

- **appName** `string` _optional_

A name identifier to distinguish uniquely the application. Default `RSK`.
A name identifier to distinguish uniquely the application. Useful if multiple ReactSearchKit apps are loaded in the same page. 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`.

- **initialQueryState** `object` _optional_

Set the initial state of your ReactSearchKit application. It will be used to render each component with these default selected values and to perform the first search query, if `searchOnInit` is set to `true`.
The object keys must match the query state fields and the values must be valid values that correspond to the values passed to each parameters.

- **defaultSortingOnEmptyQueryString** `object` _optional_

It is sometimes useful to automatically change the default sorting in case the query string is set or empty.
A typical case is when your app should return by default the most recent items when the user did not search for anything in particular, but it should instead return the best matching items when searching with a particular query string.
You can enabled this behavior by defining in the `initialQueryState` the expected sorting when the query string is set by the user, and an alternative sorting when empty.

- **sortBy** `string`: the query state `sortBy` value to use on empty query string.

- **sortOrder** `string`: the query state `sortOrder` value to use on empty query string.

27 changes: 18 additions & 9 deletions src/demos/cern-videos/App.js
Expand Up @@ -6,29 +6,29 @@
* under the terms of the MIT License; see LICENSE file for more details.
*/

import _truncate from 'lodash/truncate';
import React, { Component } from 'react';
import { OverridableContext } from 'react-overridable';
import {
Container,
Grid,
Accordion,
Menu,
Card,
Container,
Grid,
Image,
Item,
Menu,
} from 'semantic-ui-react';
import _truncate from 'lodash/truncate';
import { OverridableContext } from 'react-overridable';
import { InvenioSearchApi } from '../../lib/api/contrib/invenio';
import {
ReactSearchKit,
SearchBar,
BucketAggregation,
EmptyResults,
Error,
ReactSearchKit,
ResultsLoader,
SearchBar,
withState,
} from '../../lib/components';
import { Results } from './Results';
import { InvenioSearchApi } from '../../lib/api/contrib/invenio';

const OnResults = withState(Results);

Expand All @@ -43,6 +43,11 @@ const sortValues = [
sortBy: 'oldest',
sortOrder: 'asc',
},
{
text: 'Best match',
sortBy: 'bestmatch',
sortOrder: 'asc',
},
];

const resultsPerPageValues = [
Expand All @@ -57,7 +62,7 @@ const resultsPerPageValues = [
];

const initialState = {
sortBy: 'mostrecent',
sortBy: 'bestmatch',
sortOrder: 'asc',
layout: 'list',
page: 1,
Expand Down Expand Up @@ -157,6 +162,10 @@ export class App extends Component {
searchApi={searchApi}
initialQueryState={initialState}
urlHandlerApi={{ enabled: false }}
defaultSortingOnEmptyQueryString={{
sortBy: 'mostrecent',
sortOrder: 'asc',
}}
>
<Container>
<Grid>
Expand Down
33 changes: 22 additions & 11 deletions src/demos/zenodo/App.js
Expand Up @@ -6,30 +6,29 @@
* under the terms of the MIT License; see LICENSE file for more details.
*/

import _truncate from 'lodash/truncate';
import React, { Component } from 'react';
import { OverridableContext } from 'react-overridable';
import {
Container,
Grid,
Accordion,
Menu,
Card,
Container,
Grid,
Image,
Item,
Menu,
} from 'semantic-ui-react';
import _truncate from 'lodash/truncate';
import { OverridableContext } from 'react-overridable';

import { InvenioSearchApi } from '../../lib/api/contrib/invenio';
import {
ReactSearchKit,
SearchBar,
BucketAggregation,
EmptyResults,
Error,
ReactSearchKit,
ResultsLoader,
SearchBar,
withState,
} from '../../lib/components';
import { Results } from './Results';
import { InvenioSearchApi } from '../../lib/api/contrib/invenio';

const OnResults = withState(Results);

Expand All @@ -44,6 +43,11 @@ const sortValues = [
sortBy: 'mostviewed',
sortOrder: 'desc',
},
{
text: 'Best match',
sortBy: 'bestmatch',
sortOrder: 'asc',
},
{
text: 'Newest',
sortBy: 'mostrecent',
Expand Down Expand Up @@ -80,7 +84,7 @@ const searchApi = new InvenioSearchApi({
});

const initialState = {
sortBy: 'mostrecent',
sortBy: 'bestmatch',
sortOrder: 'asc',
layout: 'list',
page: 1,
Expand Down Expand Up @@ -168,7 +172,14 @@ export class App extends Component {
render() {
return (
<OverridableContext.Provider value={overriddenComponents}>
<ReactSearchKit searchApi={searchApi} initialQueryState={initialState}>
<ReactSearchKit
searchApi={searchApi}
initialQueryState={initialState}
defaultSortingOnEmptyQueryString={{
sortBy: 'mostrecent',
sortOrder: 'asc',
}}
>
<Container>
<Grid>
<Grid.Row>
Expand Down
8 changes: 4 additions & 4 deletions src/lib/api/contrib/invenio/InvenioSearchApi.js
Expand Up @@ -6,14 +6,14 @@
* under the terms of the MIT License; see LICENSE file for more details.
*/

import axios from 'axios';
import _get from 'lodash/get';
import _hasIn from 'lodash/hasIn';
import _isEmpty from 'lodash/isEmpty';
import axios from 'axios';
import { updateQueryState } from '../../../state/selectors';
import { INITIAL_QUERY_STATE } from '../../../storeConfig';
import { InvenioRequestSerializer } from './InvenioRequestSerializer';
import { InvenioResponseSerializer } from './InvenioResponseSerializer';
import { updateQueryState } from '../../../state/selectors';
import { STORE_KEYS } from '../../../storeConfig';

export class InvenioSearchApi {
constructor(config) {
Expand Down Expand Up @@ -88,7 +88,7 @@ export class InvenioSearchApi {
const newQueryState = updateQueryState(
stateQuery,
response.extras,
STORE_KEYS
INITIAL_QUERY_STATE
);
if (!_isEmpty(newQueryState)) {
response.newQueryState = newQueryState;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/Bootstrap/Bootstrap.js
@@ -1,13 +1,13 @@
/*
* This file is part of React-SearchKit.
* Copyright (C) 2018-2019 CERN.
* Copyright (C) 2018-2020 CERN.
*
* React-SearchKit is free software; you can redistribute it and/or modify it
* under the terms of the MIT License; see LICENSE file for more details.
*/

import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import React, { Component, Fragment } from 'react';
import Overridable from 'react-overridable';

class Bootstrap extends Component {
Expand All @@ -28,7 +28,7 @@ class Bootstrap extends Component {
this.updateQueryState(payload.searchQuery);
} else {
console.debug(
`RSK app ${this.appName}: ignore event sent for app ${appReceiverName}...`
`RSK app '${this.appName}': ignoring event sent for app '${appReceiverName}'.`
);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/Bootstrap/Bootstrap.test.js
Expand Up @@ -6,10 +6,10 @@
* under the terms of the MIT License; see LICENSE file for more details.
*/

import React from 'react';
import { mount } from 'enzyme';
import { default as Bootstrap } from './Bootstrap';
import React from 'react';
import { onQueryChanged } from '../../events';
import { default as Bootstrap } from './Bootstrap';

describe('test Bootstrap component', () => {
it('should update query state when event listener is registered', () => {
Expand Down
Expand Up @@ -6,12 +6,11 @@
* under the terms of the MIT License; see LICENSE file for more details.
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Checkbox, List } from 'semantic-ui-react';
import React, { Component } from 'react';
import Overridable from 'react-overridable';
import { Checkbox, List } from 'semantic-ui-react';
import { buildUID } from '../../util';
import _get from 'lodash/get';

class BucketAggregationValues extends Component {
constructor(props) {
Expand Down Expand Up @@ -75,7 +74,6 @@ class BucketAggregationValues extends Component {
};
const getChildAggCmps = (bucket) =>
this.getChildAggCmps(bucket, selectedFilters);
let label = null;

return (
<ValueElement
Expand Down
18 changes: 12 additions & 6 deletions src/lib/components/ReactSearchKit/ReactSearchKit.js
@@ -1,19 +1,19 @@
/*
* This file is part of React-SearchKit.
* Copyright (C) 2018-2019 CERN.
* Copyright (C) 2018-2020 CERN.
*
* React-SearchKit is free software; you can redistribute it and/or modify it
* under the terms of the MIT License; see LICENSE file for more details.
*/

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Overridable from 'react-overridable';
import { configureStore } from '../../store';
import { Provider } from 'react-redux';
import { UrlHandlerApi } from '../../api';
import { Bootstrap } from '../Bootstrap';
import { createStoreWithConfig } from '../../store';
import { buildUID } from '../../util';
import { Bootstrap } from '../Bootstrap';

export class ReactSearchKit extends Component {
constructor(props) {
Expand All @@ -28,9 +28,10 @@ export class ReactSearchKit extends Component {
: null,
searchOnInit: props.searchOnInit,
initialQueryState: props.initialQueryState,
defaultSortingOnEmptyQueryString: props.defaultSortingOnEmptyQueryString,
};

this.store = configureStore(appConfig);
this.store = createStoreWithConfig(appConfig);
this.appName = props.appName;
this.eventListenerEnabled = props.eventListenerEnabled;
}
Expand Down Expand Up @@ -83,6 +84,10 @@ ReactSearchKit.propTypes = {
hiddenParams: PropTypes.array,
layout: PropTypes.oneOf(['list', 'grid']),
}),
defaultSortingOnEmptyQueryString: PropTypes.shape({
sortBy: PropTypes.string,
sortOrder: PropTypes.string,
}),
};

ReactSearchKit.defaultProps = {
Expand All @@ -97,6 +102,7 @@ ReactSearchKit.defaultProps = {
eventListenerEnabled: false,
overridableId: '',
initialQueryState: {},
defaultSortingOnEmptyQueryString: {},
};

export default Overridable.component('ReactSearchKit', ReactSearchKit);

0 comments on commit 83b6fbf

Please sign in to comment.