forked from zooniverse/Panoptes-Front-End
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(collections): refactor collection search to match monorepo (z…
…ooniverse#7124) * refactor(collections): refactor collection search to match monorepo Panoptes full-text search requires at least 4 characters in order to return results. If your search string is longer than 4 characters, `collectionSearch` passes it to the Panoptes full-text search API. Otherwise, it fetches as many collections as it can that include the search string in the collection name. * remove unused apiClient * make MIN_SEARCH_LENGTH a configurable parameter * Add some documentation --------- Co-authored-by: Mark Bouslog <[email protected]>
- Loading branch information
1 parent
a685eeb
commit c0e17f9
Showing
2 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import apiClient from 'panoptes-client/lib/api-client'; | ||
|
||
// Tune this value to determine when a search term is long enough to use Panoptes full-text search. | ||
const MIN_SEARCH_LENGTH = 3; | ||
|
||
/** | ||
* Query the Panoptes collections search API | ||
* @param {string} text - The search term. | ||
* @returns {Promise<Array>} - A promise that resolves to an array of collections. | ||
*/ | ||
function panoptesCollectionsSearch(text) { | ||
const query = { | ||
page_size: 100, | ||
favorite: false, | ||
current_user_roles: 'owner,collaborator,contributor' | ||
}; | ||
if (text) { | ||
query.search = text; | ||
} | ||
return apiClient.type('collections').get(query); | ||
} | ||
|
||
/** | ||
* Search Panoptes collections by name. Use full-text search for long search terms. | ||
* Otherwise, request all collections and filter by name. | ||
* @param {string} text - The search term. | ||
* @returns {Promise<Array>} - A promise that resolves to an array of collections. | ||
*/ | ||
export async function collectionsSearch(text = '') { | ||
const search = text.trim().toLowerCase(); | ||
// Search terms of more than MIN_SEARCH_LENGTH characters can use Panoptes full-text search. | ||
if (search.length > MIN_SEARCH_LENGTH) { | ||
return panoptesCollectionsSearch(search); | ||
} | ||
// Otherwise, filter all your collection names by the search term. | ||
const allCollections = await panoptesCollectionsSearch(); | ||
return allCollections.filter( | ||
collection => collection.display_name.toLowerCase().includes(search) | ||
); | ||
} |