2023-12-19 07:44:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button, TabbableContainer, Modal } from '@wordpress/components';
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { createBlock, BlockInstance } from '@wordpress/blocks';
|
|
|
|
import { store as noticesStore } from '@wordpress/notices';
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
|
|
|
import { store as blockEditorStore } from '@wordpress/block-editor';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { findBlock } from '@woocommerce/utils';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ModalContent } from './modal-content';
|
|
|
|
import './editor.scss';
|
|
|
|
|
|
|
|
interface SwitchToClassicShortcodeButtonProps {
|
|
|
|
block: 'woocommerce/cart' | 'woocommerce/checkout';
|
|
|
|
clientId: string;
|
2024-01-05 03:47:34 +00:00
|
|
|
type: 'incompatible' | 'generic';
|
2023-12-19 07:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function SwitchToClassicShortcodeButton( {
|
|
|
|
block,
|
|
|
|
clientId,
|
2024-01-05 03:47:34 +00:00
|
|
|
type,
|
2023-12-19 07:44:09 +00:00
|
|
|
}: SwitchToClassicShortcodeButtonProps ): JSX.Element {
|
|
|
|
const { createInfoNotice } = useDispatch( noticesStore );
|
|
|
|
const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
|
|
|
|
|
|
|
|
const [ isOpen, setOpen ] = useState( false );
|
|
|
|
const openModal = () => setOpen( true );
|
|
|
|
const closeModal = () => setOpen( false );
|
|
|
|
const { undo } = useDispatch( coreStore );
|
|
|
|
|
2024-01-05 03:47:34 +00:00
|
|
|
const isCart = block === 'woocommerce/cart';
|
2023-12-19 07:44:09 +00:00
|
|
|
|
2024-01-05 03:47:34 +00:00
|
|
|
const switchButtonLabel = isCart
|
|
|
|
? __( 'Switch to classic cart', 'woocommerce' )
|
|
|
|
: __( 'Switch to classic checkout', 'woocommerce' );
|
|
|
|
|
|
|
|
const snackbarLabel = isCart
|
|
|
|
? __( 'Switched to classic cart.', 'woocommerce' )
|
|
|
|
: __( 'Switched to classic checkout.', 'woocommerce' );
|
|
|
|
|
|
|
|
const notice =
|
|
|
|
type === 'incompatible' ? 'incompatible_notice' : 'generic_notice';
|
|
|
|
|
|
|
|
const shortcode = isCart ? 'cart' : 'checkout';
|
|
|
|
|
|
|
|
const eventValue = {
|
|
|
|
shortcode,
|
|
|
|
notice,
|
|
|
|
};
|
2023-12-19 07:44:09 +00:00
|
|
|
|
|
|
|
const { getBlocks } = useSelect( ( select ) => {
|
|
|
|
return {
|
|
|
|
getBlocks: select( blockEditorStore ).getBlocks,
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const selectClassicShortcodeBlock = () => {
|
|
|
|
const classicShortcodeBlock = findBlock( {
|
|
|
|
blocks: getBlocks(),
|
|
|
|
findCondition: ( foundBlock: BlockInstance ) =>
|
|
|
|
foundBlock.name === 'woocommerce/classic-shortcode',
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( classicShortcodeBlock ) {
|
|
|
|
selectBlock( classicShortcodeBlock.clientId );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSwitchToClassicShortcodeClick = () => {
|
2024-01-05 03:47:34 +00:00
|
|
|
recordEvent( 'switch_to_classic_shortcode_click', eventValue );
|
2023-12-19 07:44:09 +00:00
|
|
|
openModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleUndoClick = () => {
|
|
|
|
undo();
|
2024-01-05 03:47:34 +00:00
|
|
|
recordEvent( 'switch_to_classic_shortcode_undo', eventValue );
|
2023-12-19 07:44:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleSwitchClick = () => {
|
|
|
|
replaceBlock(
|
|
|
|
clientId,
|
|
|
|
createBlock( 'woocommerce/classic-shortcode', {
|
2024-01-05 03:47:34 +00:00
|
|
|
shortcode,
|
2023-12-19 07:44:09 +00:00
|
|
|
} )
|
|
|
|
);
|
2024-01-05 03:47:34 +00:00
|
|
|
recordEvent( 'switch_to_classic_shortcode_confirm', eventValue );
|
2023-12-19 07:44:09 +00:00
|
|
|
selectClassicShortcodeBlock();
|
|
|
|
createInfoNotice( snackbarLabel, {
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: __( 'Undo', 'woocommerce' ),
|
|
|
|
onClick: handleUndoClick,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: 'snackbar',
|
|
|
|
} );
|
|
|
|
closeModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCancelClick = () => {
|
2024-01-05 03:47:34 +00:00
|
|
|
recordEvent( 'switch_to_classic_shortcode_cancel', eventValue );
|
2023-12-19 07:44:09 +00:00
|
|
|
closeModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
variant={ 'secondary' }
|
|
|
|
onClick={ handleSwitchToClassicShortcodeClick }
|
|
|
|
>
|
|
|
|
{ switchButtonLabel }
|
|
|
|
</Button>
|
|
|
|
{ isOpen && (
|
|
|
|
<Modal
|
|
|
|
size="medium"
|
|
|
|
title={ switchButtonLabel }
|
|
|
|
onRequestClose={ closeModal }
|
|
|
|
className="wc-blocks-switch-to-classic-shortcode-modal-content"
|
|
|
|
>
|
|
|
|
<ModalContent blockType={ block } />
|
|
|
|
<TabbableContainer className="wc-blocks-switch-to-classic-shortcode-modal-actions">
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
isDestructive={ true }
|
|
|
|
onClick={ handleSwitchClick }
|
|
|
|
>
|
|
|
|
{ __( 'Switch', 'woocommerce' ) }
|
|
|
|
</Button>{ ' ' }
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
onClick={ handleCancelClick }
|
|
|
|
>
|
|
|
|
{ __( 'Cancel', 'woocommerce' ) }
|
|
|
|
</Button>
|
|
|
|
</TabbableContainer>
|
|
|
|
</Modal>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|