X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/c0b0f2e4f2f795e06412db84c77b6b0447815ca2..dacbca6d4ac55f5e837d296d3ac6a048b6759c96:/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 f574b64c..653a8f53 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -50,8 +50,31 @@ export const loadRecentQueries = () => return recentSearchQueries || []; }; +export const searchData = (searchValue: string) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const currentView = getState().searchBar.currentView; + dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); + dispatch(searchBarActions.SET_SEARCH_RESULTS([])); + dispatch(searchGroups(searchValue)); + if (currentView === SearchView.BASIC) { + dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); + dispatch(navigateToSearchResults); + } + + }; + +export const searchAdvanceData = (data: SearchBarAdvanceFormData) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const searchValue = getState().searchBar.searchValue; + dispatch(saveQuery(data)); + dispatch(searchGroups(searchValue, 100, data.type)); + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); + dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); + dispatch(navigateToSearchResults); + }; + // Todo: create ids for particular searchQuery -export const saveQuery = (data: SearchBarAdvanceFormData) => +const saveQuery = (data: SearchBarAdvanceFormData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const savedSearchQueries = services.searchService.getSavedQueries(); const filteredQuery = savedSearchQueries.find(query => query.searchQuery === data.searchQuery); @@ -66,8 +89,6 @@ export const saveQuery = (data: SearchBarAdvanceFormData) => dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS })); } } - dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); - dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); }; export const deleteSavedQuery = (id: number) => @@ -101,60 +122,37 @@ export const closeSearchView = () => } }; +export const closeAdvanceView = () => + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + dispatch(searchBarActions.SET_SEARCH_VALUE('')); + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); + }; + export const navigateToItem = (uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); dispatch(navigateTo(uuid)); }; -export const searchData = (searchValue: string) => - async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const currentView = getState().searchBar.currentView; - if (currentView !== SearchView.AUTOCOMPLETE) { - dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); - } - dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); - dispatch(searchBarActions.SET_SEARCH_RESULTS([])); - if (searchValue) { - const filters = getFilters('name', searchValue); - const { items } = await services.groupsService.contents('', { - filters, - limit: 5, - recursive: true - }); - dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); - } - if (currentView !== SearchView.AUTOCOMPLETE) { - dispatch(navigateToSearchResults); - } - - }; - export const changeData = (searchValue: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); - if (searchValue.length > 0) { - dispatch(goToView(SearchView.AUTOCOMPLETE)); - } else { - dispatch(goToView(SearchView.BASIC)); - } - debounceStartSearch(dispatch); - }; + const currentView = getState().searchBar.currentView; + const searchValuePresent = searchValue.length > 0; -const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch(startSearch()), DEFAULT_SEARCH_DEBOUNCE); + if (currentView === SearchView.ADVANCED) { -const startSearch = () => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const searchValue = getState().searchBar.searchValue; - if (searchValue.length > 0) { - dispatch(searchData(searchValue)); + } else if (searchValuePresent) { + dispatch(goToView(SearchView.AUTOCOMPLETE)); + debounceStartSearch(dispatch); } else { + dispatch(goToView(SearchView.BASIC)); dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); dispatch(searchBarActions.SET_SEARCH_RESULTS([])); } }; -export const submitData = (event: React.FormEvent) => +export const submitData = (event: React.FormEvent) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { event.preventDefault(); const searchValue = getState().searchBar.searchValue; @@ -163,26 +161,41 @@ export const submitData = (event: React.FormEvent) => dispatch(loadRecentQueries()); }; -export const searchDataOnEnter = (searchValue: string) => - async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { +const searchDataOnEnter = (searchValue: string) => + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); dispatch(searchBarActions.SET_SEARCH_RESULTS([])); - if (searchValue) { - const filters = getFilters('name', searchValue); + dispatch(searchGroups(searchValue, 100)); + dispatch(navigateToSearchResults); + }; + +const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch(startSearch()), DEFAULT_SEARCH_DEBOUNCE); + +const startSearch = () => + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const searchValue = getState().searchBar.searchValue; + dispatch(searchData(searchValue)); + }; + +const searchGroups = (searchValue: string, limit = 5, resourceKind?: ResourceKind) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const currentView = getState().searchBar.currentView; + + if (searchValue || currentView === SearchView.ADVANCED) { + const filters = getFilters('name', searchValue, resourceKind); const { items } = await services.groupsService.contents('', { filters, - limit: 5, + limit, recursive: true }); dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); } - dispatch(navigateToSearchResults); }; -export const getFilters = (filterName: string, searchValue: string): string => { +export const getFilters = (filterName: string, searchValue: string, resourceKind?: ResourceKind): string => { return new FilterBuilder() - .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS]) + .addIsA("uuid", buildUuidFilter(resourceKind)) .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION) .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS) .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT) @@ -190,6 +203,10 @@ export const getFilters = (filterName: string, searchValue: string): string => { .getFilters(); }; +const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => { + return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS]; +}; + export const initAdvanceFormProjectsTree = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { dispatch(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));