Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMattiussi committed Mar 11, 2020
1 parent bb5f7f8 commit eade146
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ const useReferenceArrayInputController = ({
// optimization: we fetch selected items only once. When the user selects more items,
// as we already have the past selected items in the store, we don't fetch them.
useEffect(() => {
// Only fetch new ids
const newIdsToFetch = difference(input.value, inputValue.current);
if (newIdsToFetch.length > 0) {
setIdsToFetch(newIdsToFetch);
setIdsToGetFromStore(inputValue.current || []);
}
// Only get from store ids selected and already fetched
const newIdsToGetFromStore = difference(input.value, newIdsToFetch);
//Always change states to avoid keeping previous values no more selected
setIdsToFetch(newIdsToFetch);
setIdsToGetFromStore(newIdsToGetFromStore);

inputValue.current = input.value;
}, [input.value, setIdsToFetch]);

Expand Down Expand Up @@ -105,10 +108,7 @@ const useReferenceArrayInputController = ({
: referenceRecordsFromStore;

// filter out not found references - happens when the dataProvider doesn't guarantee referential integrity
// filter out not selected items - happens when the form value is changed by script (i.e. with a filtering Button)
const finalReferenceRecords = referenceRecords
.filter(Boolean)
.filter(item => input.value.includes(item.id));
const finalReferenceRecords = referenceRecords.filter(Boolean);

const { data: matchingReferences } = useGetMatching(
reference,
Expand Down

0 comments on commit eade146

Please sign in to comment.