add checkbox field component and change advance form
[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 });
21
22 export type SearchBarActions = UnionOf<typeof searchBarActions>;
23
24 export interface SearchBarAdvanceFormData {
25     type?: GroupContentsResource;
26     cluster?: string;
27     project?: string;
28     inTrash: boolean;
29     dataFrom: string;
30     dataTo: string;
31     saveQuery: boolean;
32     searchQuery: string;
33 }
34
35 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
36
37 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
38
39 export const saveRecentQuery = (query: string) =>
40     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
41         services.searchQueriesService.saveRecentQuery(query);
42     };
43
44 export const loadRecentQueries = () =>
45     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
46         const recentSearchQueries = services.searchQueriesService.getRecentQueries();
47         return recentSearchQueries || [];
48     };
49
50 export const searchData = (searchValue: string) =>
51     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
53         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
54         if (searchValue) {
55             const filters = getFilters('name', searchValue);
56             const { items } = await services.groupsService.contents('', {
57                 filters,
58                 limit: 5,
59                 recursive: true
60             });
61             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
62         }
63     };
64
65 const getFilters = (filterName: string, searchValue: string): string => {
66     return new FilterBuilder()
67         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
68         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
69         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
70         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
71         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
72         .getFilters();
73 };