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';
14 export const searchBarActions = unionize({
15 SET_CURRENT_VIEW: ofType<string>(),
16 OPEN_SEARCH_VIEW: ofType<{}>(),
17 CLOSE_SEARCH_VIEW: ofType<{}>(),
18 SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
19 SET_SEARCH_VALUE: ofType<string>(),
20 SET_SAVED_QUERIES: ofType<string[]>()
23 export type SearchBarActions = UnionOf<typeof searchBarActions>;
25 export interface SearchBarAdvanceFormData {
26 type?: GroupContentsResource;
34 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
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.searchQueriesService.saveRecentQuery(query);
43 export const loadRecentQueries = () =>
44 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
45 const recentSearchQueries = services.searchQueriesService.getRecentQueries();
46 return recentSearchQueries || [];
49 export const saveQuery = (query: string) =>
50 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
51 services.searchQueriesService.saveQuery(query);
52 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchQueriesService.getSavedQueries()));
55 export const deleteSavedQuery = (id: number) =>
56 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
57 services.searchQueriesService.deleteSavedQuery(id);
58 const savedSearchQueries = services.searchQueriesService.getSavedQueries();
59 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchQueriesService.getSavedQueries()));
60 return savedSearchQueries || [];
63 export const searchData = (searchValue: string) =>
64 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
66 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
68 const filters = getFilters('name', searchValue);
69 const { items } = await services.groupsService.contents('', {
74 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
78 const getFilters = (filterName: string, searchValue: string): string => {
79 return new FilterBuilder()
80 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
81 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
82 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
83 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
84 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)