* Update notice for default cart and checkout

* Show full translatable string
This commit is contained in:
Niels Lange 2023-11-21 21:23:13 +07:00 committed by GitHub
parent 51d3f9ca13
commit a0592778b6
1 changed files with 60 additions and 46 deletions

View File

@ -5,10 +5,15 @@ import { __ } from '@wordpress/i18n';
import { store as editorStore } from '@wordpress/editor'; import { store as editorStore } from '@wordpress/editor';
import triggerFetch from '@wordpress/api-fetch'; import triggerFetch from '@wordpress/api-fetch';
import { store as coreStore } from '@wordpress/core-data'; import { store as coreStore } from '@wordpress/core-data';
import { Notice, Button } from '@wordpress/components'; import { Notice } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data'; import { useSelect, useDispatch } from '@wordpress/data';
import { CHECKOUT_PAGE_ID, CART_PAGE_ID } from '@woocommerce/block-settings'; import { CHECKOUT_PAGE_ID, CART_PAGE_ID } from '@woocommerce/block-settings';
import { useCallback, useState } from '@wordpress/element'; import {
useCallback,
useState,
createInterpolateElement,
} from '@wordpress/element';
/** /**
* Internal dependencies * Internal dependencies
*/ */
@ -23,40 +28,21 @@ export function DefaultNotice( { block }: { block: string } ) {
? 'woocommerce_checkout_page_id' ? 'woocommerce_checkout_page_id'
: 'woocommerce_cart_page_id'; : 'woocommerce_cart_page_id';
const noticeContent =
block === 'checkout'
? __(
'If you would like to use this block as your default checkout, update your page settings',
'woo-gutenberg-products-block'
)
: __(
'If you would like to use this block as your default cart, update your page settings',
'woo-gutenberg-products-block'
);
// Everything below works the same for Cart/Checkout // Everything below works the same for Cart/Checkout
const { saveEntityRecord } = useDispatch( coreStore ); const { saveEntityRecord } = useDispatch( coreStore );
const { editPost, savePost } = useDispatch( editorStore ); const { editPost, savePost } = useDispatch( editorStore );
const { slug, isLoadingPage, postPublished, currentPostId } = useSelect( const { slug, postPublished, currentPostId } = useSelect( ( select ) => {
( select ) => { const { getEntityRecord } = select( coreStore );
const { getEntityRecord, isResolving } = select( coreStore ); const { isCurrentPostPublished, getCurrentPostId } =
const { isCurrentPostPublished, getCurrentPostId } = select( editorStore );
select( editorStore ); return {
return { slug:
slug: getEntityRecord( 'postType', 'page', ORIGINAL_PAGE_ID )?.slug ||
getEntityRecord( 'postType', 'page', ORIGINAL_PAGE_ID ) block,
?.slug || block, postPublished: isCurrentPostPublished(),
isLoadingPage: isResolving( 'getEntityRecord', [ currentPostId: getCurrentPostId(),
'postType', };
'page', }, [] );
ORIGINAL_PAGE_ID,
] ),
postPublished: isCurrentPostPublished(),
currentPostId: getCurrentPostId(),
};
},
[]
);
const [ settingStatus, setStatus ] = useState( 'pristine' ); const [ settingStatus, setStatus ] = useState( 'pristine' );
const updatePage = useCallback( () => { const updatePage = useCallback( () => {
setStatus( 'updating' ); setStatus( 'updating' );
@ -112,6 +98,46 @@ export function DefaultNotice( { block }: { block: string } ) {
saveEntityRecord, saveEntityRecord,
slug, slug,
] ); ] );
let noticeContent;
if ( block === 'checkout' ) {
noticeContent = createInterpolateElement(
__(
'If you would like to use this block as your default checkout, <a>update your page settings</a>.',
'woo-gutenberg-products-block'
),
{
a: (
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a href="#" onClick={ updatePage }>
{ __(
'update your page settings',
'woo-gutenberg-products-block'
) }
</a>
),
}
);
} else {
noticeContent = createInterpolateElement(
__(
'If you would like to use this block as your default cart, <a>update your page settings</a>.',
'woo-gutenberg-products-block'
),
{
a: (
// eslint-disable-next-line jsx-a11y/anchor-is-valid
<a href="#" onClick={ updatePage }>
{ __(
'update your page settings',
'woo-gutenberg-products-block'
) }
</a>
),
}
);
}
// Avoid showing the notice on the site editor, if already set, or if dismissed earlier. // Avoid showing the notice on the site editor, if already set, or if dismissed earlier.
if ( if (
( typeof pagenow === 'string' && pagenow === 'site-editor' ) || ( typeof pagenow === 'string' && pagenow === 'site-editor' ) ||
@ -123,7 +149,7 @@ export function DefaultNotice( { block }: { block: string } ) {
return ( return (
<Notice <Notice
className="wc-default-page-notice" className="wc-default-page-notice"
status={ settingStatus === 'updated' ? 'success' : 'warning' } status={ settingStatus === 'updated' ? 'success' : 'info' }
onRemove={ () => setStatus( 'dismissed' ) } onRemove={ () => setStatus( 'dismissed' ) }
spokenMessage={ spokenMessage={
settingStatus === 'updated' settingStatus === 'updated'
@ -139,18 +165,6 @@ export function DefaultNotice( { block }: { block: string } ) {
) : ( ) : (
<> <>
<p>{ noticeContent }</p> <p>{ noticeContent }</p>
<Button
onClick={ updatePage }
variant="secondary"
isBusy={ settingStatus === 'updating' }
disabled={ isLoadingPage }
isSmall={ true }
>
{ __(
'Update your page settings',
'woo-gutenberg-products-block'
) }
</Button>
</> </>
) } ) }
</Notice> </Notice>