init form properties and project tree
[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 { change, arrayPush } from 'redux-form';
9 import { RootState } from '~/store/store';
10 import { initUserProject } from '~/store/tree-picker/tree-picker-actions';
11 import { ServiceRepository } from '~/services/services';
12 import { FilterBuilder } from "~/services/api/filter-builder";
13 import { ResourceKind } from '~/models/resource';
14 import { GroupClass } from '~/models/group';
15 import { SearchBarAdvanceFormData, PropertyValues } from '~/models/search-bar';
16
17 export const searchBarActions = unionize({
18     SET_CURRENT_VIEW: ofType<string>(),
19     OPEN_SEARCH_VIEW: ofType<{}>(),
20     CLOSE_SEARCH_VIEW: ofType<{}>(),
21     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
22     SET_SEARCH_VALUE: ofType<string>(),
23     SET_SAVED_QUERIES: ofType<string[]>()
24 });
25
26 export type SearchBarActions = UnionOf<typeof searchBarActions>;
27
28 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
29
30 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
31
32 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
33
34 export const saveRecentQuery = (query: string) =>
35     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
36         services.searchService.saveRecentQuery(query);
37
38
39 export const loadRecentQueries = () =>
40     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
41         const recentSearchQueries = services.searchService.getRecentQueries();
42         return recentSearchQueries || [];
43     };
44
45 export const saveQuery = (data: SearchBarAdvanceFormData) =>
46     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
47         if (data.saveQuery && data.searchQuery) {
48             services.searchService.saveQuery(data.searchQuery);
49             dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
50         }
51     };
52
53 export const deleteSavedQuery = (id: number) =>
54     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
55         services.searchService.deleteSavedQuery(id);
56         const savedSearchQueries = services.searchService.getSavedQueries();
57         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
58         return savedSearchQueries || [];
59     };
60
61 export const openSearchView = () =>
62     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
63         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
64         const savedSearchQueries = services.searchService.getSavedQueries();
65         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
66     };
67
68 export const closeSearchView = () => 
69     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
70         const isOpen = getState().searchBar.open;
71         if(isOpen) {
72             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
73         }
74     };
75
76 export const searchData = (searchValue: string) =>
77     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
79         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
80         if (searchValue) {
81             const filters = getFilters('name', searchValue);
82             const { items } = await services.groupsService.contents('', {
83                 filters,
84                 limit: 5,
85                 recursive: true
86             });
87             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
88         }
89     };
90
91 const getFilters = (filterName: string, searchValue: string): string => {
92     return new FilterBuilder()
93         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
94         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
95         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
96         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
97         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
98         .getFilters();
99 };
100
101 export const initAdvanceFormProjectsTree = () => 
102     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
103         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
104     };
105
106 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') => 
107     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
109     };
110
111 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) => 
112     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
113         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
114     };