Skip to content

Commit

Permalink
refactor(collections): refactor collection search to match monorepo (z…
Browse files Browse the repository at this point in the history
…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
eatyourgreens and mcbouslog authored Aug 8, 2024
1 parent a685eeb commit c0e17f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
10 changes: 2 additions & 8 deletions app/collections/collection-search.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ React = require 'react'
PropTypes = require 'prop-types'
createReactClass = require 'create-react-class'
Select = require('react-select').default
apiClient = require 'panoptes-client/lib/api-client'
{ collectionsSearch } = require('./searchCollections')

module.exports = createReactClass
displayName: 'CollectionSearch'
Expand Down Expand Up @@ -30,13 +30,7 @@ module.exports = createReactClass
)

searchCollections: (value) ->
query =
page_size: 100
favorite: false
current_user_roles: 'owner,collaborator,contributor'
query.search = "#{value}" unless value is ''

apiClient.type('collections').get query
collectionsSearch(value)
.then (collections) =>
options = collections.map (collection) =>
{
Expand Down
40 changes: 40 additions & 0 deletions app/collections/searchCollections.js
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)
);
}

0 comments on commit c0e17f9

Please sign in to comment.