1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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[]>()
32 export type SearchBarActions = UnionOf<typeof searchBarActions>;
34 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
36 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
38 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
40 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
42 export const saveRecentQuery = (query: string) =>
43 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
44 services.searchService.saveRecentQuery(query);
47 export const loadRecentQueries = () =>
48 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
49 const recentSearchQueries = services.searchService.getRecentQueries();
50 return recentSearchQueries || [];
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) {
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 }));
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 }));
69 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
70 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
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 || [];
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));
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));
95 export const closeSearchView = () =>
96 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
97 const isOpen = getState().searchBar.open;
99 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
100 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
104 export const navigateToItem = (uuid: string) =>
105 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
106 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
107 dispatch(navigateTo(uuid));
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());
116 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
117 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
119 const filters = getFilters('name', searchValue);
120 const { items } = await services.groupsService.contents('', {
125 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
127 if (currentView !== SearchView.AUTOCOMPLETE) {
128 dispatch(navigateToSearchResults);
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));
139 dispatch<any>(goToView(SearchView.BASIC));
141 debounceStartSearch(dispatch);
144 const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
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));
152 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
153 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
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());
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([]));
172 const filters = getFilters('name', searchValue);
173 const { items } = await services.groupsService.contents('', {
178 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
180 dispatch(navigateToSearchResults);
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)
193 export const initAdvanceFormProjectsTree = () =>
194 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
195 dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
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));
203 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
204 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
205 dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));