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

Add cart view switcher #7307

Closed
wants to merge 6 commits into from
Closed
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
21 changes: 18 additions & 3 deletions assets/js/base/context/providers/editor-context.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* External dependencies
*/
import { createContext, useContext, useCallback } from '@wordpress/element';
import {
createContext,
useContext,
useCallback,
useState,
} from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { useEffect } from 'react';

/**
* @typedef {import('@woocommerce/type-defs/contexts').EditorDataContext} EditorDataContext
Expand All @@ -15,6 +21,7 @@ const EditorContext = createContext( {
currentView: '',
previewData: {},
getPreviewData: () => void null,
setCurrentView: () => void null,
} );

/**
Expand All @@ -36,7 +43,7 @@ export const useEditorContext = () => {
export const EditorProvider = ( {
children,
currentPostId = 0,
currentView = '',
currentView: initialView = null,
previewData = {},
} ) => {
/**
Expand All @@ -52,7 +59,6 @@ export const EditorProvider = ( {
},
[ currentPostId ]
);

const getPreviewData = useCallback(
( name ) => {
if ( name in previewData ) {
Expand All @@ -62,6 +68,14 @@ export const EditorProvider = ( {
},
[ previewData ]
);
const setCurrentView = ( view ) => {
setView( view );
};

const [ currentView, setView ] = useState( initialView );
useEffect( () => {
setView( initialView );
}, [ initialView ] );

/**
* @type {EditorDataContext}
Expand All @@ -72,6 +86,7 @@ export const EditorProvider = ( {
currentView,
previewData,
getPreviewData,
setCurrentView,
};

return (
Expand Down
13 changes: 9 additions & 4 deletions assets/js/blocks/cart-checkout-shared/use-view-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components';
import { eye } from '@woocommerce/icons';
import { Icon } from '@wordpress/icons';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useEditorContext } from '@woocommerce/base-context';

interface View {
view: string;
Expand All @@ -21,18 +22,23 @@ function getView( viewName: string, views: View[] ) {

export const useViewSwitcher = (
clientId: string,
views: View[]
views: View[],
onViewChange: ( view: View ) => void
): {
currentView: string;
component: JSX.Element;
} => {
const initialView = views[ 0 ];
const [ currentView, setCurrentView ] = useState( initialView );
const { currentView, setCurrentView } = useEditorContext();
const { selectBlock } = useDispatch( 'core/block-editor' );
const { getBlock, getSelectedBlockClientId, getBlockParentsByBlockName } =
select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();

if ( ! currentView ) {
setCurrentView( initialView );
}

useEffect( () => {
const selectedBlock = getBlock( selectedBlockClientId );

Expand Down Expand Up @@ -68,7 +74,6 @@ export const useViewSwitcher = (
}

const newView = getView( parentBlock.name, views );

if ( newView ) {
setCurrentView( newView );
}
Expand All @@ -78,6 +83,7 @@ export const useViewSwitcher = (
getBlock,
currentView.view,
views,
setCurrentView,
] );

const ViewSwitcherComponent = (
Expand All @@ -103,7 +109,6 @@ export const useViewSwitcher = (
/>
</ToolbarGroup>
);

return {
currentView: currentView.view,
component: ViewSwitcherComponent,
Expand Down
85 changes: 85 additions & 0 deletions assets/js/blocks/cart-checkout-shared/view-switcher/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// /**
// * External dependencies
// */
// import { createHigherOrderComponent } from '@wordpress/compose';
// import {
// BlockControls,
// store as blockEditorStore,
// } from '@wordpress/block-editor';
// import { addFilter, hasFilter } from '@wordpress/hooks';
// import { filledCart, removeCart } from '@woocommerce/icons';
// import { Icon } from '@wordpress/icons';
// import { __ } from '@wordpress/i18n';
// import { useSelect } from '@wordpress/data';
// import { EditorProvider, useEditorContext } from '@woocommerce/base-context';

// /**
// * Internal dependencies
// */
// import { useViewSwitcher } from '../../cart-checkout-shared';
// import { previewCart } from '@woocommerce/resource-previews';

// const cartViews = [
// {
// view: 'woocommerce/filled-cart-block',
// label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
// icon: <Icon icon={ filledCart } />,
// },
// {
// view: 'woocommerce/empty-cart-block',
// label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
// icon: <Icon icon={ removeCart } />,
// },
// ];
// interface View {
// view: string;
// label: string;
// icon: string | JSX.Element;
// }

// const withViewSwitcher = createHigherOrderComponent(
// ( BlockEdit ) => ( props ) => {
// const { clientId } = props;
// const { currentView, setCurrentView } = useEditorContext();
// const onViewChange = ( view: View ) => {
// console.log( 'it is working' );
// setCurrentView( view );
// };
// const { component: ViewSwitcherComponent } = useViewSwitcher(
// clientId,
// cartViews,
// onViewChange
// );
// const { isCartBlock } = useSelect( ( select ) => {
// const { getBlockName } = select( blockEditorStore );
// const currentBlockName = getBlockName( clientId );
// return {
// isCartBlock: currentBlockName === 'woocommerce/cart',
// };
// } );
// console.log( isCartBlock );
// return (
// <>
// <EditorProvider
// currentView={ currentView }
// previewData={ { previewCart } }
// >
// { ! isCartBlock && (
// <BlockControls>{ ViewSwitcherComponent }</BlockControls>
// ) }
// <BlockEdit { ...props } />
// </EditorProvider>
// </>
// );
// },
// 'withViewSwitcher'
// );

// if ( ! hasFilter( 'editor.BlockEdit', 'woocommerce/add/cart-view-switcher' ) ) {
// addFilter(
// 'editor.BlockEdit',
// 'woocommerce/add/cart-view-switcher',
// withViewSwitcher,
// 11
// );
// }
1 change: 1 addition & 0 deletions assets/js/blocks/cart/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
BlockSettings,
} from '../cart-checkout-shared';
import '../cart-checkout-shared/sidebar-notices';
import '../cart-checkout-shared/view-switcher';
import { CartBlockContext } from './context';

// This is adds a class to body to signal if the selected block is locked
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"./assets/js/blocks/filter-wrapper/register-components.ts",
"./assets/js/blocks/product-query/variations/**.tsx",
"./assets/js/blocks/product-query/index.tsx",
"./assets/js/blocks/product-query/inspector-controls.tsx"
"./assets/js/blocks/product-query/inspector-controls.tsx",
"./assets/js/blocks/cart-checkout-shared/view-switcher/index.tsx"
],
"repository": {
"type": "git",
Expand Down