code refactoring and improve views
[arvados.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 { SearchView } from '~/store/search-bar/search-bar-reducer';
16 import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action';
17 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 import { initialize } from 'redux-form';
19 import { SearchBarAdvanceFormData, PropertyValues } from '~/models/search-bar';
20
21 export const searchBarActions = unionize({
22     SET_CURRENT_VIEW: ofType<string>(),
23     OPEN_SEARCH_VIEW: ofType<{}>(),
24     CLOSE_SEARCH_VIEW: ofType<{}>(),
25     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
26     SET_SEARCH_VALUE: ofType<string>(),
27     SET_SAVED_QUERIES: ofType<SearchBarAdvanceFormData[]>(),
28     UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>()
29 });
30
31 export type SearchBarActions = UnionOf<typeof searchBarActions>;
32
33 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
34
35 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
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.searchService.saveRecentQuery(query);
42
43
44 export const loadRecentQueries = () =>
45     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
46         const recentSearchQueries = services.searchService.getRecentQueries();
47         return recentSearchQueries || [];
48     };
49
50 // Todo: create ids for particular searchQuery
51 export const saveQuery = (data: SearchBarAdvanceFormData) =>
52     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53         const savedSearchQueries = services.searchService.getSavedQueries();
54         const filteredQuery = savedSearchQueries.find(query => query.searchQuery === data.searchQuery);
55         if (data.saveQuery && data.searchQuery) {
56             if (filteredQuery) {
57                 services.searchService.editSavedQueries(data);
58                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedSearchQueries));
59                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
60             } else {
61                 services.searchService.saveQuery(data);
62                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
63                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
64             }
65         }
66         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
67         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
68     };
69
70 export const deleteSavedQuery = (id: number) =>
71     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
72         services.searchService.deleteSavedQuery(id);
73         const savedSearchQueries = services.searchService.getSavedQueries();
74         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
75         return savedSearchQueries || [];
76     };
77
78 export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
79     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
81         dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery));
82         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
83     };
84
85 export const openSearchView = () =>
86     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
87         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
88         const savedSearchQueries = services.searchService.getSavedQueries();
89         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
90     };
91
92 export const closeSearchView = () =>
93     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94         const isOpen = getState().searchBar.open;
95         if (isOpen) {
96             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
97             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
98         }
99     };
100
101 export const navigateToItem = (uuid: string) =>
102     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
103         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
104         dispatch(navigateTo(uuid));
105     };
106
107 export const searchData = (searchValue: string) =>
108     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109         const currentView = getState().searchBar.currentView;
110         if (currentView !== SearchView.AUTOCOMPLETE) {
111             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
112         }
113         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
114         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
115         if (searchValue) {
116             const filters = getFilters('name', searchValue);
117             const { items } = await services.groupsService.contents('', {
118                 filters,
119                 limit: 5,
120                 recursive: true
121             });
122             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
123         }
124         if (currentView !== SearchView.AUTOCOMPLETE) {
125             dispatch(navigateToSearchResults);
126         }
127         
128     };
129
130 export const searchDataOnEnter = (searchValue: string) =>
131     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
132         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
133         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
134         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
135         if (searchValue) {
136             const filters = getFilters('name', searchValue);
137             const { items } = await services.groupsService.contents('', {
138                 filters,
139                 limit: 5,
140                 recursive: true
141             });
142             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
143         }
144         dispatch(navigateToSearchResults);
145     };
146
147 export const getFilters = (filterName: string, searchValue: string): string => {
148     return new FilterBuilder()
149         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
150         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
151         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
152         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
153         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
154         .getFilters();
155 };
156
157 export const initAdvanceFormProjectsTree = () =>
158     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
159         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
160     };
161
162 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
163     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
164         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
165     };
166
167 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
168     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
169         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
170     };