Merge branch '14276-structured-search-searvice'
[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 // Todo: create ids for particular searchQuery
54 export const saveQuery = (data: SearchBarAdvanceFormData) =>
55     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
56         const savedSearchQueries = services.searchService.getSavedQueries();
57         const filteredQuery = savedSearchQueries.find(query => query.searchQuery === data.searchQuery);
58         if (data.saveQuery && data.searchQuery) {
59             if (filteredQuery) {
60                 services.searchService.editSavedQueries(data);
61                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedSearchQueries));
62                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
63             } else {
64                 services.searchService.saveQuery(data);
65                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
66                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
67             }
68         }
69         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
70         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
71     };
72
73 export const deleteSavedQuery = (id: number) =>
74     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
75         services.searchService.deleteSavedQuery(id);
76         const savedSearchQueries = services.searchService.getSavedQueries();
77         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
78         return savedSearchQueries || [];
79     };
80
81 export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
82     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
83         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
84         dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery));
85         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
86     };
87
88 export const openSearchView = () =>
89     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
90         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
91         const savedSearchQueries = services.searchService.getSavedQueries();
92         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
93     };
94
95 export const closeSearchView = () =>
96     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
97         const isOpen = getState().searchBar.open;
98         if (isOpen) {
99             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
100             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
101         }
102     };
103
104 export const navigateToItem = (uuid: string) =>
105     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
106         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
107         dispatch(navigateTo(uuid));
108     };
109
110 export const searchData = (searchValue: string) =>
111     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
112         const currentView = getState().searchBar.currentView;
113         if (currentView !== SearchView.AUTOCOMPLETE) {
114             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
115         }
116         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
117         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
118         if (searchValue) {
119             const filters = getFilters('name', searchValue);
120             const { items } = await services.groupsService.contents('', {
121                 filters,
122                 limit: 5,
123                 recursive: true
124             });
125             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
126         }
127         if (currentView !== SearchView.AUTOCOMPLETE) {
128             dispatch(navigateToSearchResults);
129         }
130         
131     };
132
133 export const changeData = (searchValue: string) => 
134     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
135         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
136         if (searchValue.length > 0) {
137             dispatch<any>(goToView(SearchView.AUTOCOMPLETE));
138         } else {
139             dispatch<any>(goToView(SearchView.BASIC));
140         }
141         debounceStartSearch(dispatch);
142     };
143
144 const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
145
146 const startSearch = () => 
147     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
148         const searchValue = getState().searchBar.searchValue;
149         if (searchValue.length > 0) {
150             dispatch<any>(searchData(searchValue));
151         } else {
152             dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
153             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
154         }
155     };
156
157 export const submitData = (event: React.FormEvent<HTMLFormElement>) => 
158     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
159         event.preventDefault();
160         const searchValue = getState().searchBar.searchValue;
161         dispatch<any>(saveRecentQuery(searchValue));
162         dispatch<any>(searchDataOnEnter(searchValue));
163         dispatch<any>(loadRecentQueries());
164     };
165
166 export const searchDataOnEnter = (searchValue: string) =>
167     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
168         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
169         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
170         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
171         if (searchValue) {
172             const filters = getFilters('name', searchValue);
173             const { items } = await services.groupsService.contents('', {
174                 filters,
175                 limit: 5,
176                 recursive: true
177             });
178             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
179         }
180         dispatch(navigateToSearchResults);
181     };
182
183 export const getFilters = (filterName: string, searchValue: string): string => {
184     return new FilterBuilder()
185         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
186         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
187         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
188         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
189         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
190         .getFilters();
191 };
192
193 export const initAdvanceFormProjectsTree = () =>
194     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
195         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
196     };
197
198 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
199     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
200         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
201     };
202
203 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
204     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
205         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
206     };