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

Switch to correct view if inner block is selected #5358

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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