Merge branch 'main' from arvados-workbench2.git
[arvados.git] / services / workbench2 / src / views-components / search-bar / search-bar.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from 'react-redux';
6 import { RootState } from 'store/store';
7 import { Dispatch } from 'redux';
8 import {
9     goToView,
10     searchData,
11     deleteSavedQuery,
12     loadRecentQueries,
13     openSearchView,
14     closeSearchView,
15     closeAdvanceView,
16     navigateToItem,
17     editSavedQuery,
18     changeData,
19     submitData, moveUp, moveDown, setAdvancedDataFromSearchValue, SEARCH_BAR_ADVANCED_FORM_NAME
20 } from 'store/search-bar/search-bar-actions';
21 import { SearchBarView, SearchBarActionProps, SearchBarDataProps } from 'views-components/search-bar/search-bar-view';
22 import { SearchBarAdvancedFormData } from 'models/search-bar';
23 import { Vocabulary } from 'models/vocabulary';
24
25 const mapStateToProps = ({ searchBar, form }: RootState): SearchBarDataProps => {
26     return {
27         searchValue: searchBar.searchValue,
28         currentView: searchBar.currentView,
29         isPopoverOpen: searchBar.open,
30         searchResults: searchBar.searchResults,
31         selectedItem: searchBar.selectedItem,
32         savedQueries: searchBar.savedQueries,
33         tags: form[SEARCH_BAR_ADVANCED_FORM_NAME],
34         saveQuery: form[SEARCH_BAR_ADVANCED_FORM_NAME] &&
35             form[SEARCH_BAR_ADVANCED_FORM_NAME].values &&
36             form[SEARCH_BAR_ADVANCED_FORM_NAME].values!.saveQuery
37     };
38 };
39
40 const mapDispatchToProps = (dispatch: Dispatch): SearchBarActionProps => ({
41     onSearch: (valueSearch: string) => dispatch<any>(searchData(valueSearch, true)),
42     onChange: (event: React.ChangeEvent<HTMLInputElement>) => dispatch<any>(changeData(event.target.value)),
43     onSetView: (currentView: string) => dispatch(goToView(currentView)),
44     onSubmit: (event: React.FormEvent<HTMLFormElement>) => dispatch<any>(submitData(event)),
45     closeView: () => dispatch<any>(closeSearchView()),
46     closeAdvanceView: () => dispatch<any>(closeAdvanceView()),
47     loadRecentQueries: () => dispatch<any>(loadRecentQueries()),
48     deleteSavedQuery: (id: number) => dispatch<any>(deleteSavedQuery(id)),
49     openSearchView: () => dispatch<any>(openSearchView()),
50     navigateTo: (uuid: string) => dispatch<any>(navigateToItem(uuid)),
51     editSavedQuery: (data: SearchBarAdvancedFormData) => dispatch<any>(editSavedQuery(data)),
52     moveUp: () => dispatch<any>(moveUp()),
53     moveDown: () => dispatch<any>(moveDown()),
54     setAdvancedDataFromSearchValue: (search: string, vocabulary: Vocabulary) => dispatch<any>(setAdvancedDataFromSearchValue(search, vocabulary))
55 });
56
57 export const SearchBar = connect(mapStateToProps, mapDispatchToProps)(SearchBarView);