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 { SearchBarAdvanceFormData, PropertyValues } from '~/models/search-bar';
17 export const searchBarActions = unionize({
18 SET_CURRENT_VIEW: ofType<string>(),
19 OPEN_SEARCH_VIEW: ofType<{}>(),
20 CLOSE_SEARCH_VIEW: ofType<{}>(),
21 SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
22 SET_SEARCH_VALUE: ofType<string>(),
23 SET_SAVED_QUERIES: ofType<string[]>()
26 export type SearchBarActions = UnionOf<typeof searchBarActions>;
28 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
30 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
32 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
34 export const saveRecentQuery = (query: string) =>
35 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
36 services.searchService.saveRecentQuery(query);
39 export const loadRecentQueries = () =>
40 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
41 const recentSearchQueries = services.searchService.getRecentQueries();
42 return recentSearchQueries || [];
45 export const saveQuery = (data: SearchBarAdvanceFormData) =>
46 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
47 if (data.saveQuery && data.searchQuery) {
48 services.searchService.saveQuery(data.searchQuery);
49 dispatch(searchBarActions.SET_SAVED_QUERIES(services.searchService.getSavedQueries()));
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());
76 export const searchData = (searchValue: string) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
79 dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
81 const filters = getFilters('name', searchValue);
82 const { items } = await services.groupsService.contents('', {
87 dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
91 const getFilters = (filterName: string, searchValue: string): string => {
92 return new FilterBuilder()
93 .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
94 .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
95 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
96 .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
97 .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
101 export const initAdvanceFormProjectsTree = () =>
102 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
103 dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
106 export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | string = '') =>
107 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108 dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
111 export const updateAdvanceFormProperties = (propertyValues: PropertyValues) =>
112 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
113 dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));