saved to local-storage
[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 import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action';
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
16 import { SearchBarAdvanceFormData } from '~/models/search-bar';
17 import { initialize } from 'redux-form';
18
19 export const searchBarActions = unionize({
20     SET_CURRENT_VIEW: ofType<string>(),
21     OPEN_SEARCH_VIEW: ofType<{}>(),
22     CLOSE_SEARCH_VIEW: ofType<{}>(),
23     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
24     SET_SEARCH_VALUE: ofType<string>(),
25     SET_SAVED_QUERIES: ofType<SearchBarAdvanceFormData[]>()
26 });
27
28 export type SearchBarActions = UnionOf<typeof searchBarActions>;
29
30 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
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);
49             dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
50             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', kind: SnackbarKind.SUCCESS }));
51         }
52     };
53
54 export const deleteSavedQuery = (id: number) =>
55     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
56         services.searchService.deleteSavedQuery(id);
57         const savedSearchQueries = services.searchService.getSavedQueries();
58         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
59         return savedSearchQueries || [];
60     };
61
62 export const editSavedQuery = (data: SearchBarAdvanceFormData, id: number) =>
63     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
64         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
65         const initialData: SearchBarAdvanceFormData = {
66             type: data.type,
67             cluster: data.cluster,
68             project: data.project,
69             inTrash: data.inTrash,
70             dateFrom: data.dateFrom,
71             dateTo: data.dateTo,
72             saveQuery: data.saveQuery,
73             searchQuery: data.searchQuery
74         };
75         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, initialData));
76     };
77
78 export const openSearchView = () =>
79     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
81         const savedSearchQueries = services.searchService.getSavedQueries();
82         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
83     };
84
85 export const closeSearchView = () =>
86     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
87         const isOpen = getState().searchBar.open;
88         if (isOpen) {
89             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
90             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
91         }
92     };
93
94 export const navigateToItem = (uuid: string) =>
95     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
96         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
97         dispatch(navigateTo(uuid));
98     };
99
100 export const searchData = (searchValue: string) =>
101     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
102         const currentView = getState().searchBar.currentView;
103         if (currentView !== SearchView.AUTOCOMPLETE) {
104             dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
105         }
106         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
107         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
108         if (searchValue) {
109             const filters = getFilters('name', searchValue);
110             const { items } = await services.groupsService.contents('', {
111                 filters,
112                 limit: 5,
113                 recursive: true
114             });
115             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
116         }
117         dispatch(navigateToSearchResults);
118     };
119
120 export const getFilters = (filterName: string, searchValue: string): string => {
121     return new FilterBuilder()
122         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
123         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
124         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
125         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
126         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
127         .getFilters();
128 };