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 { 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';
18 export const searchBarActions = unionize({
19 SET_CURRENT_VIEW: ofType<string>(),
20 OPEN_SEARCH_VIEW: ofType<{}>(),
21 CLOSE_SEARCH_VIEW: ofType<{}>(),
22 SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
23 SET_SEARCH_VALUE: ofType<string>(),
24 SET_SAVED_QUERIES: ofType<string[]>()
27 export type SearchBarActions = UnionOf<typeof searchBarActions>;
29 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
31 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
33 export const saveRecentQuery = (query: string) =>
34 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
35 services.searchService.saveRecentQuery(query);
38 export const loadRecentQueries = () =>
39 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
40 const recentSearchQueries = services.searchService.getRecentQueries();
41 return recentSearchQueries || [];
44 export const saveQuery = (data: SearchBarAdvanceFormData) =>
45 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
46 if (data.saveQuery && data.searchQuery) {
47 services.searchService.saveQuery(data.searchQuery);
48 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
49 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been sucessfully saved', kind: SnackbarKind.SUCCESS }));
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 || [];
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));
68 export const closeSearchView = () =>
69 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
70 const isOpen = getState().searchBar.open;
72 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
73 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
78 export const navigateToItem = (uuid: string) =>
79 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
81 dispatch(navigateTo(uuid));
84 export const searchData = (searchValue: string) =>
85 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 const currentView = getState().searchBar.currentView;
87 if (currentView !== SearchView.AUTOCOMPLETE) {
88 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
90 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
91 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
93 const filters = getFilters('name', searchValue);
94 const { items } = await services.groupsService.contents('', {
99 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
101 dispatch(navigateToSearchResults);
104 export const getFilters = (filterName: string, searchValue: string): string => {
105 return new FilterBuilder()
106 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
107 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
108 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
109 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
110 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)