X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/34399d176adae18bd42d70129c0a51f740afeb8e..6faff5910b3e7bdaf47ab5ede7cf8094a1b4adb9:/src/store/search-bar/search-bar-actions.ts diff --git a/src/store/search-bar/search-bar-actions.ts b/src/store/search-bar/search-bar-actions.ts index c4fb727d..199ec3f9 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -2,23 +2,24 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from "~/common/unionize"; +import { ofType, unionize, UnionOf } from "~/common/unionize"; import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service'; import { Dispatch } from 'redux'; -import { change, arrayPush } from 'redux-form'; +import { arrayPush, change, initialize } from 'redux-form'; import { RootState } from '~/store/store'; -import { initUserProject } from '~/store/tree-picker/tree-picker-actions'; +import { initUserProject, treePickerActions } from '~/store/tree-picker/tree-picker-actions'; import { ServiceRepository } from '~/services/services'; import { FilterBuilder } from "~/services/api/filter-builder"; import { ResourceKind } from '~/models/resource'; import { GroupClass } from '~/models/group'; import { SearchView } from '~/store/search-bar/search-bar-reducer'; -import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action'; +import { navigateTo, navigateToSearchResults } from '~/store/navigation/navigation-action'; import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions'; -import { initialize } from 'redux-form'; -import { SearchBarAdvanceFormData, PropertyValues, ClusterObjectType } from '~/models/search-bar'; +import { ClusterObjectType, PropertyValue, SearchBarAdvanceFormData } from '~/models/search-bar'; import { debounce } from 'debounce'; import * as _ from "lodash"; +import { getModifiedKeysValues } from "~/common/objects"; +import { activateSearchBarProject } from "~/store/search-bar/search-bar-tree-actions"; export const searchBarActions = unionize({ SET_CURRENT_VIEW: ofType(), @@ -78,36 +79,32 @@ export const searchAdvanceData = (data: SearchBarAdvanceFormData) => dispatch(navigateToSearchResults); }; -export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, prevData: SearchBarAdvanceFormData) => - (dispatch: Dispatch) => { - let value = ''; - if (data.type) { - value += ` type:${data.type}`; - } - if (data.cluster) { - value += ` cluster:${data.cluster}`; - } +export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => + (dispatch: Dispatch, getState: () => RootState) => { + const searchValue = getState().searchBar.searchValue; + const value = getQueryFromAdvancedData({ + ...data, + searchValue + }, prevData); + dispatch(searchBarActions.SET_SEARCH_VALUE(value)); + }; + +export const setAdvancedDataFromSearchValue = (search: string) => + async (dispatch: Dispatch) => { + const data = getAdvancedDataFromQuery(search); + dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); if (data.projectUuid) { - value += ` project:${data.projectUuid}`; - } - if (data.inTrash) { - value += ` is:trashed`; + await dispatch(activateSearchBarProject(data.projectUuid)); + dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID, id: data.projectUuid })); } - if (data.dateFrom) { - value += ` from:${data.dateFrom}`; - } - if (data.dateTo) { - value += ` from:${data.dateTo}`; - } - value = value.substr(1) + data.searchQuery; - dispatch(searchBarActions.SET_SEARCH_VALUE(value)); }; const saveQuery = (data: SearchBarAdvanceFormData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const savedQueries = services.searchService.getSavedQueries(); - if (data.saveQuery && data.searchQuery) { - const filteredQuery = savedQueries.find(query => query.searchQuery === data.searchQuery); + if (data.saveQuery && data.queryName) { + const filteredQuery = savedQueries.find(query => query.queryName === data.queryName); + data.searchValue = getState().searchBar.searchValue; if (filteredQuery) { services.searchService.editSavedQueries(data); dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries)); @@ -131,7 +128,7 @@ export const deleteSavedQuery = (id: number) => export const editSavedQuery = (data: SearchBarAdvanceFormData) => (dispatch: Dispatch) => { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED)); - dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery)); + dispatch(searchBarActions.SET_SEARCH_VALUE(getQueryFromAdvancedData(data))); dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); }; @@ -153,6 +150,7 @@ export const closeSearchView = () => export const closeAdvanceView = () => (dispatch: Dispatch) => { dispatch(searchBarActions.SET_SEARCH_VALUE('')); + dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID })); dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); }; @@ -170,7 +168,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)); @@ -217,25 +215,109 @@ const searchGroups = (searchValue: string, limit: number) => } }; -export const parseQuery: (searchValue: string) => { hasKeywords: boolean; values: string[]; properties: any } = (searchValue: string) => { +const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | 'reuse') => { + let value = data.searchValue; + + 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}` + : `${field}:${v}`; + + if (mode === 'rebuild') { + value = value + ' ' + nv; + } else { + value = nv + ' ' + value; + } + } + }; + + keyMap.forEach(km => addRem(km[0], km[1])); + + return value; +}; + +export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => { + let value = ''; + + const flatData = (data: SearchBarAdvanceFormData) => { + const fo = { + searchValue: data.searchValue, + type: data.type, + cluster: data.cluster, + projectUuid: data.projectUuid, + inTrash: data.inTrash, + dateFrom: data.dateFrom, + dateTo: data.dateTo, + }; + (data.properties || []).forEach(p => fo[`prop-${p.key}`] = p.value); + return fo; + }; + + const keyMap = [ + ['type', 'type'], + ['cluster', 'cluster'], + ['project', 'projectUuid'], + ['is:trashed', 'inTrash'], + ['from', 'dateFrom'], + ['to', 'dateTo'] + ]; + _.union(data.properties, prevData ? prevData.properties : []) + .forEach(p => keyMap.push([`has:${p.key}`, `prop-${p.key}`])); + + if (prevData) { + const obj = getModifiedKeysValues(flatData(data), flatData(prevData)); + value = buildQueryFromKeyMap({ + searchValue: data.searchValue, + ...obj + } as SearchBarAdvanceFormData, keyMap, "reuse"); + } else { + value = buildQueryFromKeyMap(flatData(data), keyMap, "rebuild"); + } + + value = value.trim(); + return value; +}; + +export interface ParseSearchQuery { + hasKeywords: boolean; + values: string[]; + properties: { + [key: string]: string[] + }; +} + +export const parseSearchQuery: (query: string) => ParseSearchQuery = (searchValue: string) => { const keywords = [ 'type:', 'cluster:', 'project:', 'is:', 'from:', - 'to:' + 'to:', + 'has:' ]; - const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + search.indexOf(keyword) >= 0 ? 1 : 0, 0); + const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + (search.includes(keyword) ? 1 : 0), 0); let keywordsCnt = 0; const properties = {}; keywords.forEach(k => { - const p = searchValue.indexOf(k); - const l = searchValue.length; - if (p >= 0) { + let p = searchValue.indexOf(k); + const key = k.substr(0, k.length - 1); + + while (p >= 0) { + const l = searchValue.length; keywordsCnt += 1; let v = ''; @@ -253,10 +335,14 @@ export const parseQuery: (searchValue: string) => { hasKeywords: boolean; values searchValue = searchValue.substr(0, p) + searchValue.substr(vp); } else { if (v !== '') { - properties[k.substr(0, k.length - 1)] = v; + if (!properties[key]) { + properties[key] = []; + } + properties[key].push(v); } searchValue = searchValue.substr(0, p) + searchValue.substr(i); } + p = searchValue.indexOf(k); } }); @@ -265,38 +351,69 @@ export const parseQuery: (searchValue: string) => { hasKeywords: boolean; values return { hasKeywords: keywordsCnt > 0, values, properties }; }; +const getFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0]; +const getPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value); +const getProperties = (sq: ParseSearchQuery): PropertyValue[] => { + if (sq.properties.has) { + return sq.properties.has.map((value: string) => { + const v = value.split(':'); + return { + key: v[0], + value: v[1] + }; + }); + } + return []; +}; + +export const getAdvancedDataFromQuery = (query: string): SearchBarAdvanceFormData => { + const sq = parseSearchQuery(query); + + return { + searchValue: sq.values.join(' '), + type: getFirstProp(sq, 'type') as ResourceKind, + cluster: getFirstProp(sq, 'cluster') as ClusterObjectType, + projectUuid: getFirstProp(sq, 'project'), + inTrash: getPropValue(sq, 'is', 'trashed') !== undefined, + dateFrom: getFirstProp(sq, 'from'), + dateTo: getFirstProp(sq, 'to'), + properties: getProperties(sq), + saveQuery: false, + queryName: '' + }; +}; + export const getFilters = (filterName: string, searchValue: string): string => { const filter = new FilterBuilder(); + const sq = parseSearchQuery(searchValue); + + const resourceKind = getFirstProp(sq, 'type') as ResourceKind; + + let prefix = ''; + switch (resourceKind) { + case ResourceKind.COLLECTION: + prefix = GroupContentsResourcePrefix.COLLECTION; + break; + case ResourceKind.PROCESS: + prefix = GroupContentsResourcePrefix.PROCESS; + break; + default: + prefix = GroupContentsResourcePrefix.PROJECT; + break; + } - const pq = parseQuery(searchValue); - - if (!pq.hasKeywords) { + if (!sq.hasKeywords) { filter .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION) .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS) .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT); } else { - - if (pq.properties.type) { - pq.values.forEach(v => { - let prefix = ''; - switch (ResourceKind[pq.properties.type]) { - case ResourceKind.PROJECT: - prefix = GroupContentsResourcePrefix.PROJECT; - break; - case ResourceKind.COLLECTION: - prefix = GroupContentsResourcePrefix.COLLECTION; - break; - case ResourceKind.PROCESS: - prefix = GroupContentsResourcePrefix.PROCESS; - break; - } - if (prefix !== '') { - filter.addILike(filterName, v, prefix); - } - }); + if (prefix) { + sq.values.forEach(v => + filter.addILike(filterName, v, prefix) + ); } else { - pq.values.forEach(v => { + sq.values.forEach(v => { filter .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION) .addILike(filterName, v, GroupContentsResourcePrefix.PROCESS) @@ -304,32 +421,35 @@ export const getFilters = (filterName: string, searchValue: string): string => { }); } - if (pq.properties.is && pq.properties.is === 'trashed') { + if (getPropValue(sq, 'is', 'trashed')) { + filter.addEqual("is_trashed", true); } - if (pq.properties.project) { - filter.addEqual('owner_uuid', pq.properties.project, GroupContentsResourcePrefix.PROJECT); + const projectUuid = getFirstProp(sq, 'project'); + if (projectUuid) { + filter.addEqual('uuid', projectUuid, prefix); } - if (pq.properties.from) { - filter.addGte('modified_at', buildDateFilter(pq.properties.from)); + const dateFrom = getFirstProp(sq, 'from'); + if (dateFrom) { + filter.addGte('modified_at', buildDateFilter(dateFrom)); } - if (pq.properties.to) { - filter.addLte('modified_at', buildDateFilter(pq.properties.to)); + const dateTo = getFirstProp(sq, 'to'); + if (dateTo) { + filter.addLte('modified_at', buildDateFilter(dateTo)); } - // filter - // .addIsA("uuid", buildUuidFilter(resourceKind)) - // .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION) - // .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS) - // .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT) - // .addLte('modified_at', buildDateFilter(dateTo)) - // .addGte('modified_at', buildDateFilter(dateFrom)) - // .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT) + + const props = getProperties(sq); + props.forEach(p => { + // filter.addILike(`properties.${p.key}`, p.value); + filter.addExists(p.key); + }); } return filter .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT) + .addIsA("uuid", buildUuidFilter(resourceKind)) .getFilters(); }; @@ -346,12 +466,12 @@ export const initAdvanceFormProjectsTree = () => dispatch(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)); }; -export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') => +export const changeAdvanceFormProperty = (property: string, value: PropertyValue[] | string = '') => (dispatch: Dispatch) => { dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value)); }; -export const updateAdvanceFormProperties = (propertyValues: PropertyValues) => +export const updateAdvanceFormProperties = (propertyValues: PropertyValue) => (dispatch: Dispatch) => { dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues)); };