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

Commit

Permalink
Turn current page into the default Cart/Checkout page (#6867)
Browse files Browse the repository at this point in the history
* Add default page notice

* show notice all inner blocks

* support flow when page isnt saved

* switch from where we get the current post id

* update lock

* fix types

* update logic to support cart as well

* fix package.json

* update design and move away from wc.data

* restore notice

* handle older versions of WooCommerce

* fix package lock

* fix typo
  • Loading branch information
senadir authored Aug 29, 2022
1 parent d2d809b commit 6286a6f
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 99 deletions.
11 changes: 0 additions & 11 deletions assets/js/blocks/cart-checkout-shared/default-notice/editor.scss

This file was deleted.

58 changes: 0 additions & 58 deletions assets/js/blocks/cart-checkout-shared/default-notice/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion assets/js/blocks/cart-checkout-shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export * from './hacks';
export * from './use-forced-layout';
export * from './editor-utils';
export * from './use-view-switcher';
export * from './default-notice';
export * from './sidebar-notices';
export * from './block-settings';
25 changes: 15 additions & 10 deletions assets/js/blocks/cart-checkout-shared/sidebar-notices/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import {
import { addFilter, hasFilter } from '@wordpress/hooks';
import type { StoreDescriptor } from '@wordpress/data';
import { CartCheckoutSidebarCompatibilityNotice } from '@woocommerce/editor-components/sidebar-compatibility-notice';
import {
DefaultNotice,
LegacyNotice,
} from '@woocommerce/editor-components/default-notice';
import { useSelect } from '@wordpress/data';
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';

/**
* Internal dependencies
*/
import './editor.scss';
import { DefaultNotice } from '../default-notice';

import { isWcVersion } from '@woocommerce/settings';
declare module '@wordpress/editor' {
let store: StoreDescriptor;
}
Expand Down Expand Up @@ -66,12 +64,19 @@ const withSidebarNotices = createHigherOrderComponent(
<>
{ ( isCart || isCheckout ) && (
<InspectorControls>
{ isWcVersion( '6.9.0', '>=' ) ? (
<DefaultNotice
block={ isCheckout ? 'checkout' : 'cart' }
/>
) : (
<LegacyNotice
block={ isCheckout ? 'checkout' : 'cart' }
/>
) }

<CartCheckoutSidebarCompatibilityNotice
block={ isCheckout ? 'checkout' : 'cart' }
/>
<DefaultNotice
block={ isCheckout ? 'checkout' : 'cart' }
/>
{ isAddressFieldBlock ? null : (
<CartCheckoutFeedbackPrompt />
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
.components-notice__dismiss {
min-width: 24px;
}
.components-notice__content {
margin: 4px 0;
}
svg {
width: 16px;
height: 16px;
}
}

.wc-blocks-legacy-page-notice {
margin: 0;
}
191 changes: 191 additions & 0 deletions assets/js/editor-components/default-notice/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* External dependencies
*/
import { __, sprintf } 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 { useSelect, useDispatch } from '@wordpress/data';
import { CHECKOUT_PAGE_ID, CART_PAGE_ID } from '@woocommerce/block-settings';
import {
useCallback,
useState,
createInterpolateElement,
} from '@wordpress/element';
import { getAdminLink } from '@woocommerce/settings';
/**
* Internal dependencies
*/
import './editor.scss';

export function DefaultNotice( { block }: { block: string } ) {
// To avoid having the same logic twice, we're going to handle both pages here.
const ORIGINAL_PAGE_ID =
block === 'checkout' ? CHECKOUT_PAGE_ID : CART_PAGE_ID;
const settingName =
block === 'checkout'
? '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 [ settingStatus, setStatus ] = useState( 'pristine' );
const updatePage = useCallback( () => {
setStatus( 'updating' );
Promise.resolve()
.then( () =>
triggerFetch( {
path: `/wc/v3/settings/advanced/${ settingName }`,
method: 'GET',
} )
)
.catch( ( error ) => {
if ( error.code === 'rest_setting_setting_invalid' ) {
setStatus( 'error' );
}
} )
.then( () => {
if ( ! postPublished ) {
editPost( { status: 'publish' } );
return savePost();
}
} )
.then( () =>
// Make this page ID the default cart/checkout.
triggerFetch( {
path: `/wc/v3/settings/advanced/${ settingName }`,
method: 'POST',
data: {
value: currentPostId.toString(),
},
} )
)
// Append `-2` to the original link so we can use it here.
.then( () => {
if ( ORIGINAL_PAGE_ID !== 0 ) {
return saveEntityRecord( 'postType', 'page', {
id: ORIGINAL_PAGE_ID,
slug: `${ slug }-2`,
} );
}
} )
// Use the original link for this page.
.then( () => editPost( { slug } ) )
// Save page.
.then( () => savePost() )
.then( () => setStatus( 'updated' ) );
}, [
postPublished,
editPost,
savePost,
settingName,
currentPostId,
ORIGINAL_PAGE_ID,
saveEntityRecord,
slug,
] );
if ( currentPostId === ORIGINAL_PAGE_ID || settingStatus === 'dismissed' ) {
return null;
}
return (
<Notice
className="wc-default-page-notice"
status={ settingStatus === 'updated' ? 'success' : 'warning' }
onRemove={ () => setStatus( 'dismissed' ) }
spokenMessage={
settingStatus === 'updated'
? __(
'Page settings updated',
'woo-gutenberg-products-block'
)
: noticeContent
}
>
{ settingStatus === 'updated' ? (
__( 'Page settings updated', 'woo-gutenberg-products-block' )
) : (
<>
<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>
);
}

export function LegacyNotice( { block }: { block: string } ) {
return (
<Notice
className="wc-blocks-legacy-page-notice"
isDismissible={ false }
status="warning"
>
{ createInterpolateElement(
sprintf(
/* translators: %s is the block name. It will be cart or checkout. */
__(
'If you would like to use this block as your default %s you must update your <a>page settings in WooCommerce</a>.',
'woo-gutenberg-products-block'
),
block
),
{
a: (
// eslint-disable-next-line jsx-a11y/anchor-has-content
<a
href={ getAdminLink(
'admin.php?page=wc-settings&tab=advanced'
) }
target="_blank"
rel="noopener noreferrer"
/>
),
}
) }
</Notice>
);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
.wc-blocks-sidebar-compatibility-notice {
border-top: 1px solid $gray-200;
}

.wc-blocks-sidebar-compatibility-notice__notice.components-notice.is-dismissible {
.wc-blocks-sidebar-compatibility-notice.is-dismissible {
margin: 0;
padding: $gap;
padding-right: $gap-small;

padding-right: 16px;
.components-notice__dismiss {
min-width: 24px;
}
.components-notice__content {
margin-right: 0;
margin: 4px 0;
}
svg {
width: 16px;
height: 16px;
}
&.is-hidden {
display: none;
}
}
19 changes: 9 additions & 10 deletions assets/js/editor-components/sidebar-compatibility-notice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Notice, ExternalLink } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';

/**
* Internal dependencies
Expand Down Expand Up @@ -33,16 +34,14 @@ export const CartCheckoutSidebarCompatibilityNotice = ( {
);

return (
<div
className="wc-blocks-sidebar-compatibility-notice"
style={ { display: isVisible ? 'block' : 'none' } }
<Notice
onRemove={ dismissNotice }
className={ classnames( [
'wc-blocks-sidebar-compatibility-notice',
{ 'is-hidden': ! isVisible },
] ) }
>
<Notice
onRemove={ dismissNotice }
className={ 'wc-blocks-sidebar-compatibility-notice__notice' }
>
{ noticeText }
</Notice>
</div>
{ noticeText }
</Notice>
);
};

0 comments on commit 6286a6f

Please sign in to comment.