Skip to content

Commit

Permalink
STSMACOM-787 Export new advancedSearchQueryToRows helper to be used…
Browse files Browse the repository at this point in the history
… in Inventory app to reduce code duplication (#1409)
  • Loading branch information
BogdanDenis authored Oct 31, 2023
1 parent 2641cd5 commit dbb6c43
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 9.1.0 IN PROGRESS

* Added `inputType` prop to `<SearchAndSort>` component to support both input and textarea search boxes. Refs STSMACOM-786.
* Export new `advancedSearchQueryToRows` helper to be used in Inventory app to reduce code duplication. Refs STSMACOM-787.

## [9.0.0](https://github.com/folio-org/stripes-smart-components/tree/v9.0.0) (2023-10-11)
[Full Changelog](https://github.com/folio-org/stripes-smart-components/compare/v8.0.0...v9.0.0)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export { default as MultiSelectionFilter } from './lib/SearchAndSort/components/
export { default as DateRangeFilter } from './lib/SearchAndSort/components/DateRangeFilter';

export { default as makeQueryFunction } from './lib/SearchAndSort/makeQueryFunction';
export { default as advancedSearchQueryToRows } from './lib/SearchAndSort/advancedSearchQueryToRows';

export { default as makeConnectedSource } from './lib/SearchAndSort/ConnectedSource';
export { default as StripesConnectedSource } from './lib/SearchAndSort/ConnectedSource/StripesConnectedSource';
Expand Down
50 changes: 8 additions & 42 deletions lib/SearchAndSort/SearchAndSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ import {
} from './nsQueryFunctions';
import makeConnectedSource from './ConnectedSource';
import Tags from '../Tags';
import { NoResultsMessage, ResetButton, CollapseFilterPaneButton, ExpandFilterPaneButton } from './components';
import {
NoResultsMessage,
ResetButton,
CollapseFilterPaneButton,
ExpandFilterPaneButton,
} from './components';
import { ColumnManager } from '../ColumnManager';
import PersistedPaneset from '../PersistedPaneset';

import buildUrl from './buildUrl';
import advancedSearchQueryToRows from './advancedSearchQueryToRows';

import css from './SearchAndSort.css';

Expand Down Expand Up @@ -925,7 +931,7 @@ class SearchAndSort extends React.Component {
onSearch={this.handleAdvancedSearch}
onCancel={() => this.setState({ isAdvancedSearchOpen: false })}
queryBuilder={advancedSearchQueryBuilder}
queryToRow={this.advancedSearchQueryToRow}
queryToRow={(row) => advancedSearchQueryToRows(row.query)}
hasQueryOption={false}
>
{({ resetRows }) => (
Expand Down Expand Up @@ -1167,46 +1173,6 @@ class SearchAndSort extends React.Component {
});
};

advancedSearchQueryToRow = (row) => {
const formattedQuery = row.query;

if (!formattedQuery) {
return [];
}

const splitIntoRowsRegex = /(?=\sor\s|\sand\s|\snot\s)/g;

// split will return array of strings:
// ['keyword==test', 'or issn=123', ...]
const matches = formattedQuery.split(splitIntoRowsRegex).map(i => i.trim());

return matches.map((match, index) => {
let bool = '';
let query = match;

// first row doesn't have a bool operator
if (index !== 0) {
bool = match.substr(0, match.indexOf(' '));
query = match.substr(bool.length);
}

const splitIndexAndQueryRegex = /([^=]+)(exactPhrase|containsAll|startsWith)(.+)/g;

const rowParts = [...query.matchAll(splitIndexAndQueryRegex)]?.[0] || [];
// eslint-disable-next-line no-unused-vars
const [, option, _match, value] = rowParts
.map(i => i.trim())
.map(i => i.replaceAll('"', ''));

return {
query: value,
bool,
searchOption: option,
match: _match,
};
});
};

renderSearch(source) {
const {
objectName,
Expand Down
39 changes: 39 additions & 0 deletions lib/SearchAndSort/advancedSearchQueryToRows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const advancedSearchQueryToRows = (queryValue) => {
if (!queryValue) {
return [];
}

const splitIntoRowsRegex = /(?=\sor\s|\sand\s|\snot\s)/g;

// split will return array of strings:
// ['keyword==test', 'or issn=123', ...]
const matches = queryValue.split(splitIntoRowsRegex).map(i => i.trim());

return matches.map((match, index) => {
let bool = '';
let query = match;

// first row doesn't have a bool operator
if (index !== 0) {
bool = match.substr(0, match.indexOf(' '));
query = match.substr(bool.length);
}

const splitIndexAndQueryRegex = /([^=]+)(exactPhrase|containsAll|startsWith|containsAny)(.+)/g;

const rowParts = [...query.matchAll(splitIndexAndQueryRegex)]?.[0] || [];
// eslint-disable-next-line no-unused-vars
const [, option, _match, value] = rowParts
.map(i => i.trim())
.map(i => i.replaceAll('"', ''));

return {
query: value,
bool,
searchOption: option,
match: _match,
};
});
};

export default advancedSearchQueryToRows;
1 change: 1 addition & 0 deletions lib/SearchAndSort/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default } from './SearchAndSort';
export { default as makeQueryFunction } from './makeQueryFunction';
export { default as SearchAndSortQuery } from './SearchAndSortQuery';
export { default as advancedSearchQueryToRows } from './advancedSearchQueryToRows';

0 comments on commit dbb6c43

Please sign in to comment.