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

Commit

Permalink
Add previous react code for notices
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Nov 9, 2023
1 parent 06083e0 commit 386a65a
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions assets/js/atomic/blocks/product-elements/button/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { store, getContext as getContextFn } from '@woocommerce/interactivity';
import { select, subscribe, dispatch } from '@wordpress/data';
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
import { Cart } from '@woocommerce/type-defs/cart';

// import { createRoot } from '@wordpress/element';
// import NoticeBanner from '@woocommerce/base-components/notice-banner';
import { createRoot } from '@wordpress/element';
import NoticeBanner from '@woocommerce/base-components/notice-banner';

interface Context {
isLoading: boolean;
Expand Down Expand Up @@ -46,6 +45,29 @@ interface Store {
};
}

const storeNoticeClass = '.wc-block-store-notices';

const createNoticeContainer = () => {
const noticeContainer = document.createElement( 'div' );
noticeContainer.classList.add( storeNoticeClass.replace( '.', '' ) );
return noticeContainer;
};

const injectNotice = ( domNode: Element, errorMessage: string ) => {
const root = createRoot( domNode );

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

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

const getProductById = ( cartState: Cart | undefined, productId: number ) => {
return cartState?.items.find( ( item ) => item.id === productId );
};
Expand Down Expand Up @@ -125,6 +147,25 @@ const { state } = store< Store >( 'woocommerce/product-button', {
// After the cart is updated, sync the temporary number of items again.
context.temporaryNumberOfItems = state.numberOfItemsInTheCart;
} catch ( error ) {
const storeNoticeBlock =
document.querySelector( storeNoticeClass );

if ( ! storeNoticeBlock ) {
document
.querySelector( '.entry-content' )
?.prepend( createNoticeContainer() );
}

const domNode =
storeNoticeBlock ??
document.querySelector( storeNoticeClass );

if ( domNode ) {
injectNotice( domNode, ( error as Error ).message );
}

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

0 comments on commit 386a65a

Please sign in to comment.