Merge branch '14277-search-view-editing-saved-queries'
[arvados-workbench2.git] / src / views-components / search-bar / search-bar-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     IconButton,
8     Paper,
9     StyleRulesCallback,
10     withStyles,
11     WithStyles,
12     Tooltip,
13     InputAdornment, Input,
14     ListItem, ListItemText, ListItemSecondaryAction,
15     ClickAwayListener
16 } from '@material-ui/core';
17 import SearchIcon from '@material-ui/icons/Search';
18 import { RemoveIcon, EditSavedQueryIcon } from '~/components/icon/icon';
19 import { SearchView } from '~/store/search-bar/search-bar-reducer';
20 import { SearchBarBasicView } from '~/views-components/search-bar/search-bar-basic-view';
21 import { SearchBarAdvancedView } from '~/views-components/search-bar/search-bar-advanced-view';
22 import { SearchBarAutocompleteView, SearchBarAutocompleteViewDataProps } from '~/views-components/search-bar/search-bar-autocomplete-view';
23 import { ArvadosTheme } from '~/common/custom-theme';
24 import { SearchBarAdvanceFormData } from '~/models/search-bar';
25
26 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
27
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
29     return {
30         container: {
31             position: 'relative',
32             width: '100%',
33             borderRadius: theme.spacing.unit / 2
34         },
35         containerSearchViewOpened: {
36             position: 'relative',
37             width: '100%',
38             borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
39         },
40         input: {
41             border: 'none',
42             padding: `0px ${theme.spacing.unit}px`
43         },
44         view: {
45             position: 'absolute',
46             width: '100%',
47             zIndex: 1
48         }
49     };
50 };
51
52 type SearchBarDataProps = {
53     searchValue: string;
54     currentView: string;
55     isPopoverOpen: boolean;
56     savedQueries: SearchBarAdvanceFormData[];
57 } & SearchBarAutocompleteViewDataProps;
58
59 interface SearchBarActionProps {
60     onSearch: (value: string) => any;
61     searchDataOnEnter: (value: string) => void;
62     debounce?: number;
63     onSetView: (currentView: string) => void;
64     closeView: () => void;
65     saveRecentQuery: (query: string) => void;
66     loadRecentQueries: () => string[];
67     saveQuery: (data: SearchBarAdvanceFormData) => void;
68     deleteSavedQuery: (id: number) => void;
69     openSearchView: () => void;
70     navigateTo: (uuid: string) => void;
71     editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
72 }
73
74 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
75
76 interface SearchBarState {
77     value: string;
78 }
79
80 interface RenderRecentQueriesProps {
81     text: string;
82     onSearch: (searchValue: string) => void;
83 }
84
85 export const RenderRecentQueries = (props: RenderRecentQueriesProps) => {
86     return <ListItem button>
87         <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
88     </ListItem>;
89 };
90
91 interface RenderAutocompleteItemsProps {
92     text: string | JSX.Element;
93     navigateTo: (uuid: string) => void;
94     uuid: string;
95 }
96
97 export const RenderAutocompleteItems = (props: RenderAutocompleteItemsProps) => {
98     return <ListItem button>
99         <ListItemText secondary={props.text} onClick={() => props.navigateTo(props.uuid)} />
100     </ListItem>;
101 };
102
103 interface RenderSavedQueriesProps {
104     text: string;
105     id: number;
106     deleteSavedQuery: (id: number) => void;
107     onSearch: (searchValue: string) => void;
108     editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
109     data: SearchBarAdvanceFormData;
110 }
111
112 export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
113     return <ListItem button>
114         <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
115         <ListItemSecondaryAction>
116             <Tooltip title="Edit">
117                 <IconButton aria-label="Edit" onClick={() => props.editSavedQuery(props.data, props.id)}>
118                     <EditSavedQueryIcon />
119                 </IconButton>
120             </Tooltip>
121             <Tooltip title="Remove">
122                 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
123                     <RemoveIcon />
124                 </IconButton>
125             </Tooltip>
126         </ListItemSecondaryAction>
127     </ListItem>;
128 };
129
130 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
131
132 export const SearchBarView = withStyles(styles)(
133     class extends React.Component<SearchBarProps> {
134         state: SearchBarState = {
135             value: ""
136         };
137
138         timeout: number;
139
140         render() {
141             const { classes, currentView, openSearchView, closeView, isPopoverOpen } = this.props;
142             return <ClickAwayListener onClickAway={closeView}>
143                 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
144                     <form onSubmit={this.handleSubmit}>
145                         <Input
146                             className={classes.input}
147                             onChange={this.handleChange}
148                             placeholder="Search"
149                             value={this.state.value}
150                             fullWidth={true}
151                             disableUnderline={true}
152                             onClick={openSearchView}
153                             endAdornment={
154                                 <InputAdornment position="end">
155                                     <Tooltip title='Search'>
156                                         <IconButton>
157                                             <SearchIcon />
158                                         </IconButton>
159                                     </Tooltip>
160                                 </InputAdornment>
161                             } />
162                     </form>
163                     <div className={classes.view}>
164                         {isPopoverOpen && this.getView(currentView)}
165                     </div>
166                 </Paper >
167             </ClickAwayListener>;
168         }
169
170         componentDidMount() {
171             this.setState({ value: this.props.searchValue });
172         }
173
174         componentWillReceiveProps(nextProps: SearchBarProps) {
175             if (nextProps.searchValue !== this.props.searchValue) {
176                 this.setState({ value: nextProps.searchValue });
177             }
178         }
179
180         componentWillUnmount() {
181             clearTimeout(this.timeout);
182         }
183
184         getView = (currentView: string) => {
185             const { onSetView, loadRecentQueries, savedQueries, deleteSavedQuery, searchValue, searchResults, saveQuery, onSearch, navigateTo, editSavedQuery } = this.props;
186             switch (currentView) {
187                 case SearchView.BASIC:
188                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
189                 case SearchView.ADVANCED:
190                     return <SearchBarAdvancedView setView={onSetView} saveQuery={saveQuery} />;
191                 case SearchView.AUTOCOMPLETE:
192                     return <SearchBarAutocompleteView
193                         navigateTo={navigateTo}
194                         searchResults={searchResults}
195                         searchValue={searchValue} />;
196                 default:
197                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
198             }
199         }
200
201         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
202             event.preventDefault();
203             clearTimeout(this.timeout);
204             this.props.saveRecentQuery(this.state.value);
205             this.props.searchDataOnEnter(this.state.value);
206             this.props.loadRecentQueries();
207         }
208
209         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
210             clearTimeout(this.timeout);
211             this.setState({ value: event.target.value });
212             this.timeout = window.setTimeout(
213                 () => this.props.onSearch(this.state.value),
214                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
215             );
216             if (event.target.value.length > 0) {
217                 this.props.onSetView(SearchView.AUTOCOMPLETE);
218             } else {
219                 this.props.onSetView(SearchView.BASIC);
220             }
221         }
222     }
223 );