Add navigation with arrow on basic search view
[arvados-workbench2.git] / src / store / search-bar / search-bar-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "~/common/unionize";
6 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
7 import { Dispatch } from 'redux';
8 import { change, arrayPush } from 'redux-form';
9 import { RootState } from '~/store/store';
10 import { initUserProject } from '~/store/tree-picker/tree-picker-actions';
11 import { ServiceRepository } from '~/services/services';
12 import { FilterBuilder } from "~/services/api/filter-builder";
13 import { ResourceKind } from '~/models/resource';
14 import { GroupClass } from '~/models/group';
15 import { SearchView } from '~/store/search-bar/search-bar-reducer';
16 import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action';
17 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 import { initialize } from 'redux-form';
19 import { SearchBarAdvanceFormData, PropertyValues } from '~/models/search-bar';
20 import { debounce } from 'debounce';
21
22 export const searchBarActions = unionize({
23     SET_CURRENT_VIEW: ofType<string>(),
24     OPEN_SEARCH_VIEW: ofType<{}>(),
25     CLOSE_SEARCH_VIEW: ofType<{}>(),
26     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
27     SET_SEARCH_VALUE: ofType<string>(),
28     SET_SAVED_QUERIES: ofType<SearchBarAdvanceFormData[]>(),
29     SET_RECENT_QUERIES: ofType<string[]>(),
30     UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>(),
31     SET_SELECTED_ITEM: ofType<string>(),
32     MOVE_UP: ofType<{}>(),
33     MOVE_DOWN: ofType<{}>()
34 });
35
36 export type SearchBarActions = UnionOf<typeof searchBarActions>;
37
38 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
39
40 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
41
42 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
43
44 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
45
46 export const saveRecentQuery = (query: string) =>
47     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
48         services.searchService.saveRecentQuery(query);
49
50
51 export const loadRecentQueries = () =>
52     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53         const recentQueries = services.searchService.getRecentQueries() || [];
54         dispatch(searchBarActions.SET_RECENT_QUERIES(recentQueries));
55         return recentQueries;
56     };
57
58 export const searchData = (searchValue: string) =>
59     async (dispatch: Dispatch, getState: () => RootState) => {
60         const currentView = getState().searchBar.currentView;
61         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
62         // dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
63         if (searchValue.length > 0) {
64             dispatch<any>(searchGroups(searchValue));
65             if (currentView === SearchView.BASIC) {
66                 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
67                 dispatch(navigateToSearchResults);
68             }
69         }
70     };
71
72 export const searchAdvanceData = (data: SearchBarAdvanceFormData) =>
73     async (dispatch: Dispatch) => {
74         dispatch<any>(saveQuery(data));
75         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
76         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
77         dispatch(navigateToSearchResults);
78     };
79
80 const saveQuery = (data: SearchBarAdvanceFormData) =>
81     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
82         const savedQueries = services.searchService.getSavedQueries();
83         if (data.saveQuery && data.searchQuery) {
84             const filteredQuery = savedQueries.find(query => query.searchQuery === data.searchQuery);
85             if (filteredQuery) {
86                 services.searchService.editSavedQueries(data);
87                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries));
88                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
89             } else {
90                 services.searchService.saveQuery(data);
91                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedQueries));
92                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
93             }
94         }
95     };
96
97 export const deleteSavedQuery = (id: number) =>
98     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
99         services.searchService.deleteSavedQuery(id);
100         const savedSearchQueries = services.searchService.getSavedQueries();
101         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
102         return savedSearchQueries || [];
103     };
104
105 export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
106     (dispatch: Dispatch<any>) => {
107         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
108         dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery));
109         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
110     };
111
112 export const openSearchView = () =>
113     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
114         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
115         const savedSearchQueries = services.searchService.getSavedQueries();
116         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
117     };
118
119 export const closeSearchView = () =>
120     (dispatch: Dispatch<any>, getState: () => RootState) => {
121         const isOpen = getState().searchBar.open;
122         if (isOpen) {
123             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
124             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
125         }
126     };
127
128 export const closeAdvanceView = () =>
129     (dispatch: Dispatch<any>) => {
130         dispatch(searchBarActions.SET_SEARCH_VALUE(''));
131         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
132     };
133
134 export const navigateToItem = (uuid: string) =>
135     (dispatch: Dispatch<any>) => {
136         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
137         dispatch(navigateTo(uuid));
138     };
139
140 export const changeData = (searchValue: string) =>
141     (dispatch: Dispatch, getState: () => RootState) => {
142         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
143         const currentView = getState().searchBar.currentView;
144         const searchValuePresent = searchValue.length > 0;
145
146         if (currentView === SearchView.ADVANCED) {
147
148         } else if (searchValuePresent) {
149             dispatch<any>(goToView(SearchView.AUTOCOMPLETE));
150             debounceStartSearch(dispatch);
151         } else {
152             dispatch<any>(goToView(SearchView.BASIC));
153             dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
154             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
155         }
156     };
157
158 export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
159     (dispatch: Dispatch, getState: () => RootState) => {
160         event.preventDefault();
161         const searchValue = getState().searchBar.searchValue;
162         dispatch<any>(saveRecentQuery(searchValue));
163         dispatch<any>(loadRecentQueries());
164         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
165         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
166         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
167         dispatch(navigateToSearchResults);
168     };
169
170 const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
171
172 const startSearch = () =>
173     (dispatch: Dispatch, getState: () => RootState) => {
174         const searchValue = getState().searchBar.searchValue;
175         dispatch<any>(searchData(searchValue));
176     };
177
178 const searchGroups = (searchValue: string, limit = 5, resourceKind?: ResourceKind) =>
179     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
180         const currentView = getState().searchBar.currentView;
181
182         if (searchValue || currentView === SearchView.ADVANCED) {
183             const filters = getFilters('name', searchValue, resourceKind);
184             const { items } = await services.groupsService.contents('', {
185                 filters,
186                 limit,
187                 recursive: true
188             });
189             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
190         }
191     };
192
193 export const getFilters = (filterName: string, searchValue: string, resourceKind?: ResourceKind): string => {
194     return new FilterBuilder()
195         .addIsA("uuid", buildUuidFilter(resourceKind))
196         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
197         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
198         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
199         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
200         .getFilters();
201 };
202
203 const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => {
204     return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS];
205 };
206
207 export const initAdvanceFormProjectsTree = () =>
208     (dispatch: Dispatch) => {
209         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
210     };
211
212 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
213     (dispatch: Dispatch) => {
214         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
215     };
216
217 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
218     (dispatch: Dispatch) => {
219         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
220     };
221
222 export const moveUp = () =>
223     (dispatch: Dispatch) => {
224         dispatch(searchBarActions.MOVE_UP());
225     };
226
227 export const moveDown = () =>
228     (dispatch: Dispatch) => {
229         dispatch(searchBarActions.MOVE_DOWN());
230     };