searching on text
[arvados.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 } 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 '~/store/search-bar/search-bar-actions';
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: string[];
57 } & SearchBarAutocompleteViewDataProps;
58
59 interface SearchBarActionProps {
60     onSearch: (value: string) => any;
61     debounce?: number;
62     onSetView: (currentView: string) => void;
63     closeView: () => void;
64     saveRecentQuery: (query: string) => void;
65     loadRecentQueries: () => string[];
66     saveQuery: (data: SearchBarAdvanceFormData) => void;
67     deleteSavedQuery: (id: number) => void;
68     openSearchView: () => void;
69     navigateTo: (uuid: string) => void;
70 }
71
72 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
73
74 interface SearchBarState {
75     value: string;
76 }
77
78
79
80 interface RenderRecentQueriesProps {
81     text: string | JSX.Element;
82     onSearch: (searchValue: string | JSX.Element) => 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 | JSX.Element;
105     id: number;
106     deleteSavedQuery: (id: number) => void;
107     onSearch: (searchValue: string | JSX.Element) => void;
108 }
109
110 export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
111     return <ListItem button>
112         <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
113         <ListItemSecondaryAction>
114             <Tooltip title="Remove">
115                 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
116                     <RemoveIcon />
117                 </IconButton>
118             </Tooltip>
119         </ListItemSecondaryAction>
120     </ListItem>;
121 };
122
123 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
124
125 export const SearchBarView = withStyles(styles)(
126     class extends React.Component<SearchBarProps> {
127         state: SearchBarState = {
128             value: ""
129         };
130
131         timeout: number;
132
133         render() {
134             const { classes, currentView, openSearchView, closeView, isPopoverOpen } = this.props;
135             return <ClickAwayListener onClickAway={closeView}>
136                 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
137                     <form onSubmit={this.handleSubmit}>
138                         <Input
139                             className={classes.input}
140                             onChange={this.handleChange}
141                             placeholder="Search"
142                             value={this.state.value}
143                             fullWidth={true}
144                             disableUnderline={true}
145                             onClick={openSearchView}
146                             endAdornment={
147                                 <InputAdornment position="end">
148                                     <Tooltip title='Search'>
149                                         <IconButton>
150                                             <SearchIcon />
151                                         </IconButton>
152                                     </Tooltip>
153                                 </InputAdornment>
154                             } />
155                     </form>
156                     <div className={classes.view}>
157                         {isPopoverOpen && this.getView(currentView)}
158                     </div>
159                 </Paper >
160             </ClickAwayListener>;
161         }
162
163         componentDidMount() {
164             this.setState({ value: this.props.searchValue });
165         }
166
167         componentWillReceiveProps(nextProps: SearchBarProps) {
168             if (nextProps.searchValue !== this.props.searchValue) {
169                 this.setState({ value: nextProps.searchValue });
170             }
171         }
172
173         componentWillUnmount() {
174             clearTimeout(this.timeout);
175         }
176
177         getView = (currentView: string) => {
178             const { onSetView, loadRecentQueries, savedQueries, deleteSavedQuery, searchValue, searchResults, saveQuery, onSearch, navigateTo } = this.props;
179             switch (currentView) {
180                 case SearchView.BASIC:
181                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} />;
182                 case SearchView.ADVANCED:
183                     return <SearchBarAdvancedView setView={onSetView} saveQuery={saveQuery} />;
184                 case SearchView.AUTOCOMPLETE:
185                     return <SearchBarAutocompleteView
186                         navigateTo={navigateTo}
187                         searchResults={searchResults}
188                         searchValue={searchValue} />;
189                 default:
190                     return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} />;
191             }
192         }
193
194         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
195             event.preventDefault();
196             clearTimeout(this.timeout);
197             this.props.saveRecentQuery(this.state.value);
198             this.props.onSearch(this.state.value);
199             this.props.loadRecentQueries();
200         }
201
202         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
203             clearTimeout(this.timeout);
204             this.setState({ value: event.target.value });
205             this.timeout = window.setTimeout(
206                 () => this.props.onSearch(this.state.value),
207                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
208             );
209             if (event.target.value.length > 0) {
210                 this.props.onSetView(SearchView.AUTOCOMPLETE);
211             } else {
212                 this.props.onSetView(SearchView.BASIC);
213             }
214         }
215     }
216 );