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

Commit

Permalink
Update notice for default cart and checkout (#11861)
Browse files Browse the repository at this point in the history
* Update notice for default cart and checkout

* Show full translatable string
  • Loading branch information
nielslange committed Nov 21, 2023
1 parent ca7d3bd commit 64ba5f2
Showing 1 changed file with 60 additions and 46 deletions.
106 changes: 60 additions & 46 deletions assets/js/editor-components/default-notice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { __ } from '@wordpress/i18n';
import { store as editorStore } from '@wordpress/editor';
import triggerFetch from '@wordpress/api-fetch';
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 { 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
*/
Expand All @@ -23,40 +28,21 @@ export function DefaultNotice( { block }: { block: string } ) {
? 'woocommerce_checkout_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
const { saveEntityRecord } = useDispatch( coreStore );
const { editPost, savePost } = useDispatch( editorStore );
const { slug, isLoadingPage, postPublished, currentPostId } = useSelect(
( select ) => {
const { getEntityRecord, isResolving } = select( coreStore );
const { isCurrentPostPublished, getCurrentPostId } =
select( editorStore );
return {
slug:
getEntityRecord( 'postType', 'page', ORIGINAL_PAGE_ID )
?.slug || block,
isLoadingPage: isResolving( 'getEntityRecord', [
'postType',
'page',
ORIGINAL_PAGE_ID,
] ),
postPublished: isCurrentPostPublished(),
currentPostId: getCurrentPostId(),
};
},
[]
);
const { slug, postPublished, currentPostId } = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const { isCurrentPostPublished, getCurrentPostId } =
select( editorStore );
return {
slug:
getEntityRecord( 'postType', 'page', ORIGINAL_PAGE_ID )?.slug ||
block,
postPublished: isCurrentPostPublished(),
currentPostId: getCurrentPostId(),
};
}, [] );
const [ settingStatus, setStatus ] = useState( 'pristine' );
const updatePage = useCallback( () => {
setStatus( 'updating' );
Expand Down Expand Up @@ -112,6 +98,46 @@ export function DefaultNotice( { block }: { block: string } ) {
saveEntityRecord,
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.
if (
( typeof pagenow === 'string' && pagenow === 'site-editor' ) ||
Expand All @@ -123,7 +149,7 @@ export function DefaultNotice( { block }: { block: string } ) {
return (
<Notice
className="wc-default-page-notice"
status={ settingStatus === 'updated' ? 'success' : 'warning' }
status={ settingStatus === 'updated' ? 'success' : 'info' }
onRemove={ () => setStatus( 'dismissed' ) }
spokenMessage={
settingStatus === 'updated'
Expand All @@ -139,18 +165,6 @@ export function DefaultNotice( { block }: { block: string } ) {
) : (
<>
<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>
Expand Down

0 comments on commit 64ba5f2

Please sign in to comment.