-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename useReference to useReferenceField and add more generic useRefe…
…rence
- Loading branch information
1 parent
47e47a6
commit fe9342a
Showing
6 changed files
with
100 additions
and
62 deletions.
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
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,15 +1,15 @@ | ||
import ReferenceArrayFieldController from './ReferenceArrayFieldController'; | ||
import ReferenceFieldController from './ReferenceFieldController'; | ||
import ReferenceManyFieldController from './ReferenceManyFieldController'; | ||
import useReference from './useReference'; | ||
import useReferenceField from './useReferenceField'; | ||
import useReferenceArray from './useReferenceArray'; | ||
import useReferenceMany from './useReferenceMany'; | ||
|
||
export { | ||
useReferenceArray, | ||
ReferenceArrayFieldController, | ||
ReferenceFieldController, | ||
useReference, | ||
useReferenceField, | ||
useReferenceMany, | ||
ReferenceManyFieldController, | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useEffect } from 'react'; | ||
// @ts-ignore | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { crudGetManyAccumulate } from '../actions'; | ||
import { Record } from '../types'; | ||
import { getReferenceResource } from '../reducer'; | ||
import { createSelector } from 'reselect'; | ||
|
||
interface Option { | ||
id: string; | ||
reference: string; | ||
allowEmpty?: boolean; | ||
} | ||
|
||
export interface UseReferenceProps { | ||
isLoading: boolean; | ||
referenceRecord: Record; | ||
} | ||
|
||
/** | ||
* @typedef ReferenceProps | ||
* @type {Object} | ||
* @property {boolean} isLoading: boolean indicating if the reference has loaded | ||
* @property {Object} referenceRecord: the referenced record. | ||
*/ | ||
|
||
/** | ||
* Fetch reference record, and return it when avaliable | ||
* | ||
* The reference prop sould be the name of one of the <Resource> components | ||
* added as <Admin> child. | ||
* | ||
* @example | ||
* | ||
* const { isLoading, referenceRecord } = useReference({ | ||
* id: 7, | ||
* reference: 'users', | ||
* }); | ||
* | ||
* @param {Object} option | ||
* @param {boolean} option.allowEmpty do we allow for no referenced record (default to false) | ||
* @param {string} option.reference The linked resource name | ||
* @param {string} option.id The id of the reference | ||
* | ||
* @returns {ReferenceProps} The reference record | ||
*/ | ||
export const useReference = ({ | ||
allowEmpty = false, | ||
reference, | ||
id, | ||
}: Option): UseReferenceProps => { | ||
const referenceRecord = useSelector(getReferenceRecord({ id, reference })); | ||
const dispatch = useDispatch(); | ||
useEffect(() => { | ||
if (id !== null && typeof id !== 'undefined') { | ||
dispatch(crudGetManyAccumulate(reference, [id])); | ||
} | ||
}, [id, reference]); | ||
|
||
return { | ||
isLoading: !referenceRecord && !allowEmpty, | ||
referenceRecord, | ||
}; | ||
}; | ||
|
||
const selectedReferenceSelector = createSelector( | ||
[getReferenceResource, (_, props) => props.id], | ||
(referenceState, id) => referenceState && referenceState.data[id] | ||
); | ||
|
||
const getReferenceRecord = props => state => | ||
selectedReferenceSelector(state, props); | ||
|
||
export default useReference; |
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