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';
15 export const searchBarActions = unionize({
16 SET_CURRENT_VIEW: ofType<string>(),
17 OPEN_SEARCH_VIEW: ofType<{}>(),
18 CLOSE_SEARCH_VIEW: ofType<{}>(),
19 SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
20 SET_SEARCH_VALUE: ofType<string>(),
21 SET_SAVED_QUERIES: ofType<string[]>()
24 export type SearchBarActions = UnionOf<typeof searchBarActions>;
26 export interface SearchBarAdvanceFormData {
27 type?: GroupContentsResource;
37 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
39 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
41 export const saveRecentQuery = (query: string) =>
42 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
43 services.searchService.saveRecentQuery(query);
46 export const loadRecentQueries = () =>
47 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
48 const recentSearchQueries = services.searchService.getRecentQueries();
49 return recentSearchQueries || [];
52 export const saveQuery = (data: SearchBarAdvanceFormData) =>
53 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
54 if (data.saveQuery && data.searchQuery) {
55 services.searchService.saveQuery(data.searchQuery);
56 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
60 export const deleteSavedQuery = (id: number) =>
61 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
62 services.searchService.deleteSavedQuery(id);
63 const savedSearchQueries = services.searchService.getSavedQueries();
64 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
65 return savedSearchQueries || [];
68 export const openSearchView = () =>
69 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
70 dispatch(searchBarActions.OPEN_SEARCH_VIEW());
71 dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
72 const savedSearchQueries = services.searchService.getSavedQueries();
73 dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
77 export const searchData = (searchValue: string) =>
78 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
79 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
80 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
82 const filters = getFilters('name', searchValue);
83 const { items } = await services.groupsService.contents('', {
88 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
92 const getFilters = (filterName: string, searchValue: string): string => {
93 return new FilterBuilder()
94 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
95 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
96 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
97 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
98 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)