cr changes
[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     dataFrom: string;
30     dataTo: string;
31     searchQuery: string;
32 }
33
34 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
35
36 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
37
38 export const saveRecentQuery = (query: string) =>
39     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
40         services.searchService.saveRecentQuery(query);
41
42
43 export const loadRecentQueries = () =>
44     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
45         const recentSearchQueries = services.searchService.getRecentQueries();
46         return recentSearchQueries || [];
47     };
48
49 export const saveQuery = (query: string) =>
50     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
51         services.searchService.saveQuery(query);
52         dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
53     };
54
55 export const deleteSavedQuery = (id: number) =>
56     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
57         services.searchService.deleteSavedQuery(id);
58         const savedSearchQueries = services.searchService.getSavedQueries();
59         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
60         return savedSearchQueries || [];
61     };
62
63 export const openSearchView = () =>
64     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
65         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
66         const savedSearchQueries = services.searchService.getSavedQueries();
67         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
68     };
69
70
71 export const searchData = (searchValue: string) =>
72     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
74         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
75         if (searchValue) {
76             const filters = getFilters('name', searchValue);
77             const { items } = await services.groupsService.contents('', {
78                 filters,
79                 limit: 5,
80                 recursive: true
81             });
82             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
83         }
84     };
85
86 const getFilters = (filterName: string, searchValue: string): string => {
87     return new FilterBuilder()
88         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
89         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
90         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
91         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
92         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
93         .getFilters();
94 };