-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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] Fix ReferenceArrayInput does not work in Filters #3898
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { useMemo, useState, useEffect, useRef } from 'react'; | ||
import isEqual from 'lodash/isEqual'; | ||
import difference from 'lodash/difference'; | ||
import { Record, Pagination, Sort } from '../../types'; | ||
import { Pagination, Record, Sort } from '../../types'; | ||
import { useGetMany } from '../../dataProvider'; | ||
import { FieldInputProps } from 'react-final-form'; | ||
import useGetMatching from '../../dataProvider/useGetMatching'; | ||
|
@@ -13,7 +13,7 @@ import { getStatusForArrayInput as getDataStatus } from './referenceDataStatus'; | |
* | ||
* @example | ||
* | ||
* const { choices, error, loaded, loading, referenceBasePath } = useReferenceArrayInputController({ | ||
* const { choices, error, loaded, loading } = useReferenceArrayInputController({ | ||
* basePath: 'resource'; | ||
* record: { referenceIds: ['id1', 'id2']}; | ||
* reference: 'reference'; | ||
|
@@ -33,7 +33,6 @@ import { getStatusForArrayInput as getDataStatus } from './referenceDataStatus'; | |
* @return {Object} controllerProps Fetched data and callbacks for the ReferenceArrayInput components | ||
*/ | ||
const useReferenceArrayInputController = ({ | ||
basePath, | ||
filter: defaultFilter, | ||
filterToQuery = defaultFilterToQuery, | ||
input, | ||
|
@@ -115,7 +114,7 @@ const useReferenceArrayInputController = ({ | |
// the component displaying the currently selected records may fail | ||
const finalMatchingReferences = | ||
matchingReferences && matchingReferences.length > 0 | ||
? matchingReferences.concat(finalReferenceRecords) | ||
? mergeReferences(matchingReferences, finalReferenceRecords) | ||
: finalReferenceRecords.length > 0 | ||
? finalReferenceRecords | ||
: matchingReferences; | ||
|
@@ -127,20 +126,31 @@ const useReferenceArrayInputController = ({ | |
translate, | ||
}); | ||
|
||
const referenceBasePath = basePath.replace(resource, reference); // FIXME obviously very weak | ||
return { | ||
choices: dataStatus.choices, | ||
error: dataStatus.error, | ||
loaded, | ||
loading: dataStatus.waiting, | ||
referenceBasePath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is never used by child components. It was not passed in 2.0. I think it comes from a bad copy/paste of ReferenceArrayField during the hooks refactoring. |
||
setFilter, | ||
setPagination, | ||
setSort, | ||
warning: dataStatus.warning, | ||
}; | ||
}; | ||
|
||
// concatenate and deduplicate two lists of records | ||
const mergeReferences = (ref1: Record[], ref2: Record[]): Record[] => { | ||
const res = [...ref1]; | ||
const ids = ref1.map(ref => ref.id); | ||
ref2.forEach(ref => { | ||
if (!ids.includes(ref.id)) { | ||
ids.push(ref.id); | ||
res.push(ref); | ||
} | ||
}); | ||
return res; | ||
}; | ||
|
||
export default useReferenceArrayInputController; | ||
|
||
/** | ||
|
@@ -151,15 +161,13 @@ export default useReferenceArrayInputController; | |
* @property {Object} error the error returned by the dataProvider | ||
* @property {boolean} loading is the reference currently loading | ||
* @property {boolean} loaded has the reference already been loaded | ||
* @property {string} referenceBasePath basePath of the reference | ||
*/ | ||
interface ReferenceArrayInputProps { | ||
choices: Record[]; | ||
error?: any; | ||
warning?: any; | ||
loading: boolean; | ||
loaded: boolean; | ||
referenceBasePath: string; | ||
setFilter: (filter: any) => void; | ||
setPagination: (pagination: Pagination) => void; | ||
setSort: (sort: Sort) => void; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was creating warnings about duplicate keys: selected values that were also in the list of possible choices were displayed twice!