Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
chore(project): fix processSearchResults function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Apr 30, 2021
1 parent 7b64b3c commit 61895f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Example extends Component {
To start the example demo, run

```bash
npm run start:example
yarn start:example
```

And visit http://localhost:5050 in a browser
Expand Down
12 changes: 5 additions & 7 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PostprocessCustomType {

static searchAllQuery = gql`
query($query: String!) {
ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
allResults: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
identifier
name
itemListElement(filter:{name_regexp:$query}) {
Expand All @@ -65,10 +65,10 @@ class PostprocessCustomType {

static searchQuery = gql`
query($filter: _ThingInterfaceFilter) {
ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
results: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
identifier
name
itemListElement(filter: $filter) {
itemListElement(filter: $filter, first: 50) {
identifier
name
}
Expand All @@ -78,10 +78,8 @@ class PostprocessCustomType {

static processSearchResult = result => {
// Find the itemListElements of this ItemList, instead of a list of ItemLists
if (result) {
if (result.data.ItemList) {
return result.data.ItemList[0].itemListElement;
}
if (Array.isArray(result) && result[0]) {
return result[0].itemListElement;
}

return [];
Expand Down
14 changes: 10 additions & 4 deletions src/search/SearchConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class SearchConfig {
},
});

let processedAllResults;
let processedAllResults = allResults;

if (searchType.processSearchResult) {
if (typeof searchType.processSearchResult === 'function') {
processedAllResults = searchType.processSearchResult(allResults);
}

Expand All @@ -87,11 +87,17 @@ class SearchConfig {
},
});

let processedResults = results;

if (typeof searchType.processSearchResult === 'function') {
processedResults = searchType.processSearchResult(results);
}

return {
typename : searchType.name,
total : searchType.processSearchResult ? searchType.processSearchResult(results).length : results.length,
total : processedResults.length,
allResults: processedAllResults,
results : searchType.processSearchResult ? searchType.processSearchResult(results) : results,
results : processedResults,
};
}

Expand Down

0 comments on commit 61895f0

Please sign in to comment.