Skip to content

Commit

Permalink
[6.x] Add debounce to visualize search filter so we don't send a requ…
Browse files Browse the repository at this point in the history
…est on every character press (#15039)

* Fixes #14908 (#15003)

* Cancel debounce when component unmounts

* remove trailing whitespace

* Remove more trailing whitespace
  • Loading branch information
stacey-gammon authored Nov 20, 2017
1 parent 9a0b6b5 commit 82bcf81
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';

import { SortableProperties } from 'ui_framework/services';
import { Pager } from 'ui/pager';
Expand Down Expand Up @@ -48,6 +49,23 @@ export class VisualizeListingTable extends Component {
);
this.items = [];
this.pager = new Pager(this.items.length, 20, 1);

this.debouncedFetch = _.debounce(filter => {
this.props.fetchItems(filter)
.then(items => {
this.setState({
isFetchingItems: false,
selectedRowIds: [],
filter,
});
this.items = items;
this.calculateItemsOnPage();
});
}, 200);
}

componentWillUnmount() {
this.debouncedFetch.cancel();
}

calculateItemsOnPage = () => {
Expand Down Expand Up @@ -75,18 +93,8 @@ export class VisualizeListingTable extends Component {
};

fetchItems = (filter) => {
this.setState({ isFetchingItems: true });

this.props.fetchItems(filter)
.then(items => {
this.setState({
isFetchingItems: false,
selectedRowIds: [],
filter,
});
this.items = items;
this.calculateItemsOnPage();
});
this.setState({ isFetchingItems: true, filter });
this.debouncedFetch(filter);
};

componentDidMount() {
Expand Down

0 comments on commit 82bcf81

Please sign in to comment.