-
Notifications
You must be signed in to change notification settings - Fork 4.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
Add isSaving and isDeleting to useEntityRecord, add props to existing… #56473
base: trunk
Are you sure you want to change the base?
Conversation
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 we should also return defaults for these new values when queries are disabled.
if ( ! options.enabled ) {
return {
editedRecord: EMPTY_OBJECT,
hasEdits: false,
edits: EMPTY_OBJECT,
};
}
After thinking about this some more, I think the last thing missing from Any suggestions on the best place to wire this in? |
I left a comment in the parent issue #56471 (comment). It would be great to find other use cases before committing to new API parts. I’m also wondering now why @adamziel didn’t decide to include |
@gziolo here’s my original rationale: Different anglesRe-throwing errors is different than having a convenience wrapper for actions like const { create, isCreating, error } = useEntityRecordCreate();
const { editedRecord, applyEdits, saveEdits, isSaving, error } = useEntityRecordEdit();
const { remove, isRemoving, error } = useEntityRecordRemove(); // it's "remove", because "delete" is a reserved keyword However, given the "throwing" actions, accessing const { create } = useEntityRecordCreate();
const { editedRecord, applyEdits, saveEdits } = useEntityRecordEdit();
const { remove } = useEntityRecordRemove(); Which doesn't do much for creating and removing records, so we could also ditch these hooks and settle only for the const { editedRecord, applyEdits, saveEdits } = useEntityRecordEdit(); |
If we have I like the idea of different hooks for different contexts (eg the edit context) but I personally don't see the harm in having them all under one umbrella hook (I realise I might not be viewing this with the big picture in mind), the |
I don’t have a strong preference and will defer to @gziolo. At the same time, just to nurture, isSaving etc. became a part of state because a few years ago you couldn’t easily use try {
const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
{
slug: cleanSlug,
title,
content: '',
area,
},
{ throwOnError: true }
);
} catch ( e ) {
// Display an error notice
} It solves the same problem of knowing „what state are we in”, only in a more ”JavaScripty” way. Would that work for your use-case? If not, maybe it’s indeed time to expose isSaving from that hook. See also #39258 |
Thanks for the info @adamziel - for me its just about making the various states of the entities use a similar API across the board. It feels like Saying that, the place you do the I guess I thought Some places in core would benefit from this change also such the navigation menu. |
What?
This exposes
isSaving
andisDeleting
onuseEntityRecord
.Why?
useEntityRecord
exposes nearly all actions and data releated to entity records, without having to go an use an additionaluseSelect
to get anything else... exceptisSaving
andisDeleting
still require a seperateselect
call.How?
I've added
isSavingEntityRecord
andisDeletingEntityRecord
inside theuseEntityRecord
hook.I did look at
useQuerySelect
but that seems to deal more with getting things and checking if a record has resolved.The best place I figured would be directly inside the hook, in the existing
useSelect
.Testing Instructions
You can now use
useEntityRecord
like this:Notice
isSaving
andisDeleting
are now exposed.Testing
I'm really not too confident with updating and writing the tests for this, but I've added these new props to the expected testing outputs, I've not written any additional tests to check whether
isSaving
andisDeleting
actually work as expected.I would be interested to learn how to do this though.