X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/47440ead99a61a13bc635630e0f4b07d9322edbf..6234908246415d038d74f9f0b69ab8ea886f9296:/src/views-components/search-bar/search-bar-view.tsx?ds=sidebyside

diff --git a/src/views-components/search-bar/search-bar-view.tsx b/src/views-components/search-bar/search-bar-view.tsx
index 69e3717c..68418ba1 100644
--- a/src/views-components/search-bar/search-bar-view.tsx
+++ b/src/views-components/search-bar/search-bar-view.tsx
@@ -15,44 +15,61 @@ import {
     ClickAwayListener
 } from '@material-ui/core';
 import SearchIcon from '@material-ui/icons/Search';
-import { RemoveIcon } from '~/components/icon/icon';
+import { RemoveIcon, EditSavedQueryIcon } from '~/components/icon/icon';
 import { SearchView } from '~/store/search-bar/search-bar-reducer';
 import { SearchBarBasicView } from '~/views-components/search-bar/search-bar-basic-view';
 import { SearchBarAdvancedView } from '~/views-components/search-bar/search-bar-advanced-view';
 import { SearchBarAutocompleteView, SearchBarAutocompleteViewDataProps } from '~/views-components/search-bar/search-bar-autocomplete-view';
-import { GroupContentsResource } from '~/services/groups-service/groups-service';
+import { ArvadosTheme } from '~/common/custom-theme';
+import { SearchBarAdvanceFormData } from '~/models/search-bar';
 
-type CssRules = 'container' | 'input' | 'searchBar';
+type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
 
-const styles: StyleRulesCallback<CssRules> = theme => {
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
     return {
         container: {
             position: 'relative',
             width: '100%',
-            borderRadius: '0px'
+            borderRadius: theme.spacing.unit / 2
+        },
+        containerSearchViewOpened: {
+            position: 'relative',
+            width: '100%',
+            borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
         },
         input: {
             border: 'none',
             padding: `0px ${theme.spacing.unit}px`
         },
-        searchBar: {
-            height: '30px'
+        view: {
+            position: 'absolute',
+            width: '100%',
+            zIndex: 1
         }
     };
 };
 
 type SearchBarDataProps = {
-    value: string;
+    searchValue: string;
     currentView: string;
-    open: boolean;
+    isPopoverOpen: boolean;
+    savedQueries: SearchBarAdvanceFormData[];
+    tags: any;
 } & SearchBarAutocompleteViewDataProps;
 
 interface SearchBarActionProps {
     onSearch: (value: string) => any;
+    searchDataOnEnter: (value: string) => void;
     debounce?: number;
     onSetView: (currentView: string) => void;
-    openView: () => void;
     closeView: () => void;
+    saveRecentQuery: (query: string) => void;
+    loadRecentQueries: () => string[];
+    saveQuery: (data: SearchBarAdvanceFormData) => void;
+    deleteSavedQuery: (id: number) => void;
+    openSearchView: () => void;
+    navigateTo: (uuid: string) => void;
+    editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
 }
 
 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
@@ -61,23 +78,49 @@ interface SearchBarState {
     value: string;
 }
 
-interface RenderQueriesProps {
+interface RenderRecentQueriesProps {
     text: string;
+    onSearch: (searchValue: string) => void;
 }
 
-export const RenderRecentQueries = (props: RenderQueriesProps) => {
+export const RenderRecentQueries = (props: RenderRecentQueriesProps) => {
     return <ListItem button>
-        <ListItemText secondary={props.text} />
+        <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
     </ListItem>;
 };
 
+interface RenderAutocompleteItemsProps {
+    text: string | JSX.Element;
+    navigateTo: (uuid: string) => void;
+    uuid: string;
+}
 
-export const RenderSavedQueries = (props: RenderQueriesProps) => {
+export const RenderAutocompleteItems = (props: RenderAutocompleteItemsProps) => {
     return <ListItem button>
-        <ListItemText secondary={props.text} />
+        <ListItemText secondary={props.text} onClick={() => props.navigateTo(props.uuid)} />
+    </ListItem>;
+};
+
+interface RenderSavedQueriesProps {
+    text: string;
+    id: number;
+    deleteSavedQuery: (id: number) => void;
+    onSearch: (searchValue: string) => void;
+    editSavedQuery: (data: SearchBarAdvanceFormData, id: number) => void;
+    data: SearchBarAdvanceFormData;
+}
+
+export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
+    return <ListItem button>
+        <ListItemText secondary={props.text} onClick={() => props.onSearch(props.text)} />
         <ListItemSecondaryAction>
+            <Tooltip title="Edit">
+                <IconButton aria-label="Edit" onClick={() => props.editSavedQuery(props.data, props.id)}>
+                    <EditSavedQueryIcon />
+                </IconButton>
+            </Tooltip>
             <Tooltip title="Remove">
-                <IconButton aria-label="Remove">
+                <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
                     <RemoveIcon />
                 </IconButton>
             </Tooltip>
@@ -96,10 +139,10 @@ export const SearchBarView = withStyles(styles)(
         timeout: number;
 
         render() {
-            const { classes, currentView, openView, closeView, open } = this.props;
-            return <ClickAwayListener onClickAway={() => closeView()}>
-                <Paper className={classes.container} >
-                    <form onSubmit={this.handleSubmit} className={classes.searchBar}>
+            const { classes, currentView, openSearchView, closeView, isPopoverOpen } = this.props;
+            return <ClickAwayListener onClickAway={closeView}>
+                <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
+                    <form onSubmit={this.handleSubmit}>
                         <Input
                             className={classes.input}
                             onChange={this.handleChange}
@@ -107,7 +150,7 @@ export const SearchBarView = withStyles(styles)(
                             value={this.state.value}
                             fullWidth={true}
                             disableUnderline={true}
-                            onClick={() => openView()}
+                            onClick={openSearchView}
                             endAdornment={
                                 <InputAdornment position="end">
                                     <Tooltip title='Search'>
@@ -117,19 +160,21 @@ export const SearchBarView = withStyles(styles)(
                                     </Tooltip>
                                 </InputAdornment>
                             } />
-                        {open && this.getView(currentView)}
                     </form>
+                    <div className={classes.view}>
+                        {isPopoverOpen && this.getView(currentView)}
+                    </div>
                 </Paper >
             </ClickAwayListener>;
         }
 
         componentDidMount() {
-            this.setState({ value: this.props.value });
+            this.setState({ value: this.props.searchValue });
         }
 
         componentWillReceiveProps(nextProps: SearchBarProps) {
-            if (nextProps.value !== this.props.value) {
-                this.setState({ value: nextProps.value });
+            if (nextProps.searchValue !== this.props.searchValue) {
+                this.setState({ value: nextProps.searchValue });
             }
         }
 
@@ -138,22 +183,28 @@ export const SearchBarView = withStyles(styles)(
         }
 
         getView = (currentView: string) => {
+            const { onSetView, loadRecentQueries, savedQueries, deleteSavedQuery, searchValue, searchResults, saveQuery, onSearch, navigateTo, editSavedQuery, tags } = this.props;
             switch (currentView) {
                 case SearchView.BASIC:
-                    return <SearchBarBasicView setView={this.props.onSetView} />;
+                    return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
                 case SearchView.ADVANCED:
-                    return <SearchBarAdvancedView setView={this.props.onSetView} />;
+                    return <SearchBarAdvancedView setView={onSetView} saveQuery={saveQuery} tags={tags} />;
                 case SearchView.AUTOCOMPLETE:
-                    return <SearchBarAutocompleteView searchResults={this.props.searchResults} />;
+                    return <SearchBarAutocompleteView
+                        navigateTo={navigateTo}
+                        searchResults={searchResults}
+                        searchValue={searchValue} />;
                 default:
-                    return <SearchBarBasicView setView={this.props.onSetView} />;
+                    return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} onSearch={onSearch} editSavedQuery={editSavedQuery} />;
             }
         }
 
         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
             event.preventDefault();
             clearTimeout(this.timeout);
-            this.props.onSearch(this.state.value);
+            this.props.saveRecentQuery(this.state.value);
+            this.props.searchDataOnEnter(this.state.value);
+            this.props.loadRecentQueries();
         }
 
         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
