add react-highlight-word and change autocomplete list
[arvados-workbench2.git] / src / store / search-bar / search-bar-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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
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 });
21
22 export type SearchBarActions = UnionOf<typeof searchBarActions>;
23
24 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
25
26 export const searchData = (searchValue: string) => 
27     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
29         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
30         if (searchValue) {
31             const filters = getFilters('name', searchValue);
32             const { items } = await services.groupsService.contents('', {
33                 filters, 
34                 limit: 5,
35                 recursive: true
36             });
37             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
38         }
39     };
40
41
42 const getFilters = (filterName: string, searchValue: string): string => {
43     return new FilterBuilder()
44         .addIsA("uuid", [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS])
45         .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
46         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
47         .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
48         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
49         .getFilters();
50 };