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

Remove old cart notices before showing new ones #7363

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions assets/js/blocks/cart/inner-blocks/filled-cart-block/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,18 +22,42 @@ const FrontendBlock = ( {
} ): JSX.Element | null => {
const { cartItems, cartIsLoading, cartItemErrors } = useStoreCart();
const { hasDarkControls } = useCartBlockContext();
const { createErrorNotice } = useDispatch( 'core/notices' );
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );

/*
* The code for removing old notices is also present in the filled-mini-cart-contents-block/frontend.tsx file and
* will take care of removing outdated errors in the Mini Cart block.
*/
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,
id: error.code,
context: 'wc/cart',
} );
} );
}, [ createErrorNotice, cartItemErrors ] );
}, [
createErrorNotice,
cartItemErrors,
currentlyDisplayedErrorNoticeCodes,
removeNotice,
] );

if ( cartIsLoading || cartItems.length >= 1 ) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { StoreNoticesContainer } from '@woocommerce/blocks-checkout';
import { useStoreCart } from '@woocommerce/base-context/hooks';

import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';

Expand All @@ -19,18 +19,42 @@ const FilledMiniCartContentsBlock = ( {
}: FilledMiniCartContentsBlockProps ): JSX.Element | null => {
const { cartItems, cartItemErrors } = useStoreCart();

const { createErrorNotice } = useDispatch( 'core/notices' );
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );

/*
* The code for removing old notices is also present in the filled-cart-block/frontend.tsx file and will take care
* of removing outdated errors in the Cart block.
*/
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' );
} );

cartItemErrors.forEach( ( error ) => {
createErrorNotice( decodeEntities( error.message ), {
isDismissible: false,
id: error.code,
context: 'wc/cart',
} );
} );
}, [ createErrorNotice, cartItemErrors ] );
}, [
createErrorNotice,
cartItemErrors,
currentlyDisplayedErrorNoticeCodes,
removeNotice,
] );

if ( cartItems.length === 0 ) {
return null;
Expand Down
3 changes: 3 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,9 @@
Target requires 1 element(s) but source may have fewer." source="TS2322" />
<error line="113" column="9" severity="error" message="Type &apos;{}[][]&apos; is not assignable to type &apos;readonly Template[]&apos;." source="TS2322" />
</file>
<file name="assets/js/blocks/cart/inner-blocks/filled-cart-block/frontend.tsx">
<error line="32" column="42" severity="error" message="Property &apos;type&apos; does not exist on type &apos;Notice&apos;." source="TS2339" />
</file>
<file name="assets/js/blocks/cart/inner-blocks/cart-items-block/frontend.tsx">
<error line="15" column="4" severity="error" message="Type &apos;{ children: Element; className: string; }&apos; is not assignable to type &apos;IntrinsicAttributes &amp; RefAttributes&lt;any&gt;&apos;.
Property &apos;children&apos; does not exist on type &apos;IntrinsicAttributes &amp; RefAttributes&lt;any&gt;&apos;." source="TS2322" />
Expand Down