Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(collections): refactor collection search to match monorepo #7124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increasing this value might be a workaround for weird results when the search is 4 or 5 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating to add that this breaks collections search for Gravity Spy, where there are 2-character collection names.


/**
* 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)
);
}
Loading