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

Commit

Permalink
Edit Proceed to Checkout button (#7733)
Browse files Browse the repository at this point in the history
* Edit Proceed to Checkout button

* Don't allow empty button and remove console log

* extract default button label into a variable

* Feedback from Nadir & Niels

Co-authored-by: Niels Lange <[email protected]>
  • Loading branch information
alexflorisca and nielslange authored Dec 13, 2022
1 parent bdc8f99 commit 1ea3470
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import { defaultButtonLabel } from './constants';

export default {
checkoutPageId: {
type: 'number',
Expand All @@ -10,4 +15,8 @@ export default {
remove: true,
},
},
buttonLabel: {
type: 'string',
default: defaultButtonLabel,
},
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { useState, useEffect } from '@wordpress/element';
import Button from '@woocommerce/base-components/button';
Expand All @@ -15,16 +14,19 @@ import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
* Internal dependencies
*/
import './style.scss';
import { defaultButtonLabel } from './constants';

/**
* Checkout button rendered in the full cart page.
*/
const Block = ( {
checkoutPageId,
className,
buttonLabel,
}: {
checkoutPageId: number;
className: string;
buttonLabel: string;
} ): JSX.Element => {
const link = getSetting( 'page-' + checkoutPageId, false );
const isCalculating = useSelect( ( select ) =>
Expand Down Expand Up @@ -64,7 +66,7 @@ const Block = ( {
onClick={ () => setShowSpinner( true ) }
showSpinner={ showSpinner }
>
{ __( 'Proceed to Checkout', 'woo-gutenberg-products-block' ) }
{ buttonLabel || defaultButtonLabel }
</Button>
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';

export const defaultButtonLabel = __(
'Proceed to Checkout',
'woo-gutenberg-products-block'
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
import { useRef } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import Button from '@woocommerce/base-components/button';
import {
InspectorControls,
RichText,
useBlockProps,
} from '@wordpress/block-editor';
import PageSelector from '@woocommerce/editor-components/page-selector';
import { CART_PAGE_ID } from '@woocommerce/block-settings';
import Noninteractive from '@woocommerce/base-components/noninteractive';

/**
* Internal dependencies
*/
import Block from './block';
import { defaultButtonLabel } from './constants';

export const Edit = ( {
attributes,
setAttributes,
}: {
attributes: {
checkoutPageId: number;
className: string;
buttonLabel: string;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => {
const blockProps = useBlockProps();
const { checkoutPageId = 0, className } = attributes;
const { checkoutPageId = 0, buttonLabel } = attributes;
const { current: savedCheckoutPageId } = useRef( checkoutPageId );

const currentPostId = useSelect(
( select ) => {
if ( ! savedCheckoutPageId ) {
Expand All @@ -44,7 +52,7 @@ export const Edit = ( {
) && (
<PageSelector
pageId={ checkoutPageId }
setPageId={ ( id ) =>
setPageId={ ( id: number ) =>
setAttributes( { checkoutPageId: id } )
}
labels={ {
Expand All @@ -60,12 +68,19 @@ export const Edit = ( {
/>
) }
</InspectorControls>
<Noninteractive>
<Block
checkoutPageId={ checkoutPageId }
className={ className }
<Button className="wc-block-cart__submit-button">
<RichText
multiline={ false }
allowedFormats={ [] }
value={ buttonLabel }
placeholder={ defaultButtonLabel }
onChange={ ( content ) => {
setAttributes( {
buttonLabel: content,
} );
} }
/>
</Noninteractive>
</Button>
</div>
);
};
Expand Down

0 comments on commit 1ea3470

Please sign in to comment.