/* eslint-disable @wordpress/no-unsafe-wp-apis */ /** * External dependencies */ import clsx from 'clsx'; import { __ } from '@wordpress/i18n'; import { PanelBody, ToggleControl } from '@wordpress/components'; import { Icon, store, shipping } from '@wordpress/icons'; import { ADMIN_URL, getSetting } from '@woocommerce/settings'; import { LOCAL_PICKUP_ENABLED } from '@woocommerce/block-settings'; import { InspectorControls, useBlockProps, RichText, } from '@wordpress/block-editor'; import Button from '@woocommerce/base-components/button'; import { useShippingData } from '@woocommerce/base-context/hooks'; import { innerBlockAreas } from '@woocommerce/blocks-checkout'; import { useDispatch, useSelect } from '@wordpress/data'; import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data'; import ExternalLinkCard from '@woocommerce/editor-components/external-link-card'; import { useEffect } from '@wordpress/element'; /** * Internal dependencies */ import { FormStepBlock, AdditionalFields, AdditionalFieldsContent, } from '../../form-step'; import { RatePrice, getLocalPickupPrices, getShippingPrices } from './shared'; import type { minMaxPrices } from './shared'; import './style.scss'; import { defaultShippingText, defaultLocalPickupText } from './constants'; const LocalPickupSelector = ( { checked, rate, showPrice, showIcon, toggleText, setAttributes, onClick, }: { checked: string; rate: minMaxPrices; showPrice: boolean; showIcon: boolean; toggleText: string; onClick: () => void; setAttributes: ( attributes: Record< string, unknown > ) => void; } ) => { return ( ); }; const ShippingSelector = ( { checked, rate, showPrice, showIcon, toggleText, setAttributes, onClick, }: { checked: string; rate: minMaxPrices; showPrice: boolean; showIcon: boolean; toggleText: string; setAttributes: ( attributes: Record< string, unknown > ) => void; onClick: () => void; } ) => { const Price = rate.min === undefined ? ( { __( 'calculated with an address', 'woocommerce' ) } ) : ( ); return ( ); }; export const Edit = ( { attributes, setAttributes, }: { attributes: { title: string; description: string; showStepNumber: boolean; allowCreateAccount: boolean; localPickupText: string; shippingText: string; showPrice: boolean; showIcon: boolean; className: string; }; setAttributes: ( attributes: Record< string, unknown > ) => void; } ): JSX.Element | null => { useEffect( () => { const localPickupTitle = getSetting< string >( 'localPickupText', attributes.localPickupText ); setAttributes( { localPickupText: localPickupTitle } ); // Disable the exhaustive deps rule because we only want to run this on first mount to set the attribute, not // each time the attribute changes. // eslint-disable-next-line react-hooks/exhaustive-deps }, [ setAttributes ] ); const { setPrefersCollection } = useDispatch( CHECKOUT_STORE_KEY ); const { prefersCollection } = useSelect( ( select ) => { const checkoutStore = select( CHECKOUT_STORE_KEY ); return { prefersCollection: checkoutStore.prefersCollection(), }; } ); const { showPrice, showIcon, className, localPickupText, shippingText } = attributes; const { shippingRates, needsShipping, hasCalculatedShipping, isCollectable, } = useShippingData(); if ( ! needsShipping || ! hasCalculatedShipping || ! shippingRates || ! isCollectable || ! LOCAL_PICKUP_ENABLED ) { return null; } const changeView = ( method: string ) => { if ( method === 'pickup' ) { setPrefersCollection( true ); } else { setPrefersCollection( false ); } }; return (

{ __( 'Choose how this block is displayed to your customers.', 'woocommerce' ) }

setAttributes( { showIcon: ! showIcon, } ) } /> setAttributes( { showPrice: ! showPrice, } ) } />

{ __( 'Methods can be made managed in your store settings.', 'woocommerce' ) }

{ changeView( 'shipping' ); } } showPrice={ showPrice } showIcon={ showIcon } setAttributes={ setAttributes } toggleText={ shippingText } /> { changeView( 'pickup' ); } } showIcon={ showIcon } setAttributes={ setAttributes } toggleText={ localPickupText } />
); }; export const Save = (): JSX.Element => { return (
); };