1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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[]>()
31 export type SearchBarActions = UnionOf<typeof searchBarActions>;
33 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
35 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
37 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
39 export const saveRecentQuery = (query: string) =>
40 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
41 services.searchService.saveRecentQuery(query);
44 export const loadRecentQueries = () =>
45 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
46 const recentSearchQueries = services.searchService.getRecentQueries();
47 return recentSearchQueries || [];
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) {
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 }));
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 }));
66 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
67 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
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 || [];
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));
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));
92 export const closeSearchView = () =>
93 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94 const isOpen = getState().searchBar.open;
96 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
97 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
101 export const navigateToItem = (uuid: string) =>
102 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
103 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
104 dispatch(navigateTo(uuid));
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());
113 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
114 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
116 const filters = getFilters('name', searchValue);
117 const { items } = await services.groupsService.contents('', {
122 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
124 if (currentView !== SearchView.AUTOCOMPLETE) {
125 dispatch(navigateToSearchResults);
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([]));
136 const filters = getFilters('name', searchValue);
137 const { items } = await services.groupsService.contents('', {
142 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
144 dispatch(navigateToSearchResults);
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)
157 export const initAdvanceFormProjectsTree = () =>
158 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
159 dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
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));
167 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
168 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
169 dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));