woocommerce/plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode.../index.tsx

150 lines
3.8 KiB
TypeScript
Raw Normal View History

/**
* 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;
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
type: 'incompatible' | 'generic';
}
export function SwitchToClassicShortcodeButton( {
block,
clientId,
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
type,
}: 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 );
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
const isCart = block === 'woocommerce/cart';
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
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,
};
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 = () => {
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
recordEvent( 'switch_to_classic_shortcode_click', eventValue );
openModal();
};
const handleUndoClick = () => {
undo();
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
recordEvent( 'switch_to_classic_shortcode_undo', eventValue );
};
const handleSwitchClick = () => {
replaceBlock(
clientId,
createBlock( 'woocommerce/classic-shortcode', {
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
shortcode,
} )
);
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
recordEvent( 'switch_to_classic_shortcode_confirm', eventValue );
selectClassicShortcodeBlock();
createInfoNotice( snackbarLabel, {
actions: [
{
label: __( 'Undo', 'woocommerce' ),
onClick: handleUndoClick,
},
],
type: 'snackbar',
} );
closeModal();
};
const handleCancelClick = () => {
Update generic incompatibility notice (#42751) * Move the switch to classic shortcode block button to separate component - Move modal component file from incompatible-extension-notice component folder to switch-to-classic-shortcode-button folder. - Rename modal component file to model-content component file. - Move the switch to classic shortcode block button to separate component folder * Update generic incompatibility notice - Update the notice description. - Add switch to classic shortcode block button to the notice. * Fix margin in editor.scss * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update class names in switch-to-classic-shortcode-button component * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Update plugins/woocommerce-blocks/assets/js/editor-components/switch-to-classic-shortcode-button/editor.scss Co-authored-by: Niels Lange <info@nielslange.de> * Refactor SwitchToClassicShortcodeButton component * Fix css lint erros * Add changefile(s) from automation for the following project(s): woocommerce-blocks * Add type property to notice events * Refactor switch to classic shortcode button logic * Use isCart for switchButtonLabel and snackbarLabel * Remove empty div in sidebar compatibility notice component * Refactor switch to classic shortcode button event handling * Adjust wording * Adjust marging and readability * Introduce union for SwitchToClassicShortcodeButtonProps type prop * Simplify logic * Use proposed text --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Niels Lange <info@nielslange.de> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
2024-01-05 03:47:34 +00:00
recordEvent( 'switch_to_classic_shortcode_cancel', eventValue );
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>
) }
</>
);
}