Merge branch '14308-basic-view-saving-and-removing-queries'
[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
14 export const searchBarActions = unionize({
15     SET_CURRENT_VIEW: ofType<string>(),
16     OPEN_SEARCH_VIEW: ofType<{}>(),
17     CLOSE_SEARCH_VIEW: ofType<{}>(),
18     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
19     SET_SEARCH_VALUE: ofType<string>(),
20     SET_SAVED_QUERIES: ofType<string[]>()
21 });
22
23 export type SearchBarActions = UnionOf<typeof searchBarActions>;
24
25 export interface SearchBarAdvanceFormData {
26     type?: GroupContentsResource;
27     cluster?: string;
28     project?: string;
29     inTrash: boolean;
30     dataFrom: string;
31     dataTo: string;
32     saveQuery: boolean;
33     searchQuery: string;
34 }
35
36 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
37
38 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
39
40 export const saveRecentQuery = (query: string) =>
41     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
42         services.searchService.saveRecentQuery(query);
43
44
45 export const loadRecentQueries = () =>
46     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
47         const recentSearchQueries = services.searchService.getRecentQueries();
48         return recentSearchQueries || [];
49     };
50
51 export const saveQuery = (data: SearchBarAdvanceFormData) =>
52     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53         if (data.saveQuery && data.searchQuery) {
54             services.searchService.saveQuery(data.searchQuery);
55             dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
56         }
57     };
58
59 export const deleteSavedQuery = (id: number) =>
60     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
61         services.searchService.deleteSavedQuery(id);
62         const savedSearchQueries = services.searchService.getSavedQueries();
63         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
64         return savedSearchQueries || [];
65     };
66
67 export const openSearchView = () =>
68     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
69         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
70         const savedSearchQueries = services.searchService.getSavedQueries();
71         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
72     };
73
74
75 export const searchData = (searchValue: string) =>
76     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
77         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
78         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
79         if (searchValue) {
80             const filters = getFilters('name', searchValue);
81             const { items } = await services.groupsService.contents('', {
82                 filters,
83                 limit: 5,
84                 recursive: true
85             });
86             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
87         }
88     };
89
90 const getFilters = (filterName: string, searchValue: string): string => {
91     return new FilterBuilder()
92         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
93         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
94         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
95         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
96         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
97         .getFilters();
98 };