-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[@wordpress/notices] Add removeAllNotices action to allow all notices to be removed from a given context
#44059
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
Conversation
|
Size Change: +32 B (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
| * | ||
| * @return {Object} Action object. | ||
| */ | ||
| export function removeAllNotices( context = DEFAULT_CONTEXT ) { |
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.
One thing about this API is calling it would remove both 'snackbar' notices and also notices that have a type of 'default'.
From a UI point of view, it might look unusual to see two things that are visually different suddenly disappear from a single dismissal, so I wonder if there should be a way to filter by type as well. The same feedback possibly also applies to #39940, though probably less so, as snackbar notices are likely to have a different id.
What do you think?
Something that would be helpful is an example of the use case this PR is fulfilling. #39940 shows the developer requirement, but doesn't describe the feature this API supports from a user perspective (if there is one).
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.
@talldan thanks for the review - I agree that allowing filtering by type is a great idea. I will implement this in both PRs.
The difference between this and #39940 is this one does not require any IDs to be passed to the action which will confer a slight performance improvement based on selecting fewer values from the store and dispatching fewer actions.
A use-case I had in mind for this would be if an app is showing multiple notices, and there is the option to clear all notices, then currently they'd need to loop through the notices and get each ID, then remove each individually. Admittedly the user experience is no different if all notices are removed in one action, or n actions, the end-result is the same.
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'm not convinced that filtering by type in #39940 is a good idea - the developer would have to already know the IDs to remove them, so asking them to pass the type as well seems like it would add more friction for not much gain. I know a snackbar notice and default notice can both have the same ID, but removeNotice does not ask for a type, so when using this it would remove notices of both type anyway.
effe1ce to
4e65fdc
Compare
d79dbd6 to
207db72
Compare
|
Hey @talldan I was wondering if you could take another look at this, or if you have any further thoughts about adding this action? Happy to hear feedback and implement any suggestions you might have. Also just noting a real-world use case for this in https://github.com/woocommerce/woocommerce-blocks/blob/bd69c1fabe5f1734f4821c197e9ebda1bd0e0c79/assets/js/base/utils/create-notice.ts#L88 |
207db72 to
06f82a8
Compare
|
Flaky tests detected in e879e72f41b5a63b9f8aa906f1c367cbf678500b. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5122396045
|
|
@nerrad this has been rebased and tested. On my end it works OK. |
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.
LGTM, pre-approving but before this is merged it needs a changelog entry. Once you added that I can get it merged.
e879e72 to
e6cb885
Compare
| case 'REMOVE_ALL_NOTICES': | ||
| return state.filter( ( { type } ) => type !== action.noticeType ); |
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 isn't considering the provided context in the action object. Should it?
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 isn't considering the provided context in the action object. Should it?
The onSubKey in the reducer takes care of that actually.
I have tests that cover removing notices from a specific context: https://github.com/wordpress/gutenberg/blob/2a5db119b8ed8e16002de157a5edf37ff47dffe6/packages/notices/src/store/test/reducer.js#L230
…ices to be removed from a given context (WordPress#44059) * Add removeAllNotices action with example and docs * Add actions tests for removeAllNotices * Add reducer case to handle REMOVE_ALL_NOTICES action type * Add tests for REMOVE_ALL_NOTICES action type * Add code fence around action example * Add noticeType to action args and tests * Change reducer and tests to account for notice type * Add changelog entry * Fix lint error
What?
Add a
removeAllNoticesaction to allow all notices to be removed from a context without requiring knowledge of each of their IDs.Why?
When clearing notices you currently need to know each notice's ID and remove it individually resulting in several different actions being dispatched to the store.
#39940 proposes the addition of
removeNoticeswhere the IDs of multiple notices to be removed can be specified, however this still requires the caller to know the IDs of all notices.This action further streamlines the use-case of removing all notices from a context by not requiring any IDs, and instead just emptying the context entirely.
How?
The reducer will return an empty array when this action is dispatched.
Testing Instructions
npm run testand check all unit tests pass.Test 1error.Screenshots or screencast