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