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

Fix: Mini Cart block: divide contents into three inner blocks #5386

Merged
merged 25 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
52f1fcf
initial render inner blocks on the front end
dinhtungdu Dec 14, 2021
7237a48
add products table block
dinhtungdu Dec 14, 2021
0c98aed
add mini cart footer
dinhtungdu Dec 14, 2021
ec3153d
update icons and title
dinhtungdu Dec 14, 2021
67fc9c8
update title block
dinhtungdu Dec 14, 2021
a23f1d6
update products table block content
dinhtungdu Dec 14, 2021
e2a2d08
update footer block content
dinhtungdu Dec 14, 2021
eea5b22
remove mini cart drawer title
dinhtungdu Dec 14, 2021
82c65f0
use Noninteractive for Mini Cart products table and footer
dinhtungdu Dec 15, 2021
952f0a8
update the initial markup
dinhtungdu Dec 15, 2021
3894b2e
front end style
dinhtungdu Dec 15, 2021
c80de05
fix front end scrolling issue with many items in cart
dinhtungdu Dec 15, 2021
d943b02
editor style
dinhtungdu Dec 15, 2021
c5e7a99
add background support
dinhtungdu Dec 15, 2021
c4eb0de
update template part file
dinhtungdu Dec 15, 2021
012c823
fix the mini cart contents height in the editor
dinhtungdu Dec 15, 2021
ce31fb0
fix: block validation
dinhtungdu Dec 15, 2021
cf4504c
update unit test
dinhtungdu Dec 15, 2021
a80e16b
only render cart/checkout button when corresponding pages exist
dinhtungdu Dec 16, 2021
c982945
remove left over height rule
dinhtungdu Dec 16, 2021
f2b5065
Apply suggestions from code review
dinhtungdu Dec 16, 2021
ed2d861
ah, comma
dinhtungdu Dec 16, 2021
8bbaf38
addess style suggestions
dinhtungdu Dec 17, 2021
45a6c00
Update assets/js/blocks/cart-checkout/mini-cart-contents/inner-blocks…
dinhtungdu Dec 17, 2021
8d08c60
update todo
dinhtungdu Dec 17, 2021
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
1 change: 1 addition & 0 deletions assets/js/blocks/cart-checkout/mini-cart-contents/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { TemplateArray } from '@wordpress/blocks';
* Internal dependencies
*/
import { useViewSwitcher, useForcedLayout } from '../shared';
import './editor.scss';

// Array of allowed block names.
const ALLOWED_BLOCKS = [
Expand Down
16 changes: 16 additions & 0 deletions assets/js/blocks/cart-checkout/mini-cart-contents/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.wp-block-woocommerce-mini-cart-contents {
max-width: 480px;
margin: 0 auto;
}

.wp-block-woocommerce-filled-mini-cart-contents-block {
.block-editor-block-list__layout {
display: flex;
flex-direction: column;
min-height: calc(100vh - 2 * $gap-largest);
}
}

.wp-block-woocommerce-mini-cart-products-table-block {
margin-bottom: auto;
}
3 changes: 3 additions & 0 deletions assets/js/blocks/cart-checkout/mini-cart-contents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const settings = {
reusable: false,
inserter: false,
__experimentalExposeControlsToChildren: true,
color: {
text: false,
},
},
attributes: {
isPreview: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
import type { TemplateArray } from '@wordpress/blocks';
import { useEditorContext } from '@woocommerce/base-context';
import { EditorProvider, useEditorContext } from '@woocommerce/base-context';
import { previewCart } from '@woocommerce/resource-previews';

/**
* Internal dependencies
Expand All @@ -18,16 +18,9 @@ export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
const { currentView } = useEditorContext();

const defaultTemplate = ( [
[
'core/heading',
{
content: __(
'Filled mini cart content',
'woo-gutenberg-products-block'
),
level: 2,
},
],
[ 'woocommerce/mini-cart-title-block', {} ],
[ 'woocommerce/mini-cart-products-table-block', {} ],
[ 'woocommerce/mini-cart-footer-block', {} ],
].filter( Boolean ) as unknown ) as TemplateArray;

useForcedLayout( {
Expand All @@ -43,10 +36,16 @@ export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
currentView !== 'woocommerce/filled-mini-cart-contents-block'
}
>
<InnerBlocks
allowedBlocks={ allowedBlocks }
templateLock="insert"
/>
<EditorProvider
currentView={ currentView }
previewData={ { previewCart } }
>
<InnerBlocks
template={ defaultTemplate }
allowedBlocks={ allowedBlocks }
templateLock="insert"
/>
</EditorProvider>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,20 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import {
usePaymentMethods,
useStoreCart,
} from '@woocommerce/base-context/hooks';
import { TotalsItem } from '@woocommerce/blocks-checkout';
import { CART_URL, CHECKOUT_URL } from '@woocommerce/block-settings';
import Button from '@woocommerce/base-components/button';
import { PaymentMethodDataProvider } from '@woocommerce/base-context';
import { getIconsFromPaymentMethods } from '@woocommerce/base-utils';
import PaymentMethodIcons from '@woocommerce/base-components/cart-checkout/payment-method-icons';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
import { getSetting } from '@woocommerce/settings';
import { useStoreCart } from '@woocommerce/base-context/hooks';

/**
* Internal dependencies
*/
import CartLineItemsTable from '../../../cart/cart-line-items-table';

const PaymentMethodIconsElement = (): JSX.Element => {
const { paymentMethods } = usePaymentMethods();
return (
<PaymentMethodIcons
icons={ getIconsFromPaymentMethods( paymentMethods ) }
/>
);
};

type FilledMiniCartContentsBlockProps = {
const FilledMiniCartContentsBlock = ( {
children,
}: {
children: JSX.Element | JSX.Element[];
};

const FilledMiniCartContentsBlock = ( {}: FilledMiniCartContentsBlockProps ): JSX.Element | null => {
const { cartItems, cartIsLoading, cartTotals } = useStoreCart();
const subTotal = getSetting( 'displayCartPricesIncludingTax', false )
? parseInt( cartTotals.total_items, 10 ) +
parseInt( cartTotals.total_items_tax, 10 )
: parseInt( cartTotals.total_items, 10 );
} ): JSX.Element | null => {
const { cartItems } = useStoreCart();

if ( cartIsLoading || cartItems.length === 0 ) {
if ( cartItems.length === 0 ) {
return null;
}

return (
<>
<div className="wc-block-mini-cart__items">
<CartLineItemsTable
lineItems={ cartItems }
isLoading={ cartIsLoading }
/>
</div>
<div className="wc-block-mini-cart__footer">
<TotalsItem
className="wc-block-mini-cart__footer-subtotal"
currency={ getCurrencyFromPriceResponse( cartTotals ) }
label={ __( 'Subtotal', 'woo-gutenberg-products-block' ) }
value={ subTotal }
description={ __(
'Shipping, taxes, and discounts calculated at checkout.',
'woo-gutenberg-products-block'
) }
/>
<div className="wc-block-mini-cart__footer-actions">
<Button
className="wc-block-mini-cart__footer-cart"
href={ CART_URL }
>
{ __( 'View my cart', 'woo-gutenberg-products-block' ) }
</Button>
<Button
className="wc-block-mini-cart__footer-checkout"
href={ CHECKOUT_URL }
>
{ __(
'Go to checkout',
'woo-gutenberg-products-block'
) }
</Button>
</div>
<PaymentMethodDataProvider>
<PaymentMethodIconsElement />
</PaymentMethodDataProvider>
</div>
</>
);
return <>{ children }</>;
};

export default FilledMiniCartContentsBlock;
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*/
import './empty-mini-cart-contents-block';
import './filled-mini-cart-contents-block';
import './mini-cart-title-block';
import './mini-cart-products-table-block';
import './mini-cart-footer-block';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "woocommerce/mini-cart-footer-block",
"version": "1.0.0",
"title": "Mini Cart Footer",
"description": "Block that displays the footer of the Mini Cart block.",
"category": "woocommerce",
"supports": {
"align": false,
"html": false,
"multiple": false,
"reusable": false,
"inserter": false
},
"attributes": {
"lock": {
"type": "object",
"default": {
"remove": true,
"move": true
}
}
},
"parent": [ "woocommerce/filled-mini-cart-contents-block" ],
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { TotalsItem } from '@woocommerce/blocks-checkout';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
import {
usePaymentMethods,
useStoreCart,
} from '@woocommerce/base-context/hooks';
import PaymentMethodIcons from '@woocommerce/base-components/cart-checkout/payment-method-icons';
import { getIconsFromPaymentMethods } from '@woocommerce/base-utils';
import { getSetting } from '@woocommerce/settings';
import { CART_URL, CHECKOUT_URL } from '@woocommerce/block-settings';
import Button from '@woocommerce/base-components/button';
import { PaymentMethodDataProvider } from '@woocommerce/base-context';

const PaymentMethodIconsElement = (): JSX.Element => {
const { paymentMethods } = usePaymentMethods();
return (
<PaymentMethodIcons
icons={ getIconsFromPaymentMethods( paymentMethods ) }
/>
);
};

const Block = (): JSX.Element => {
const { cartTotals } = useStoreCart();
const subTotal = getSetting( 'displayCartPricesIncludingTax', false )
? parseInt( cartTotals.total_items, 10 ) +
parseInt( cartTotals.total_items_tax, 10 )
: parseInt( cartTotals.total_items, 10 );
return (
<div className="wc-block-mini-cart__footer">
<TotalsItem
className="wc-block-mini-cart__footer-subtotal"
currency={ getCurrencyFromPriceResponse( cartTotals ) }
label={ __( 'Subtotal', 'woo-gutenberg-products-block' ) }
value={ subTotal }
description={ __(
'Shipping, taxes, and discounts calculated at checkout.',
'woo-gutenberg-products-block'
) }
/>
<div className="wc-block-mini-cart__footer-actions">
{ CART_URL && (
<Button
className="wc-block-mini-cart__footer-cart"
href={ CART_URL }
>
{ __( 'View my cart', 'woo-gutenberg-products-block' ) }
</Button>
) }
{ CHECKOUT_URL && (
<Button
className="wc-block-mini-cart__footer-checkout"
href={ CHECKOUT_URL }
>
{ __(
'Go to checkout',
'woo-gutenberg-products-block'
) }
</Button>
) }
</div>
<PaymentMethodDataProvider>
<PaymentMethodIconsElement />
</PaymentMethodDataProvider>
</div>
);
};

export default Block;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import Noninteractive from '@woocommerce/base-components/noninteractive';

/**
* Internal dependencies
*/
import Block from './block';

export const Edit = (): JSX.Element => {
const blockProps = useBlockProps();

return (
<div { ...blockProps }>
<Noninteractive>
<Block />
</Noninteractive>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import { Icon, card } from '@woocommerce/icons';
import { registerFeaturePluginBlockType } from '@woocommerce/block-settings';

/**
* Internal dependencies
*/
import { Edit } from './edit';
import metadata from './block.json';

registerFeaturePluginBlockType( metadata, {
icon: {
src: (
<Icon
srcElement={ card }
className="wc-block-editor-components-block-icon"
/>
),
},
edit: Edit,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "woocommerce/mini-cart-products-table-block",
"version": "1.0.0",
"title": "Mini Cart Products Table",
"description": "Block that displays the products table of the Mini Cart block.",
"category": "woocommerce",
"supports": {
"align": false,
"html": false,
"multiple": false,
"reusable": false,
"inserter": false
},
"attributes": {
"lock": {
"type": "object",
"default": {
"remove": true,
"move": true
}
}
},
"parent": [ "woocommerce/filled-mini-cart-contents-block" ],
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import { useStoreCart } from '@woocommerce/base-context/hooks';

/**
* Internal dependencies
*/
import CartLineItemsTable from '../../../cart/cart-line-items-table';

const Block = (): JSX.Element => {
const { cartItems, cartIsLoading } = useStoreCart();
return (
<div className="wc-block-mini-cart__items">
<CartLineItemsTable
lineItems={ cartItems }
isLoading={ cartIsLoading }
/>
</div>
);
};

export default Block;
Loading