diff --git a/client/my-sites/current-site/index.jsx b/client/my-sites/current-site/index.jsx index a7f8b9987db80..990cab3efc73a 100644 --- a/client/my-sites/current-site/index.jsx +++ b/client/my-sites/current-site/index.jsx @@ -22,6 +22,7 @@ import { getCurrentUserSiteCount } from 'calypso/state/current-user/selectors'; import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { hasAllSitesList } from 'calypso/state/sites/selectors'; import { expandSidebar } from 'calypso/state/ui/actions'; +import CalypsoShoppingCartProvider from 'calypso/my-sites/checkout/calypso-shopping-cart-provider'; /** * Style dependencies @@ -112,10 +113,12 @@ class CurrentSite extends Component { /> ) } { selectedSite && isEnabled( 'current-site/stale-cart-notice' ) && ( - + + + ) } { selectedSite && isEnabled( 'current-site/notice' ) && ( { +export default function StaleCartItemsNotice() { + useShowStaleCartNotice(); + return null; +} + +function useShowStaleCartNotice() { + const selectedSiteSlug = useSelector( getSelectedSiteSlug ); + const staleCartItemNoticeLastTimeShown = useSelector( ( state ) => + getNoticeLastTimeShown( state, staleCartItemNoticeId ) + ); + const sectionName = useSelector( getSectionName ); + const reduxDispatch = useDispatch(); + const { responseCart, isPendingUpdate } = useShoppingCart(); + const translate = useTranslate(); + + useEffect( () => { // Don't show on the checkout page? - if ( this.props.sectionName === 'upgrades' ) { + if ( sectionName === 'upgrades' ) { // Remove any existing stale cart notice - this.props.removeNotice( staleCartItemNoticeId ); - return null; + reduxDispatch( removeNotice( staleCartItemNoticeId ) ); + return; } // Show a notice if there are stale items in the cart and it hasn't been shown // in the last 10 minutes (cart abandonment) - const cart = CartStore.get(); if ( - this.props.selectedSiteSlug && - hasStaleItem( cart ) && - this.props.staleCartItemNoticeLastTimeShown < Date.now() - 10 * 60 * 1000 && - cart.hasLoadedFromServer && - ! cart.hasPendingServerUpdates + selectedSiteSlug && + hasStaleItem( responseCart ) && + staleCartItemNoticeLastTimeShown < Date.now() - 10 * 60 * 1000 && + ! isPendingUpdate ) { - this.props.recordTracksEvent( 'calypso_cart_abandonment_notice_view' ); + reduxDispatch( recordTracksEvent( 'calypso_cart_abandonment_notice_view' ) ); // Remove any existing stale cart notice - this.props.removeNotice( staleCartItemNoticeId ); + reduxDispatch( removeNotice( staleCartItemNoticeId ) ); - this.props.infoNotice( this.props.translate( 'Your cart is awaiting payment.' ), { - id: staleCartItemNoticeId, - button: this.props.translate( 'View your cart' ), - href: '/checkout/' + this.props.selectedSiteSlug, - onClick: this.clickStaleCartItemsNotice, - } ); + reduxDispatch( + infoNotice( translate( 'Your cart is awaiting payment.' ), { + id: staleCartItemNoticeId, + button: translate( 'View your cart' ), + href: '/checkout/' + selectedSiteSlug, + onClick: () => { + reduxDispatch( recordTracksEvent( 'calypso_cart_abandonment_notice_click' ) ); + }, + } ) + ); } - }; - - clickStaleCartItemsNotice = () => { - this.props.recordTracksEvent( 'calypso_cart_abandonment_notice_click' ); - }; - - componentDidMount() { - reloadCart(); - CartStore.on( 'change', this.showStaleCartItemsNotice ); - } - - componentWillUnmount() { - CartStore.off( 'change', this.showStaleCartItemsNotice ); - } - - render() { - return null; - } + }, [ + isPendingUpdate, + sectionName, + selectedSiteSlug, + responseCart, + reduxDispatch, + translate, + staleCartItemNoticeLastTimeShown, + ] ); } - -export default connect( - ( state ) => ( { - selectedSiteSlug: getSelectedSiteSlug( state ), - staleCartItemNoticeLastTimeShown: getNoticeLastTimeShown( state, staleCartItemNoticeId ), - sectionName: getSectionName( state ), - } ), - { infoNotice, removeNotice, recordTracksEvent } -)( localize( StaleCartItemsNotice ) );