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