diff --git a/src/Search/searchActions.js b/src/Search/searchActions.js index d5a655ee..e1ba730c 100644 --- a/src/Search/searchActions.js +++ b/src/Search/searchActions.js @@ -108,6 +108,26 @@ function handleSearch(params, inputValue, scope) { p_value: { min: '', max: '' }, }; } + // insert 'is_meta" field to fields array if ktype is 'metab' + // and delete 'protein_name' field if it exists + if (params.ktype === 'metab') { + if (!params.fields.includes('is_meta')) { + params.fields = ['is_meta', ...params.fields]; + } + if (params.fields.includes('protein_name')) { + const index = params.fields.indexOf('protein_name'); + params.fields.splice(index, 1); + } + } + // delete 'is_meta' flag from fields array (if it exists) + // if ktype is 'protein' or 'gene' + if (params.ktype === 'protein' || params.ktype === 'gene') { + if (params.fields.includes('is_meta')) { + const index = params.fields.indexOf('is_meta'); + params.fields.splice(index, 1); + } + } + return (dispatch) => { dispatch(searchSubmit(params, scope)); return axios diff --git a/src/Search/searchPage.jsx b/src/Search/searchPage.jsx index eef719ab..b3c6592b 100644 --- a/src/Search/searchPage.jsx +++ b/src/Search/searchPage.jsx @@ -20,6 +20,7 @@ import IconSet from '../lib/iconSet'; import { trackEvent } from '../GoogleAnalytics/googleAnalytics'; import { genes } from '../data/genes'; import { metabolites } from '../data/metabolites'; +import { proteins } from '../data/proteins'; import searchStructuredData from '../lib/searchStructuredData/search'; import UserSurveyModal from '../UserSurvey/userSurveyModal'; @@ -110,50 +111,61 @@ export function SearchPage({ return null; } + // get options based on selected search context + function getOptions() { + switch (searchParams.ktype) { + case 'gene': + return genes; + case 'metab': + return metabolites; + case 'protein': + return proteins; + default: + return []; + } + } + // render placeholder text in primary search input field function renderPlaceholder() { if (searchParams.ktype === 'protein') { - return 'Example: NP_001000006.1, NP_001001508.2, NP_001005898.3'; + return 'Example: "atpase inhibitor, mitochondrial", "global ischemia-induced protein 11"'; } if (searchParams.ktype === 'metab') { - return 'Example: 8,9-EpETrE, C18:1 LPC plasmalogen B'; + return 'Example: "amino acids and peptides", "c10:2 carnitine"'; } - return 'Example: BRD2, SMAD3, ID1'; + return 'Example: brd2, smad3, vegfa'; } const inputEl = document.querySelector('.rbt-input-main'); - // FIXME: transform react-bootstrap-typeahead state from array to string + // Transform input values + // Keep react-bootstrap-typeahead state array as is + // Convert manually entered gene/protein/metabolite string input to array function formatSearchInput() { const newArr = []; + // react-bootstrap-typeahead state array has values if (multiSelections.length) { multiSelections.forEach((item) => newArr.push(item.id)); - return newArr.join(', '); + return newArr; } - // Handle manually entered gene/metabolite input + // Handle manually entered gene/protein/metabolite string input + // convert formatted string to array if (inputEl.value && inputEl.value.length) { - const str = inputEl.value; - if (searchParams.ktype === 'gene') { - const arr = str.split(',').map((s) => s.trim()); - return arr.join(', '); - } - return str; + const inputStr = inputEl.value; + // Match terms enclosed in double quotes or not containing commas + const terms = inputStr.match(/("[^"]+"|[^, ]+)/g); + // Remove double quotes from terms that are enclosed and trim any extra spaces + return terms.map((term) => term.replace(/"/g, '').trim()); } - return ''; + return newArr; } // Clear manually entered gene/protein/metabolite input - function clearGeneInput(ktype) { - const inputElProtein = document.querySelector('.search-input-kype'); - - if (ktype && ktype === 'protein') { - if (inputElProtein && inputElProtein.value && inputElProtein.value.length) { - inputElProtein.value = ''; - } - } else if (inputEl && inputEl.value && inputEl.value.length) { + const clearSearchTermInput = () => { + if (inputEl && inputEl.value && inputEl.value.length) { inputRef.current.clear(); } - } + }; return (