change code after CR
[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     UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>()
30 });
31
32 export type SearchBarActions = UnionOf<typeof searchBarActions>;
33
34 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
35
36 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
37
38 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
39
40 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
41
42 export const saveRecentQuery = (query: string) =>
43     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
44         services.searchService.saveRecentQuery(query);
45
46
47 export const loadRecentQueries = () =>
48     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
49         const recentSearchQueries = services.searchService.getRecentQueries();
50         return recentSearchQueries || [];
51     };
52
53 export const searchData = (searchValue: string) =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         const currentView = getState().searchBar.currentView;
56         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
57         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
58         dispatch<any>(searchGroups(searchValue, 5, {}));
59         if (currentView === SearchView.BASIC) {
60             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
61             dispatch(navigateToSearchResults);
62         }
63
64     };
65
66 export const searchAdvanceData = (data: SearchBarAdvanceFormData) =>
67     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68         const searchValue = getState().searchBar.searchValue;
69         dispatch<any>(saveQuery(data));
70         dispatch<any>(searchGroups(searchValue, 100, data));
71         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
72         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
73         dispatch(navigateToSearchResults);
74     };
75
76 // Todo: create ids for particular searchQuery
77 const saveQuery = (data: SearchBarAdvanceFormData) =>
78     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
79         const savedSearchQueries = services.searchService.getSavedQueries();
80         const filteredQuery = savedSearchQueries.find(query => query.searchQuery === data.searchQuery);
81         if (data.saveQuery && data.searchQuery) {
82             if (filteredQuery) {
83                 services.searchService.editSavedQueries(data);
84                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedSearchQueries));
85                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
86             } else {
87                 services.searchService.saveQuery(data);
88                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
89                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
90             }
91         }
92     };
93
94 export const deleteSavedQuery = (id: number) =>
95     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
96         services.searchService.deleteSavedQuery(id);
97         const savedSearchQueries = services.searchService.getSavedQueries();
98         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
99         return savedSearchQueries || [];
100     };
101
102 export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
103     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
104         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
105         dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery));
106         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
107     };
108
109 export const openSearchView = () =>
110     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
111         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
112         const savedSearchQueries = services.searchService.getSavedQueries();
113         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
114     };
115
116 export const closeSearchView = () =>
117     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
118         const isOpen = getState().searchBar.open;
119         if (isOpen) {
120             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
121             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
122         }
123     };
124
125 export const closeAdvanceView = () =>
126     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
127         dispatch(searchBarActions.SET_SEARCH_VALUE(''));
128         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
129     };
130
131 export const navigateToItem = (uuid: string) =>
132     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
133         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
134         dispatch(navigateTo(uuid));
135     };
136
137 export const changeData = (searchValue: string) =>
138     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
139         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
140         const currentView = getState().searchBar.currentView;
141         const searchValuePresent = searchValue.length > 0;
142
143         if (currentView === SearchView.ADVANCED) {
144
145         } else if (searchValuePresent) {
146             dispatch<any>(goToView(SearchView.AUTOCOMPLETE));
147             debounceStartSearch(dispatch);
148         } else {
149             dispatch<any>(goToView(SearchView.BASIC));
150             dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
151             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
152         }
153     };
154
155 export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
156     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
157         event.preventDefault();
158         const searchValue = getState().searchBar.searchValue;
159         dispatch<any>(saveRecentQuery(searchValue));
160         dispatch<any>(searchDataOnEnter(searchValue));
161         dispatch<any>(loadRecentQueries());
162     };
163
164 const searchDataOnEnter = (searchValue: string) =>
165     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
166         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
167         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
168         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
169         dispatch<any>(searchGroups(searchValue, 100, {}));
170         dispatch(navigateToSearchResults);
171     };
172
173 const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
174
175 const startSearch = () =>
176     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
177         const searchValue = getState().searchBar.searchValue;
178         dispatch<any>(searchData(searchValue));
179     };
180
181 const searchGroups = (searchValue: string, limit: number, {...props}) =>
182     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
183         const currentView = getState().searchBar.currentView;
184
185         if (searchValue || currentView === SearchView.ADVANCED) {
186             const filters = getFilters('name', searchValue, props);
187             const { items } = await services.groupsService.contents('', {
188                 filters,
189                 limit,
190                 recursive: true
191             });
192             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
193         }
194     };
195
196 export const getFilters = (filterName: string, searchValue: string, props: any): string => {
197     const { resourceKind, dateTo, dateFrom } = props;
198     return new FilterBuilder()
199         .addIsA("uuid", buildUuidFilter(resourceKind))
200         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
201         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
202         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
203         .addLte('modified_at', buildDateFilter(dateTo))
204         .addGte('modified_at', buildDateFilter(dateFrom))
205         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
206         .getFilters();
207 };
208
209 const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => {
210     return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS];
211 };
212
213 const buildDateFilter = (date?: string): string => {
214     return date ? date : '';
215 };
216
217 export const initAdvanceFormProjectsTree = () =>
218     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
219         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
220     };
221
222 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
223     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
224         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
225     };
226
227 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
228     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
229         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
230     };