Merge branch 'master' into 14498-virtual-machines-admin-panel
[arvados.git] / src / store / search-bar / search-bar-actions.ts
index 9f86e4600ff163a319affaea8deddee64f1efa2b..165392c6c20ef0dc9fef9bdbfe559608529a623a 100644 (file)
@@ -30,7 +30,8 @@ export const searchBarActions = unionize({
     UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>(),
     SET_SELECTED_ITEM: ofType<string>(),
     MOVE_UP: ofType<{}>(),
-    MOVE_DOWN: ofType<{}>()
+    MOVE_DOWN: ofType<{}>(),
+    SELECT_FIRST_ITEM: ofType<{}>()
 });
 
 export type SearchBarActions = UnionOf<typeof searchBarActions>;
@@ -50,7 +51,7 @@ export const saveRecentQuery = (query: string) =>
 
 export const loadRecentQueries = () =>
     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
-        const recentQueries = services.searchService.getRecentQueries() || [];
+        const recentQueries = services.searchService.getRecentQueries();
         dispatch(searchBarActions.SET_RECENT_QUERIES(recentQueries));
         return recentQueries;
     };
@@ -59,9 +60,8 @@ export const searchData = (searchValue: string) =>
     async (dispatch: Dispatch, getState: () => RootState) => {
         const currentView = getState().searchBar.currentView;
         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
-        // dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
         if (searchValue.length > 0) {
-            dispatch<any>(searchGroups(searchValue));
+            dispatch<any>(searchGroups(searchValue, 5, {}));
             if (currentView === SearchView.BASIC) {
                 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
                 dispatch(navigateToSearchResults);
@@ -111,18 +111,17 @@ export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
 
 export const openSearchView = () =>
     (dispatch: Dispatch<any>, 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<any>, getState: () => RootState) => {
-        const isOpen = getState().searchBar.open;
-        if (isOpen) {
-            dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
-            dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
-        }
+    (dispatch: Dispatch<any>) => {
+        dispatch(searchBarActions.SET_SELECTED_ITEM(''));
+        dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
     };
 
 export const closeAdvanceView = () =>
@@ -133,6 +132,7 @@ export const closeAdvanceView = () =>
 
 export const navigateToItem = (uuid: string) =>
     (dispatch: Dispatch<any>) => {
+        dispatch(searchBarActions.SET_SELECTED_ITEM(''));
         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
         dispatch(navigateTo(uuid));
     };
@@ -146,12 +146,13 @@ export const changeData = (searchValue: string) =>
         if (currentView === SearchView.ADVANCED) {
 
         } else if (searchValuePresent) {
-            dispatch<any>(goToView(SearchView.AUTOCOMPLETE));
+            dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
+            dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue));
             debounceStartSearch(dispatch);
         } else {
-            dispatch<any>(goToView(SearchView.BASIC));
-            dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
+            dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
+            dispatch(searchBarActions.SELECT_FIRST_ITEM());
         }
     };
 
@@ -175,12 +176,12 @@ const startSearch = () =>
         dispatch<any>(searchData(searchValue));
     };
 
-const searchGroups = (searchValue: string, limit = 5, resourceKind?: ResourceKind) =>
+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, resourceKind);
+            const filters = getFilters('name', searchValue, props);
             const { items } = await services.groupsService.contents('', {
                 filters,
                 limit,
@@ -190,12 +191,15 @@ const searchGroups = (searchValue: string, limit = 5, resourceKind?: ResourceKin
         }
     };
 
-export const getFilters = (filterName: string, searchValue: string, resourceKind?: ResourceKind): string => {
+export const getFilters = (filterName: string, searchValue: string, props: any): string => {
+    const { resourceKind, dateTo, dateFrom } = props;
     return new FilterBuilder()
         .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();
 };
@@ -204,6 +208,10 @@ 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) => {
         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));