saving queries to localstorage
[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 } 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
25 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'searchBar' | 'view';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
28     return {
29         container: {
30             position: 'relative',
31             width: '100%',
32             borderRadius: theme.spacing.unit / 4
33         },
34         containerSearchViewOpened: {
35             position: 'relative',
36             width: '100%',
37             borderRadius: `${theme.spacing.unit / 4}px ${theme.spacing.unit / 4}px 0 0`
38         },
39         input: {
40             border: 'none',
41             padding: `0px ${theme.spacing.unit}px`
42         },
43         searchBar: {
44             height: '30px'
45         },
46         view: {
47             position: 'absolute',
48             width: '100%'
49         }
50     };
51 };
52
53 type SearchBarDataProps = {
54     searchValue: string;
55     currentView: string;
56     isPopoverOpen: boolean;
57 } & SearchBarAutocompleteViewDataProps;
58
59 interface SearchBarActionProps {
60     onSearch: (value: string) => any;
61     debounce?: number;
62     onSetView: (currentView: string) => void;
63     openView: () => void;
64     closeView: () => void;
65     saveRecentQuery: (query: string) => void;
66     loadRecentQueries: () => string[];
67     saveQuery: (query: string) => void;
68     loadSavedQueries: () => string[];
69     deleteSavedQuery: (id: number) => void;
70 }
71
72 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
73
74 interface SearchBarState {
75     value: string;
76 }
77
78 interface RenderSavedQueriesProps {
79     text: string | JSX.Element;
80     id: number;
81     deleteSavedQuery: (id: number) => void;
82 }
83
84 interface RenderRecentQueriesProps {
85     text: string | JSX.Element;
86 }
87
88 export const RecentQueriesItem = (props: RenderRecentQueriesProps) => {
89     return <ListItem button>
90         <ListItemText secondary={props.text} />
91     </ListItem>;
92 };
93
94
95 export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
96     return <ListItem button>
97         <ListItemText secondary={props.text} />
98         <ListItemSecondaryAction>
99             <Tooltip title="Remove">
100                 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
101                     <RemoveIcon />
102                 </IconButton>
103             </Tooltip>
104         </ListItemSecondaryAction>
105     </ListItem>;
106 };
107
108 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
109
110 export const SearchBarView = withStyles(styles)(
111     class extends React.Component<SearchBarProps> {
112         state: SearchBarState = {
113             value: ""
114         };
115
116         timeout: number;
117
118         render() {
119             const { classes, currentView, openView, closeView, isPopoverOpen } = this.props;
120             return <ClickAwayListener onClickAway={() => closeView()}>
121                 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
122                     <form onSubmit={this.handleSubmit} className={classes.searchBar}>
123                         <Input
124                             className={classes.input}
125                             onChange={this.handleChange}
126                             placeholder="Search"
127                             value={this.state.value}
128                             fullWidth={true}
129                             disableUnderline={true}
130                             onClick={() => openView()}
131                             endAdornment={
132                                 <InputAdornment position="end">
133                                     <Tooltip title='Search'>
134                                         <IconButton>
135                                             <SearchIcon />
136                                         </IconButton>
137                                     </Tooltip>
138                                 </InputAdornment>
139                             } />
140                     </form>
141                     <div className={classes.view}>
142                         {isPopoverOpen && this.getView(currentView)}
143                     </div>
144                 </Paper >
145             </ClickAwayListener>;
146         }
147
148         componentDidMount() {
149             this.setState({ value: this.props.searchValue });
150         }
151
152         componentWillReceiveProps(nextProps: SearchBarProps) {
153             if (nextProps.searchValue !== this.props.searchValue) {
154                 this.setState({ value: nextProps.searchValue });
155             }
156         }
157
158         componentWillUnmount() {
159             clearTimeout(this.timeout);
160         }
161
162         getView = (currentView: string) => {
163             switch (currentView) {
164                 case SearchView.BASIC:
165                     return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadRecentQueries} savedQueries={this.props.loadSavedQueries} deleteSavedQuery={this.props.deleteSavedQuery}/>;
166                 case SearchView.ADVANCED:
167                     return <SearchBarAdvancedView setView={this.props.onSetView} />;
168                 case SearchView.AUTOCOMPLETE:
169                     return <SearchBarAutocompleteView
170                         searchResults={this.props.searchResults}
171                         searchValue={this.props.searchValue} />;
172                 default:
173                     return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadRecentQueries} savedQueries={this.props.loadSavedQueries} deleteSavedQuery={this.props.deleteSavedQuery}/>;
174             }
175         }
176
177         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
178             event.preventDefault();
179             clearTimeout(this.timeout);
180             this.props.saveRecentQuery(this.state.value);
181             this.props.saveQuery(this.state.value);
182             this.props.onSearch(this.state.value);
183             this.props.loadRecentQueries();
184             this.props.loadSavedQueries();
185         }
186
187         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
188             clearTimeout(this.timeout);
189             this.setState({ value: event.target.value });
190             this.timeout = window.setTimeout(
191                 () => this.props.onSearch(this.state.value),
192                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
193             );
194             if (event.target.value.length > 0) {
195                 this.props.onSetView(SearchView.AUTOCOMPLETE);
196             } else {
197                 this.props.onSetView(SearchView.BASIC);
198             }
199         }
200     }
201 );