searching on text
[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 { RootState } from '~/store/store';
9 import { ServiceRepository } from '~/services/services';
10 import { FilterBuilder } from "~/services/api/filter-builder";
11 import { ResourceKind } from '~/models/resource';
12 import { GroupClass } from '~/models/group';
13 import { SearchView } from '~/store/search-bar/search-bar-reducer';
14 import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action';
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16
17 export const searchBarActions = unionize({
18     SET_CURRENT_VIEW: ofType<string>(),
19     OPEN_SEARCH_VIEW: ofType<{}>(),
20     CLOSE_SEARCH_VIEW: ofType<{}>(),
21     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
22     SET_SEARCH_VALUE: ofType<string>(),
23     SET_SAVED_QUERIES: ofType<string[]>()
24 });
25
26 export type SearchBarActions = UnionOf<typeof searchBarActions>;
27
28 export interface SearchBarAdvanceFormData {
29     type?: GroupContentsResource;
30     cluster?: string;
31     project?: string;
32     inTrash: boolean;
33     dataFrom: string;
34     dataTo: string;
35     saveQuery: boolean;
36     searchQuery: string;
37 }
38
39 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
40
41 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
42
43 export const saveRecentQuery = (query: string) =>
44     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
45         services.searchService.saveRecentQuery(query);
46
47
48 export const loadRecentQueries = () =>
49     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
50         const recentSearchQueries = services.searchService.getRecentQueries();
51         return recentSearchQueries || [];
52     };
53
54 export const saveQuery = (data: SearchBarAdvanceFormData) =>
55     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
56         if (data.saveQuery && data.searchQuery) {
57             services.searchService.saveQuery(data.searchQuery);
58             dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
59             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', kind: SnackbarKind.SUCCESS }));
60         }
61     };
62
63 export const deleteSavedQuery = (id: number) =>
64     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
65         services.searchService.deleteSavedQuery(id);
66         const savedSearchQueries = services.searchService.getSavedQueries();
67         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
68         return savedSearchQueries || [];
69     };
70
71 export const openSearchView = () =>
72     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
73         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
74         const savedSearchQueries = services.searchService.getSavedQueries();
75         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
76     };
77
78 export const closeSearchView = () =>
79     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
81         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
82     };
83
84 export const navigateToItem = (uuid: string) =>
85     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
86         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
87         dispatch(navigateTo(uuid));
88     };
89     
90 export const searchData = (searchValue: string) =>
91     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
92         const currentView = getState().searchBar.currentView;
93         if (currentView !== SearchView.AUTOCOMPLETE) {
94             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
95         }
96         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
97         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
98         if (searchValue) {
99             const filters = getFilters('name', searchValue);
100             const { items } = await services.groupsService.contents('', {
101                 filters,
102                 limit: 5,
103                 recursive: true
104             });
105             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
106         }
107         dispatch(navigateToSearchResults);
108     };
109
110 export const getFilters = (filterName: string, searchValue: string): string => {
111     return new FilterBuilder()
112         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
113         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
114         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
115         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
116         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
117         .getFilters();
118 };