Skip to content

Commit

Permalink
Switch to correct view if inner block is selected (woocommerce#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhtungdu authored and jonny-bull committed Dec 16, 2021
1 parent 259c16c commit 96af449
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
26 changes: 14 additions & 12 deletions assets/js/blocks/cart-checkout/cart/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ const ALLOWED_BLOCKS = [
'woocommerce/empty-cart-block',
];

const views = [
{
view: 'woocommerce/filled-cart-block',
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
},
{
view: 'woocommerce/empty-cart-block',
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
];

const BlockSettings = ( { attributes, setAttributes } ) => {
const { hasDarkControls } = attributes;
const { currentPostId } = useEditorContext();
Expand Down Expand Up @@ -104,18 +117,7 @@ export const Edit = ( { className, attributes, setAttributes, clientId } ) => {
const { hasDarkControls } = attributes;
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
clientId,
[
{
view: 'woocommerce/filled-cart-block',
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
},
{
view: 'woocommerce/empty-cart-block',
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
]
views
);
const defaultTemplate = [
[ 'woocommerce/filled-cart-block', {}, [] ],
Expand Down
27 changes: 15 additions & 12 deletions assets/js/blocks/cart-checkout/mini-cart-contents/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ const ALLOWED_BLOCKS = [
'woocommerce/filled-mini-cart-contents-block',
'woocommerce/empty-mini-cart-contents-block',
];

const views = [
{
view: 'woocommerce/filled-mini-cart-contents-block',
label: __( 'Filled Mini Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
},
{
view: 'woocommerce/empty-mini-cart-contents-block',
label: __( 'Empty Mini Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
];

interface Props {
clientId: string;
}
Expand All @@ -36,18 +50,7 @@ const Edit = ( { clientId }: Props ): ReactElement => {

const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
clientId,
[
{
view: 'woocommerce/filled-mini-cart-contents-block',
label: __( 'Filled Mini Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ filledCart } />,
},
{
view: 'woocommerce/empty-mini-cart-contents-block',
label: __( 'Empty Mini Cart', 'woo-gutenberg-products-block' ),
icon: <Icon srcElement={ removeCart } />,
},
]
views
);

useForcedLayout( {
Expand Down
42 changes: 40 additions & 2 deletions assets/js/blocks/cart-checkout/shared/use-view-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { useDispatch, select } from '@wordpress/data';
import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components';
import { Icon, eye } from '@woocommerce/icons';
Expand All @@ -24,7 +24,45 @@ export const useViewSwitcher = (
const initialView = views[ 0 ];
const [ currentView, setCurrentView ] = useState( initialView );
const { selectBlock } = useDispatch( 'core/block-editor' );
const { getBlock } = select( blockEditorStore );
const {
getBlock,
getSelectedBlockClientId,
getBlockParentsByBlockName,
} = select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();

useEffect( () => {
const viewNames = views.map( ( { view } ) => view );
const parentBlockIds = getBlockParentsByBlockName(
selectedBlockClientId,
viewNames
);

if ( parentBlockIds.length !== 1 ) {
return;
}
const parentBlock = getBlock( parentBlockIds[ 0 ] );

if ( currentView.view === parentBlock.name ) {
return;
}

const filteredViews = views.filter(
( { view } ) => view === parentBlock.name
);

if ( filteredViews.length !== 1 ) {
return;
}

setCurrentView( filteredViews[ 0 ] );
}, [
getBlockParentsByBlockName,
selectedBlockClientId,
getBlock,
currentView.view,
views,
] );

const ViewSwitcherComponent = (
<ToolbarGroup>
Expand Down

0 comments on commit 96af449

Please sign in to comment.