X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/b595068e020c8fd8e8021ec1c8aa7c2a903f9668..c038a5258d5790773ccca89a192c8c2b7dcd86cb:/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..165392c6 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -26,7 +26,12 @@ export const searchBarActions = unionize({ SET_SEARCH_RESULTS: ofType(), SET_SEARCH_VALUE: ofType(), SET_SAVED_QUERIES: ofType(), - UPDATE_SAVED_QUERY: ofType() + SET_RECENT_QUERIES: ofType(), + UPDATE_SAVED_QUERY: ofType(), + SET_SELECTED_ITEM: ofType(), + MOVE_UP: ofType<{}>(), + MOVE_DOWN: ofType<{}>(), + SELECT_FIRST_ITEM: ofType<{}>() }); export type SearchBarActions = UnionOf; @@ -46,28 +51,47 @@ export const saveRecentQuery = (query: string) => export const loadRecentQueries = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const recentSearchQueries = services.searchService.getRecentQueries(); - return recentSearchQueries || []; + const recentQueries = services.searchService.getRecentQueries(); + dispatch(searchBarActions.SET_RECENT_QUERIES(recentQueries)); + return recentQueries; }; -// Todo: create ids for particular searchQuery -export const saveQuery = (data: SearchBarAdvanceFormData) => +export const searchData = (searchValue: string) => + async (dispatch: Dispatch, getState: () => RootState) => { + const currentView = getState().searchBar.currentView; + dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); + if (searchValue.length > 0) { + dispatch(searchGroups(searchValue, 5, {})); + if (currentView === SearchView.BASIC) { + dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); + dispatch(navigateToSearchResults); + } + } + }; + +export const searchAdvanceData = (data: SearchBarAdvanceFormData) => + async (dispatch: Dispatch) => { + dispatch(saveQuery(data)); + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); + dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); + dispatch(navigateToSearchResults); + }; + +const saveQuery = (data: SearchBarAdvanceFormData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const savedSearchQueries = services.searchService.getSavedQueries(); - const filteredQuery = savedSearchQueries.find(query => query.searchQuery === data.searchQuery); + const savedQueries = services.searchService.getSavedQueries(); if (data.saveQuery && data.searchQuery) { + const filteredQuery = savedQueries.find(query => query.searchQuery === data.searchQuery); if (filteredQuery) { services.searchService.editSavedQueries(data); - dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedSearchQueries)); - dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS })); + dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries)); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS })); } else { services.searchService.saveQuery(data); - dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries)); - dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS })); + dispatch(searchBarActions.SET_SAVED_QUERIES(savedQueries)); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS })); } } - dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); - dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); }; export const deleteSavedQuery = (id: number) => @@ -79,7 +103,7 @@ export const deleteSavedQuery = (id: number) => }; export const editSavedQuery = (data: SearchBarAdvanceFormData) => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + (dispatch: Dispatch) => { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED)); dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery)); dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); @@ -87,120 +111,128 @@ export const editSavedQuery = (data: SearchBarAdvanceFormData) => export const openSearchView = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - dispatch(searchBarActions.OPEN_SEARCH_VIEW()); const savedSearchQueries = services.searchService.getSavedQueries(); dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries)); + dispatch(loadRecentQueries()); + dispatch(searchBarActions.OPEN_SEARCH_VIEW()); + dispatch(searchBarActions.SELECT_FIRST_ITEM()); }; export const closeSearchView = () => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - const isOpen = getState().searchBar.open; - if (isOpen) { - dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); - dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); - } + (dispatch: Dispatch) => { + dispatch(searchBarActions.SET_SELECTED_ITEM('')); + dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); + }; + +export const closeAdvanceView = () => + (dispatch: Dispatch) => { + dispatch(searchBarActions.SET_SEARCH_VALUE('')); + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); }; export const navigateToItem = (uuid: string) => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + (dispatch: Dispatch) => { + dispatch(searchBarActions.SET_SELECTED_ITEM('')); 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()); - } +export const changeData = (searchValue: string) => + (dispatch: Dispatch, getState: () => RootState) => { 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(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE)); + dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue)); + debounceStartSearch(dispatch); } else { - dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); + dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); dispatch(searchBarActions.SET_SEARCH_RESULTS([])); + dispatch(searchBarActions.SELECT_FIRST_ITEM()); } }; -export const submitData = (event: React.FormEvent) => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { +export const submitData = (event: React.FormEvent) => + (dispatch: Dispatch, getState: () => RootState) => { event.preventDefault(); const searchValue = getState().searchBar.searchValue; dispatch(saveRecentQuery(searchValue)); - dispatch(searchDataOnEnter(searchValue)); dispatch(loadRecentQueries()); - }; - -export const searchDataOnEnter = (searchValue: string) => - async (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(navigateToSearchResults); + }; + +const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch(startSearch()), DEFAULT_SEARCH_DEBOUNCE); + +const startSearch = () => + (dispatch: Dispatch, getState: () => RootState) => { + const searchValue = getState().searchBar.searchValue; + dispatch(searchData(searchValue)); + }; + +const searchGroups = (searchValue: string, limit: number, {...props}) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const currentView = getState().searchBar.currentView; + + if (searchValue || currentView === SearchView.ADVANCED) { + const filters = getFilters('name', searchValue, props); 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, props: any): string => { + const { resourceKind, dateTo, dateFrom } = props; 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) + .addLte('modified_at', buildDateFilter(dateTo)) + .addGte('modified_at', buildDateFilter(dateFrom)) .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT) .getFilters(); }; +const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => { + return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS]; +}; + +const buildDateFilter = (date?: string): string => { + return date ? date : ''; +}; + export const initAdvanceFormProjectsTree = () => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + (dispatch: Dispatch) => { dispatch(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)); }; export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + (dispatch: Dispatch) => { dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value)); }; export const updateAdvanceFormProperties = (propertyValues: PropertyValues) => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + (dispatch: Dispatch) => { dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues)); - }; \ No newline at end of file + }; + +export const moveUp = () => + (dispatch: Dispatch) => { + dispatch(searchBarActions.MOVE_UP()); + }; + +export const moveDown = () => + (dispatch: Dispatch) => { + dispatch(searchBarActions.MOVE_DOWN()); + };