From 56ee0a472ab8c77df51f6bb19d616d5f81ffde96 Mon Sep 17 00:00:00 2001 From: Daniel Kos Date: Tue, 27 Nov 2018 09:37:19 +0100 Subject: [PATCH] Fix various search issues Feature #14280 Arvados-DCO-1.1-Signed-off-by: Daniel Kos --- src/store/search-bar/search-bar-actions.ts | 30 +++++++++++++------ src/store/search-bar/search-bar-reducer.ts | 3 +- .../search-bar/search-bar-view.tsx | 30 +++++++++++++++++-- .../search-bar/search-bar.tsx | 5 ++-- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/store/search-bar/search-bar-actions.ts b/src/store/search-bar/search-bar-actions.ts index 2ad9b283..2bac2451 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -80,13 +80,20 @@ export const searchAdvanceData = (data: SearchBarAdvanceFormData) => export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => (dispatch: Dispatch, getState: () => RootState) => { + const searchValue = getState().searchBar.searchValue; const value = getQueryFromAdvancedData({ - searchValue: getState().searchBar.searchValue, - ...data + ...data, + searchValue }, prevData); dispatch(searchBarActions.SET_SEARCH_VALUE(value)); }; +export const setAdvancedDataFromSearchValue = (search: string) => + (dispatch: Dispatch) => { + const data = getAdvancedDataFromQuery(search); + dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); + }; + const saveQuery = (data: SearchBarAdvanceFormData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const savedQueries = services.searchService.getSavedQueries(); @@ -155,7 +162,7 @@ export const changeData = (searchValue: string) => const searchValuePresent = searchValue.length > 0; if (currentView === SearchView.ADVANCED) { - + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE)); } else if (searchValuePresent) { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE)); dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue)); @@ -205,8 +212,19 @@ const searchGroups = (searchValue: string, limit: number) => const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | 'reuse') => { let value = data.searchValue; + const rem = (field: string, fieldValue: any, value: string) => { + }; + const addRem = (field: string, key: string) => { const v = data[key]; + + if (data.hasOwnProperty(key)) { + const pattern = v === false + ? `${field.replace(':', '\\:\\s*')}\\s*` + : `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`; + value = value.replace(new RegExp(pattern), ''); + } + if (v) { const nv = v === true ? `${field}` @@ -217,11 +235,6 @@ const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | ' } else { value = nv + ' ' + value; } - } else if (data.hasOwnProperty(key) && (v === undefined || v === false)) { - const pattern = v === false - ? `${field.replace(':', '\\:\\s*')}\\s*` - : `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*`; - value = value.replace(new RegExp(pattern), ''); } }; @@ -260,7 +273,6 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevDat if (prevData) { const obj = getModifiedKeysValues(flatData(data), flatData(prevData)); - console.log(obj); value = buildQueryFromKeyMap({ searchValue: data.searchValue, ...obj diff --git a/src/store/search-bar/search-bar-reducer.ts b/src/store/search-bar/search-bar-reducer.ts index 46477c1c..4f663eeb 100644 --- a/src/store/search-bar/search-bar-reducer.ts +++ b/src/store/search-bar/search-bar-reducer.ts @@ -5,8 +5,7 @@ import { getQueryFromAdvancedData, searchBarActions, - SearchBarActions, - setSearchValueFromAdvancedData + SearchBarActions } from '~/store/search-bar/search-bar-actions'; import { GroupContentsResource } from '~/services/groups-service/groups-service'; import { SearchBarAdvanceFormData } from '~/models/search-bar'; diff --git a/src/views-components/search-bar/search-bar-view.tsx b/src/views-components/search-bar/search-bar-view.tsx index ec1c8b83..beaad493 100644 --- a/src/views-components/search-bar/search-bar-view.tsx +++ b/src/views-components/search-bar/search-bar-view.tsx @@ -86,6 +86,7 @@ interface SearchBarViewActionProps { loadRecentQueries: () => string[]; moveUp: () => void; moveDown: () => void; + setAdvancedDataFromSearchValue: (search: string) => void; } type SearchBarViewProps = SearchBarDataProps & SearchBarActionProps & WithStyles; @@ -94,6 +95,7 @@ const handleKeyDown = (e: React.KeyboardEvent, props: SearchBarViewProps) => { if (e.keyCode === KEY_CODE_DOWN) { e.preventDefault(); if (!props.isPopoverOpen) { + props.onSetView(SearchView.AUTOCOMPLETE); props.openSearchView(); } else { props.moveDown(); @@ -117,6 +119,30 @@ const handleKeyDown = (e: React.KeyboardEvent, props: SearchBarViewProps) => { } }; +const handleInputClick = (e: React.MouseEvent, props: SearchBarViewProps) => { + if (props.searchValue) { + props.onSetView(SearchView.AUTOCOMPLETE); + props.openSearchView(); + } else { + props.closeView(); + } +}; + +const handleDropdownClick = (e: React.MouseEvent, props: SearchBarViewProps) => { + e.stopPropagation(); + if (props.isPopoverOpen) { + if (props.currentView === SearchView.ADVANCED) { + props.closeView(); + } else { + props.setAdvancedDataFromSearchValue(props.searchValue); + props.onSetView(SearchView.ADVANCED); + } + } else { + props.setAdvancedDataFromSearchValue(props.searchValue); + props.onSetView(SearchView.ADVANCED); + } +}; + export const SearchBarView = withStyles(styles)( (props : SearchBarViewProps) => { const { classes, isPopoverOpen } = props; @@ -131,7 +157,7 @@ export const SearchBarView = withStyles(styles)( value={props.searchValue} fullWidth={true} disableUnderline={true} - onClick={props.openSearchView} + onClick={e => handleInputClick(e, props)} onKeyDown={e => handleKeyDown(e, props)} startAdornment={ @@ -145,7 +171,7 @@ export const SearchBarView = withStyles(styles)( endAdornment={ - props.onSetView(SearchView.ADVANCED)}> + handleDropdownClick(e, props)}> diff --git a/src/views-components/search-bar/search-bar.tsx b/src/views-components/search-bar/search-bar.tsx index e60b2141..3107ad73 100644 --- a/src/views-components/search-bar/search-bar.tsx +++ b/src/views-components/search-bar/search-bar.tsx @@ -16,7 +16,7 @@ import { navigateToItem, editSavedQuery, changeData, - submitData, moveUp, moveDown + submitData, moveUp, moveDown, setAdvancedDataFromSearchValue } from '~/store/search-bar/search-bar-actions'; import { SearchBarView, SearchBarActionProps, SearchBarDataProps } from '~/views-components/search-bar/search-bar-view'; import { SearchBarAdvanceFormData } from '~/models/search-bar'; @@ -46,7 +46,8 @@ const mapDispatchToProps = (dispatch: Dispatch): SearchBarActionProps => ({ navigateTo: (uuid: string) => dispatch(navigateToItem(uuid)), editSavedQuery: (data: SearchBarAdvanceFormData) => dispatch(editSavedQuery(data)), moveUp: () => dispatch(moveUp()), - moveDown: () => dispatch(moveDown()) + moveDown: () => dispatch(moveDown()), + setAdvancedDataFromSearchValue: (search: string) => dispatch(setAdvancedDataFromSearchValue(search)) }); export const SearchBar = connect(mapStateToProps, mapDispatchToProps)(SearchBarView); -- 2.30.2