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[]>()
30 export type SearchBarActions = UnionOf<typeof searchBarActions>;
32 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
34 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
36 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
38 export const saveRecentQuery = (query: string) =>
39 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
40 services.searchService.saveRecentQuery(query);
43 export const loadRecentQueries = () =>
44 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
45 const recentSearchQueries = services.searchService.getRecentQueries();
46 return recentSearchQueries || [];
49 export const saveQuery = (data: SearchBarAdvanceFormData) =>
50 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
51 if (data.saveQuery && data.searchQuery) {
52 services.searchService.saveQuery(data);
53 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
54 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', kind: SnackbarKind.SUCCESS }));
58 export const deleteSavedQuery = (id: number) =>
59 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
60 services.searchService.deleteSavedQuery(id);
61 const savedSearchQueries = services.searchService.getSavedQueries();
62 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
63 return savedSearchQueries || [];
66 export const editSavedQuery = (data: SearchBarAdvanceFormData, id: number) =>
67 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
68 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
69 dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
72 export const openSearchView = () =>
73 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
74 dispatch(searchBarActions.OPEN_SEARCH_VIEW());
75 const savedSearchQueries = services.searchService.getSavedQueries();
76 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
79 export const closeSearchView = () =>
80 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
81 const isOpen = getState().searchBar.open;
83 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
84 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
88 export const navigateToItem = (uuid: string) =>
89 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
90 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
91 dispatch(navigateTo(uuid));
94 export const searchData = (searchValue: string) =>
95 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
96 const currentView = getState().searchBar.currentView;
97 if (currentView !== SearchView.AUTOCOMPLETE) {
98 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
100 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
101 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
103 const filters = getFilters('name', searchValue);
104 const { items } = await services.groupsService.contents('', {
109 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
111 dispatch(navigateToSearchResults);
114 export const getFilters = (filterName: string, searchValue: string): string => {
115 return new FilterBuilder()
116 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
117 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
118 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
119 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
120 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
124 export const initAdvanceFormProjectsTree = () =>
125 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
126 dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
129 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
130 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
131 dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
134 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
135 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
136 dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));