Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Remove duplicate API call in ReferenceArrayInput #3252

Merged
merged 6 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,44 @@ describe('<ReferenceArrayInputController />', () => {
assert.equal(crudGetMany.mock.calls.length, 2);
});

it('should call crudGetMany when input value changes only with the additional input values', () => {
const crudGetMany = jest.fn();
const wrapper = shallow(
<ReferenceArrayInputController
{...defaultProps}
input={{ value: [5] }}
allowEmpty
crudGetMany={crudGetMany}
/>
);
expect(
crudGetMany.mock.calls[crudGetMany.mock.calls.length - 1]
).toEqual([defaultProps.reference, [5]]);
wrapper.setProps({ input: { value: [5, 6] } });
expect(
crudGetMany.mock.calls[crudGetMany.mock.calls.length - 1]
).toEqual([defaultProps.reference, [6]]);
});

it('should call crudGetMany with empty list when already fetched input value changes', () => {
fargito marked this conversation as resolved.
Show resolved Hide resolved
const crudGetMany = jest.fn();
const wrapper = shallow(
<ReferenceArrayInputController
{...defaultProps}
input={{ value: [5, 6] }}
allowEmpty
crudGetMany={crudGetMany}
/>
);
expect(
crudGetMany.mock.calls[crudGetMany.mock.calls.length - 1]
).toEqual([defaultProps.reference, [5, 6]]);
wrapper.setProps({ input: { value: [6] } });
expect(
crudGetMany.mock.calls[crudGetMany.mock.calls.length - 1]
).toEqual([defaultProps.reference, []]);
});

it('should call crudGetOne and crudGetMatching when record changes', () => {
const crudGetMany = jest.fn();
const crudGetMatching = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import debounce from 'lodash/debounce';
import compose from 'recompose/compose';
import { createSelector } from 'reselect';
import isEqual from 'lodash/isEqual';
import difference from 'lodash/difference';
import { WrappedFieldInputProps } from 'redux-form';

import {
Expand Down Expand Up @@ -164,7 +165,8 @@ export class UnconnectedReferenceArrayInputController extends Component<
}

componentDidMount() {
this.fetchReferencesAndOptions(this.props);
fargito marked this conversation as resolved.
Show resolved Hide resolved
this.fetchReferences(this.props, true);
this.fetchOptions(this.props);
}

componentWillReceiveProps(nextProps: Props & EnhancedProps) {
Expand Down Expand Up @@ -223,7 +225,7 @@ export class UnconnectedReferenceArrayInputController extends Component<
}
};

fetchReferences = (props = this.props) => {
fetchReferences = (props = this.props, isInitialCall = false) => {
const { crudGetMany, input, reference } = props;
const ids = input.value;
if (ids) {
Expand All @@ -232,7 +234,10 @@ export class UnconnectedReferenceArrayInputController extends Component<
'The value of ReferenceArrayInput should be an array'
);
}
crudGetMany(reference, ids);
const idsToFetch = isInitialCall
fargito marked this conversation as resolved.
Show resolved Hide resolved
? ids
: difference(ids, this.props.input.value);
crudGetMany(reference, idsToFetch);
fargito marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand All @@ -255,9 +260,9 @@ export class UnconnectedReferenceArrayInputController extends Component<
);
};

fetchReferencesAndOptions(nextProps) {
this.fetchReferences(nextProps);
this.fetchOptions(nextProps);
fetchReferencesAndOptions(props = this.props) {
this.fetchReferences(props);
this.fetchOptions(props);
}

render() {
Expand Down