Skip to content

Commit

Permalink
Merge pull request #124 from creative-commoners/pulls/2.2/ready-or-no…
Browse files Browse the repository at this point in the history
…t-here-i-load

FIX Adding a debounce to fetching lazy loaded tags with tag field
  • Loading branch information
robbieaverill authored Dec 4, 2018
2 parents ada0ec2 + 836f5ff commit feff8b5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ framework
node_modules/
/**/*.js.map
/**/*.css.map
yarn-error.log
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion client/src/components/TagField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { Component, PropTypes } from 'react';
import Select from 'react-select';
import fetch from 'isomorphic-fetch';
import url from 'url';
// eslint-disable-next-line import/no-extraneous-dependencies
import debounce from 'lodash/debounce';

class TagField extends Component {
constructor(props) {
Expand All @@ -13,6 +15,7 @@ class TagField extends Component {

this.onChange = this.onChange.bind(this);
this.getOptions = this.getOptions.bind(this);
this.fetchOptions = debounce(this.fetchOptions, 500);
}

onChange(value) {
Expand All @@ -26,7 +29,7 @@ class TagField extends Component {
}

getOptions(input) {
const { lazyLoad, options, optionUrl, labelKey, valueKey } = this.props;
const { lazyLoad, options } = this.props;

if (!lazyLoad) {
return Promise.resolve({ options });
Expand All @@ -36,6 +39,11 @@ class TagField extends Component {
return Promise.resolve({ options: [] });
}

return this.fetchOptions(input);
}

fetchOptions(input) {
const { optionUrl, labelKey, valueKey } = this.props;
const fetchURL = url.parse(optionUrl, true);
fetchURL.query.term = input;

Expand Down
8 changes: 6 additions & 2 deletions client/src/components/tests/TagField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ describe('TagField', () => {
}));
});

it('should fetch the URL for results', () => {
it('should fetch the URL for results', done => {
wrapper.instance().getOptions('a');
expect(fetch).toBeCalledWith('localhost/some-fetch-url?term=a', expect.anything());

setTimeout(() => {
expect(fetch).toBeCalledWith('localhost/some-fetch-url?term=a', expect.anything());
done();
}, 500);
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">=6.x"
},
"scripts": {
"build": "yarn && yarn lint && NODE_ENV=production webpack -p --bail --progress",
"build": "yarn && yarn lint && yarn test && NODE_ENV=production webpack -p --bail --progress",
"dev": "NODE_ENV=development webpack --progress",
"watch": "NODE_ENV=development webpack --watch --progress",
"css": "WEBPACK_CHILD=css npm run build",
Expand Down Expand Up @@ -72,6 +72,7 @@
"classnames": "^2.2.5",
"isomorphic-fetch": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz",
"jquery": "^3.1.1",
"lodash.debounce": "^4.0.8",
"react": "15.3.1",
"react-dom": "15.3.1",
"react-select": "^1.0.0-rc.5",
Expand Down

0 comments on commit feff8b5

Please sign in to comment.