-
-
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] Rely on meta to handle optimistic actions #2684
Conversation
We were relying on the action type to handle optimistic effects. This prevent custom actions to be handled optimistically too.
} | ||
if (meta.fetch === UPDATE_MANY) { | ||
const updatedRecords = payload.ids | ||
.reduce((records, id) => records.concat(previousState[id]), []) |
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.
Unless previousState[id] returns an array you can use a map here. And in this case you can do it in a single map
.map(id => ({
...previousState[id],
...payload.data,
}));
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.
I think the reduce
has another purpose: avoid updating non-existing ids.
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.
Even so, it makes no difference. We create a new array any way from the ids in the payload.
And we will always merge the previousState with the data from the payloads, so we might as well do it in a single map.
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.
agreed
It took me some time to understand the purpose of this PR so let me summarize my findings here. The
For instance, for an undoable crudDelete action, the saga dispatches the following action: // Original action
{
type: CRUD_DELETE,
payload: { id, previousData },
meta: {
resource,
fetch: DELETE,
onSuccess: {
notification: {
body: 'ra.notification.deleted',
level: 'info',
messageArgs: {
smart_count: 1,
},
},
redirectTo,
basePath,
},
onFailure: {
notification: {
body: 'ra.notification.http_error',
level: 'warning',
},
},
},
}
// dispatched action
{
type: CRUD_DELETE_OPTIMISTIC,
payload: { id, previousData },
meta: {
optimistic: true,
resource,
fetch: DELETE,
notification: {
body: 'ra.notification.deleted',
level: 'info',
messageArgs: {
smart_count: 1,
},
},
redirectTo,
basePath,
},
} This is fine. The problem lies in the This prevents usage of custom optimistic actions relying on the same fetch metas, for instance the So the proposal is to change the I'm 👍 for that change. |
} | ||
if (meta.fetch === UPDATE_MANY) { | ||
const updatedRecords = payload.ids | ||
.reduce((records, id) => records.concat(previousState[id]), []) |
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.
I think the reduce
has another purpose: avoid updating non-existing ids.
[RFR] Fix incomplete optimistic handling from #2684
Context: https://marmelab.com/react-admin/Actions.html#using-a-custom-action-creator
We were relying on the action type to handle optimistic effects. This prevent custom actions to be handled optimistically too.
Consider the following case: I have an
ACCEPT_CHANGE
andREJECT_CHANGE
actions. Both will remove their targeted item from thechange
resource but will be caught by a custom saga to be applied on another resource. From thechange
resource perspective, they should be removed after being either accepted or rejected.