-
-
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
[WIP] add useOptimisticQuery hook #3248
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,14 @@ import { ReactNode, useEffect, useCallback } from 'react'; | |
import { useSelector, useDispatch } from 'react-redux'; | ||
import { reset as resetForm } from 'redux-form'; | ||
import inflection from 'inflection'; | ||
import { crudGetOne, crudUpdate, startUndoable } from '../actions'; | ||
|
||
import { crudUpdate, startUndoable } from '../actions'; | ||
import { REDUX_FORM_NAME } from '../form'; | ||
import { useCheckMinimumRequiredProps } from './checkMinimumRequiredProps'; | ||
import { Translate, Record, Identifier, ReduxState } from '../types'; | ||
import { RedirectionSideEffect } from '../sideEffect'; | ||
import { useTranslate } from '../i18n'; | ||
import useGetOne from './useGetOne'; | ||
|
||
interface ChildrenFuncParams { | ||
isLoading: boolean; | ||
|
@@ -85,23 +87,29 @@ const EditController = (props: Props) => { | |
|
||
const { basePath, children, id, resource, undoable } = props; | ||
|
||
const record = useSelector((state: ReduxState) => | ||
state.admin.resources[props.resource] | ||
? state.admin.resources[props.resource].data[props.id] | ||
: null | ||
); | ||
|
||
const isLoading = useSelector( | ||
(state: ReduxState) => state.admin.loading > 0 | ||
); | ||
|
||
const version = useSelector( | ||
(state: ReduxState) => state.admin.ui.viewVersion | ||
); | ||
|
||
const { data: record, loading } = useGetOne( | ||
resource, | ||
id, | ||
{ | ||
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. now it's the controller who decides the side effects, not the action creator |
||
basePath, | ||
onFailure: { | ||
notification: { | ||
body: 'ra.notification.item_doesnt_exist', | ||
level: 'warning', | ||
}, | ||
redirectTo: 'list', | ||
refresh: true, | ||
}, | ||
}, | ||
version | ||
); | ||
|
||
useEffect(() => { | ||
dispatch(resetForm(REDUX_FORM_NAME)); | ||
dispatch(crudGetOne(resource, id, basePath)); | ||
}, [resource, id, basePath, version]); | ||
|
||
if (!children) { | ||
|
@@ -139,7 +147,7 @@ const EditController = (props: Props) => { | |
); | ||
|
||
return children({ | ||
isLoading, | ||
isLoading: loading, | ||
defaultTitle, | ||
save, | ||
resource, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { crudGetOne } from '../actions'; | ||
import { ReduxState, Record, Identifier } from '../types'; | ||
import useOptimisticQuery from './useOptimisticQuery'; | ||
|
||
export interface GetOneResponse { | ||
data?: Record; | ||
loading: boolean; | ||
loaded: boolean; | ||
error?: any; | ||
} | ||
|
||
/** | ||
* @typedef GetOneResponse | ||
* @type {Object} | ||
* @property {Object} data | ||
* @property {boolean} loading | ||
* @property {boolean} loaded | ||
* @property {Object} error | ||
*/ | ||
|
||
/** | ||
* Fetches a record by resource name and id and returns that record. | ||
* | ||
* Optimistically loads the record from the store if it was already fetched before. | ||
* | ||
* @example const { data } = useGetOne('posts', 123); | ||
* | ||
* @param {string} resource The resource name, e.g. "posts" | ||
* @param {string|int} id the resource identifier, e.g. 123 | ||
* @param {object} meta the action meta (including side effects) | ||
* @param {number} version An optional integer used to force fetching the record again | ||
* | ||
* @returns {GetOneResponse} An object with the shape { data, error, loading, loaded } | ||
*/ | ||
const useGetOne = ( | ||
resource: string, | ||
id: Identifier, | ||
meta: any = {}, | ||
version: number | ||
): GetOneResponse => { | ||
return useOptimisticQuery( | ||
crudGetOne(resource, id, meta), | ||
(state: ReduxState) => | ||
state.admin.resources[resource] | ||
? state.admin.resources[resource].data[id] | ||
: null, | ||
version | ||
); | ||
}; | ||
|
||
export default useGetOne; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useEffect } from 'react'; | ||
// @ts-ignore | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
|
||
import { ReduxState } from '../types'; | ||
|
||
export interface AsyncQueryResponse { | ||
data?: any; | ||
loading: boolean; | ||
loaded: boolean; | ||
error?: any; | ||
} | ||
|
||
export interface FetchAction { | ||
type: string; | ||
payload: any; | ||
meta: { | ||
fetch: string; | ||
resource: string; | ||
onSuccess?: any; | ||
onFailure?: any; | ||
}; | ||
} | ||
|
||
/** | ||
* @typedef AsyncQueryResponse | ||
* @type {Object} | ||
* @property {Object} data | ||
* @property {boolean} loading | ||
* @property {boolean} loaded | ||
* @property {Object} error | ||
*/ | ||
|
||
/** | ||
* Uses an action to call the data provider | ||
* and optimistically loads the result from the store. | ||
* | ||
* @example | ||
* | ||
* const { data, loading, loaded, error } = useOptimisticQuery( | ||
* { type 'RA/GET_ONE', payload: { id: 123 }, meta: { fetch: GET_ONE, resource: 'posts' } } | ||
* state => state.admin.resources.posts[123] | ||
* ); | ||
* | ||
* @param {object} action The action to dispatch, e.g. { type 'RA/GET_ONE', payload: { id: 123 }, meta: { fetch: GET_ONE, resource: 'posts' } } | ||
* @param {function} selector The selector function to get the result from the store | ||
* @param {number} version The version number. Increase to force a new fetch | ||
* | ||
* @returns {GetOneResponse} An object with the shape { data, error, loading, loaded } | ||
*/ | ||
const useOptimisticQuery = ( | ||
action: FetchAction, | ||
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. I'm not so sure about the signature of this one. Should it accept:
In the latter case, we don't need the crud action creators anymore. |
||
selector: (state: ReduxState) => string, | ||
version: number = 1 | ||
) => { | ||
const dispatch = useDispatch(); | ||
useEffect(() => { | ||
dispatch(action); | ||
}, [JSON.stringify({ action, version })]); | ||
const data = useSelector(selector); | ||
const { loading, loaded, error } = useSelector((state: ReduxState) => { | ||
const key = JSON.stringify({ | ||
type: action.meta.fetch, | ||
payload: action.payload, | ||
}); | ||
return state.admin.requests && state.admin.requests[key] | ||
? state.admin.requests[key] | ||
: { loading: false, loaded: false }; | ||
}); | ||
return { data, loading, loaded, error }; | ||
}; | ||
|
||
export default useOptimisticQuery; |
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.
decoupling side effects, refs #2952