Move search call debounce to component
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 14 Mar 2019 11:42:37 +0000 (12:42 +0100)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 14 Mar 2019 11:42:37 +0000 (12:42 +0100)
Feature #14917

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/search-bar/search-bar-actions.ts
src/views-components/search-bar/search-bar-view.tsx

index a513a6bbed380643b65ebcf9c3c3bc44a32ab0b7..bffa337b28640b3d21fd5a006ce7ebf840e8c8bc 100644 (file)
@@ -178,7 +178,6 @@ export const changeData = (searchValue: string) =>
         } else if (searchValuePresent) {
             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
             dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue));
-            debounceStartSearch(dispatch);
         } else {
             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
@@ -199,7 +198,6 @@ export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
         dispatch(navigateToSearchResults);
     };
 
-const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
 
 const startSearch = () =>
     (dispatch: Dispatch, getState: () => RootState) => {
index 6251308d984f74dd31e3531d330c2fe94673a8ee..176ca018b420eff2a8ef6e3ae951d91cacb1e825 100644 (file)
@@ -32,6 +32,7 @@ import {
     SearchBarAdvancedViewActionProps
 } from '~/views-components/search-bar/search-bar-advanced-view';
 import { KEY_CODE_DOWN, KEY_CODE_ESC, KEY_CODE_UP, KEY_ENTER } from "~/common/codes";
+import { debounce } from 'debounce';
 
 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
 
@@ -145,52 +146,73 @@ const handleDropdownClick = (e: React.MouseEvent, props: SearchBarViewProps) =>
 };
 
 export const SearchBarView = withStyles(styles)(
-    (props: SearchBarViewProps) => {
-        const { classes, isPopoverOpen } = props;
-        return (
-            <>
-
-                {isPopoverOpen &&
-                    <Backdrop onClick={props.closeView} />}
-
-                <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
-                    <form onSubmit={props.onSubmit}>
-                        <Input
-                            className={classes.input}
-                            onChange={props.onChange}
-                            placeholder="Search"
-                            value={props.searchValue}
-                            fullWidth={true}
-                            disableUnderline={true}
-                            onClick={e => handleInputClick(e, props)}
-                            onKeyDown={e => handleKeyDown(e, props)}
-                            startAdornment={
-                                <InputAdornment position="start">
-                                    <Tooltip title='Search'>
-                                        <IconButton type="submit">
-                                            <SearchIcon />
-                                        </IconButton>
-                                    </Tooltip>
-                                </InputAdornment>
-                            }
-                            endAdornment={
-                                <InputAdornment position="end">
-                                    <Tooltip title='Advanced search'>
-                                        <IconButton onClick={e => handleDropdownClick(e, props)}>
-                                            <ArrowDropDownIcon />
-                                        </IconButton>
-                                    </Tooltip>
-                                </InputAdornment>
-                            } />
-                    </form>
-                    <div className={classes.view}>
-                        {isPopoverOpen && getView({ ...props })}
-                    </div>
-                </Paper >
-            </>
-        );
-    }
-);
+    class extends React.Component<SearchBarViewProps> {
+
+        debouncedSearch = debounce(() => {
+            this.props.onSearch(this.props.searchValue);
+        }, 1000);
+
+        handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
+            this.debouncedSearch();
+            this.props.onChange(event);
+        }
+
+        handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
+            this.debouncedSearch.clear();
+            this.props.onSubmit(event);
+        }
+
+        componentWillUnmount() {
+            this.debouncedSearch.clear();
+        }
+
+        render() {
+            const { children, ...props } = this.props;
+            const { classes, isPopoverOpen } = this.props;
+            return (
+                <>
+
+                    {isPopoverOpen &&
+                        <Backdrop onClick={props.closeView} />}
+
+                    <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
+                        <form onSubmit={this.handleSubmit}>
+                            <Input
+                                className={classes.input}
+                                onChange={this.handleChange}
+                                placeholder="Search"
+                                value={props.searchValue}
+                                fullWidth={true}
+                                disableUnderline={true}
+                                onClick={e => handleInputClick(e, props)}
+                                onKeyDown={e => handleKeyDown(e, props)}
+                                startAdornment={
+                                    <InputAdornment position="start">
+                                        <Tooltip title='Search'>
+                                            <IconButton type="submit">
+                                                <SearchIcon />
+                                            </IconButton>
+                                        </Tooltip>
+                                    </InputAdornment>
+                                }
+                                endAdornment={
+                                    <InputAdornment position="end">
+                                        <Tooltip title='Advanced search'>
+                                            <IconButton onClick={e => handleDropdownClick(e, props)}>
+                                                <ArrowDropDownIcon />
+                                            </IconButton>
+                                        </Tooltip>
+                                    </InputAdornment>
+                                } />
+                        </form>
+                        <div className={classes.view}>
+                            {isPopoverOpen && getView({ ...props })}
+                        </div>
+                    </Paper >
+                </>
+            );
+        }
+    });
 
 const getView = (props: SearchBarViewProps) => {
     switch (props.currentView) {