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>()
22 export type SearchBarActions = UnionOf<typeof searchBarActions>;
24 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
26 export const saveRecentQuery = (query: string) =>
27 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
28 services.searchQueriesService.saveRecentQuery(query);
31 export const loadRecentQueries = () =>
32 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
33 const recentSearchQueries = services.searchQueriesService.getRecentQueries();
34 return recentSearchQueries || [];
37 export const searchData = (searchValue: string) =>
38 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
40 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
42 const filters = getFilters('name', searchValue);
43 const { items } = await services.groupsService.contents('', {
48 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
52 const getFilters = (filterName: string, searchValue: string): string => {
53 return new FilterBuilder()
54 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
55 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
56 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
57 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
58 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)