woocommerce/plugins/woocommerce-blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx

86 lines
2.5 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
Add custom className support to Cart Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/4985) * move empty cart * remove Cart and rename Cart i2 to Cart * graduate blocks * setup template migration from Cart i1 to Cart i2 * back to js so we have a good diff * add migration * fix bug in empty cart template * add useForceLayout hook to edit * migrate from old block to new block * migrate styles * respect align * add tests * Include latest cart line item improvements from cart-i1 * Missing changes from cart-i1 * Line items table should be disabled * Fix e2e tests for cart i2 * update tests to adapt for inner blocks * update select to resolveSelect to remove warning checker * rename test/block to test/index * move blocks to their own file * undo rename to keep diff clean * remove .tsx and update jest config * Revert "update select to resolveSelect to remove warning checker" This reverts commit 79d55de30edcfe36bbdfe7506df7a09460824f03. * revert resolveControl * Fix empty cart editor E2E test by scrolling to the view switch * parse attributes for order summary block * migrate attributes when resaving * Update documentation Automatic update after running npm run build:docs * add classname support to accepted payment methods block * add classname support to express payment methods block * add classname support to cart items block * add classname support to cart line items block * add classname support to order summary block * add classname support to totals block * add classname support to empty cart block * add classname support to filled cart block * add classname support to proceed to checkout block * type edit Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Raluca Stan <ralucastn@gmail.com>
2021-10-25 15:31:22 +00:00
import classnames from 'classnames';
import { useState, useEffect } from '@wordpress/element';
import Button from '@woocommerce/base-components/button';
import { CHECKOUT_URL } from '@woocommerce/block-settings';
import { useCheckoutContext } from '@woocommerce/base-context';
import { usePositionRelativeToViewport } from '@woocommerce/base-hooks';
import { getSetting } from '@woocommerce/settings';
/**
* Internal dependencies
*/
import './style.scss';
/**
* Checkout button rendered in the full cart page.
*/
const Block = ( {
checkoutPageId,
Add custom className support to Cart Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/4985) * move empty cart * remove Cart and rename Cart i2 to Cart * graduate blocks * setup template migration from Cart i1 to Cart i2 * back to js so we have a good diff * add migration * fix bug in empty cart template * add useForceLayout hook to edit * migrate from old block to new block * migrate styles * respect align * add tests * Include latest cart line item improvements from cart-i1 * Missing changes from cart-i1 * Line items table should be disabled * Fix e2e tests for cart i2 * update tests to adapt for inner blocks * update select to resolveSelect to remove warning checker * rename test/block to test/index * move blocks to their own file * undo rename to keep diff clean * remove .tsx and update jest config * Revert "update select to resolveSelect to remove warning checker" This reverts commit 79d55de30edcfe36bbdfe7506df7a09460824f03. * revert resolveControl * Fix empty cart editor E2E test by scrolling to the view switch * parse attributes for order summary block * migrate attributes when resaving * Update documentation Automatic update after running npm run build:docs * add classname support to accepted payment methods block * add classname support to express payment methods block * add classname support to cart items block * add classname support to cart line items block * add classname support to order summary block * add classname support to totals block * add classname support to empty cart block * add classname support to filled cart block * add classname support to proceed to checkout block * type edit Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Raluca Stan <ralucastn@gmail.com>
2021-10-25 15:31:22 +00:00
className,
}: {
checkoutPageId: number;
Add custom className support to Cart Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/4985) * move empty cart * remove Cart and rename Cart i2 to Cart * graduate blocks * setup template migration from Cart i1 to Cart i2 * back to js so we have a good diff * add migration * fix bug in empty cart template * add useForceLayout hook to edit * migrate from old block to new block * migrate styles * respect align * add tests * Include latest cart line item improvements from cart-i1 * Missing changes from cart-i1 * Line items table should be disabled * Fix e2e tests for cart i2 * update tests to adapt for inner blocks * update select to resolveSelect to remove warning checker * rename test/block to test/index * move blocks to their own file * undo rename to keep diff clean * remove .tsx and update jest config * Revert "update select to resolveSelect to remove warning checker" This reverts commit 79d55de30edcfe36bbdfe7506df7a09460824f03. * revert resolveControl * Fix empty cart editor E2E test by scrolling to the view switch * parse attributes for order summary block * migrate attributes when resaving * Update documentation Automatic update after running npm run build:docs * add classname support to accepted payment methods block * add classname support to express payment methods block * add classname support to cart items block * add classname support to cart line items block * add classname support to order summary block * add classname support to totals block * add classname support to empty cart block * add classname support to filled cart block * add classname support to proceed to checkout block * type edit Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Raluca Stan <ralucastn@gmail.com>
2021-10-25 15:31:22 +00:00
className: string;
} ): JSX.Element => {
const link = getSetting( 'page-' + checkoutPageId, false );
const { isCalculating } = useCheckoutContext();
const [ positionReferenceElement, positionRelativeToViewport ] =
usePositionRelativeToViewport();
const [ showSpinner, setShowSpinner ] = useState( false );
useEffect( () => {
// Add a listener to remove the spinner on the checkout button, so the saved page snapshot does not
// contain the spinner class. See https://archive.is/lOEW0 for why this is needed for Safari.
if (
typeof global.addEventListener !== 'function' ||
typeof global.removeEventListener !== 'function'
) {
return;
}
const hideSpinner = () => {
setShowSpinner( false );
};
global.addEventListener( 'pageshow', hideSpinner );
return () => {
global.removeEventListener( 'pageshow', hideSpinner );
};
}, [] );
const submitContainerContents = (
<Button
className="wc-block-cart__submit-button"
href={ link || CHECKOUT_URL }
disabled={ isCalculating }
onClick={ () => setShowSpinner( true ) }
showSpinner={ showSpinner }
>
{ __( 'Proceed to Checkout', 'woo-gutenberg-products-block' ) }
</Button>
);
return (
Add custom className support to Cart Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/4985) * move empty cart * remove Cart and rename Cart i2 to Cart * graduate blocks * setup template migration from Cart i1 to Cart i2 * back to js so we have a good diff * add migration * fix bug in empty cart template * add useForceLayout hook to edit * migrate from old block to new block * migrate styles * respect align * add tests * Include latest cart line item improvements from cart-i1 * Missing changes from cart-i1 * Line items table should be disabled * Fix e2e tests for cart i2 * update tests to adapt for inner blocks * update select to resolveSelect to remove warning checker * rename test/block to test/index * move blocks to their own file * undo rename to keep diff clean * remove .tsx and update jest config * Revert "update select to resolveSelect to remove warning checker" This reverts commit 79d55de30edcfe36bbdfe7506df7a09460824f03. * revert resolveControl * Fix empty cart editor E2E test by scrolling to the view switch * parse attributes for order summary block * migrate attributes when resaving * Update documentation Automatic update after running npm run build:docs * add classname support to accepted payment methods block * add classname support to express payment methods block * add classname support to cart items block * add classname support to cart line items block * add classname support to order summary block * add classname support to totals block * add classname support to empty cart block * add classname support to filled cart block * add classname support to proceed to checkout block * type edit Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Raluca Stan <ralucastn@gmail.com>
2021-10-25 15:31:22 +00:00
<div className={ classnames( 'wc-block-cart__submit', className ) }>
{ positionReferenceElement }
{ /* The non-sticky container must always be visible because it gives height to its parent, which is required to calculate when it becomes visible in the viewport. */ }
<div className="wc-block-cart__submit-container">
{ submitContainerContents }
</div>
{ /* If the positionReferenceElement is below the viewport, display the sticky container. */ }
{ positionRelativeToViewport === 'below' && (
<div className="wc-block-cart__submit-container wc-block-cart__submit-container--sticky">
{ submitContainerContents }
</div>
) }
</div>
);
};
export default Block;