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

Commit

Permalink
show notice when there is an error
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Jul 24, 2023
1 parent 630b55b commit 54cb0ff
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion assets/js/atomic/blocks/product-elements/button/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
import { store } from '@woocommerce/interactivity';
import { dispatch } from '@wordpress/data';
import { Cart } from '@woocommerce/type-defs/cart';

import { createRoot } from '@wordpress/element';
import NoticeBanner from '@woocommerce/base-components/notice-banner';
type Context = {
woocommerce: {
isLoading: boolean;
addToCartText: string;
productId: number;
numberOfItems: number;
productPermalink: string;
};
};

Expand Down Expand Up @@ -141,6 +143,28 @@ store( {
context.woocommerce.numberOfItems++;
context.woocommerce.isLoading = false;
} catch ( error ) {
const domNode = document.querySelector(
'.wc-block-store-notices'
);

const root = createRoot( domNode );

root.render(
<NoticeBanner
status="error"
onRemove={ () => root.unmount() }
>
{ error.message }
</NoticeBanner>
);

domNode?.scrollIntoView( {
behavior: 'smooth',
inline: 'nearest',
} );

context.woocommerce.isLoading = false;

// we don't care about errors blocking execution, but will console.error for troubleshooting.
// eslint-disable-next-line no-console
console.error( error );
Expand Down

0 comments on commit 54cb0ff

Please sign in to comment.