Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove cart notices before showing new ones
Browse files Browse the repository at this point in the history
opr committed Nov 7, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent da19ff8 commit 8a244d9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions assets/js/blocks/cart/inner-blocks/filled-cart-block/frontend.tsx
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { SidebarLayout } from '@woocommerce/base-components/sidebar-layout';
import { useStoreCart } from '@woocommerce/base-context/hooks';
import { useEffect } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
@@ -22,10 +22,25 @@ const FrontendBlock = ( {
} ): JSX.Element | null => {
const { cartItems, cartIsLoading, cartItemErrors } = useStoreCart();
const { hasDarkControls } = useCartBlockContext();
const { createErrorNotice } = useDispatch( 'core/notices' );
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );

const currentlyDisplayedErrorNoticeCodes = useSelect( ( select ) => {
return select( 'core/notices' )
.getNotices( 'wc/cart' )
.filter(
( notice ) =>
notice.status === 'error' && notice.type === 'default'
)
.map( ( notice ) => notice.id );
} );

// Ensures any cart errors listed in the API response get shown.
useEffect( () => {
// Clear errors out of the store before adding the new ones from the response.
currentlyDisplayedErrorNoticeCodes.forEach( ( id ) => {
removeNotice( id, 'wc/cart' );
} );

// Ensures any cart errors listed in the API response get shown.
cartItemErrors.forEach( ( error ) => {
createErrorNotice( decodeEntities( error.message ), {
isDismissible: true,

0 comments on commit 8a244d9

Please sign in to comment.