Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/9846 fpm setup banner error handling #9969

Merged
merged 9 commits into from
Jan 10, 2025
Merged
15 changes: 10 additions & 5 deletions assets/js/components/notifications/FirstPartyModeSetupBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ export default function FirstPartyModeSetupBanner( { id, Notification } ) {
select( CORE_NOTIFICATIONS ).isNotificationDismissed( id )
);

const { invalidateResolution } = useDispatch( CORE_NOTIFICATIONS );

const isDismissing = useSelect( ( select ) =>
select( CORE_USER ).isDismissingItem( id )
);

const { dismissNotification, invalidateResolution } =
useDispatch( CORE_NOTIFICATIONS );
const { setValue } = useDispatch( CORE_UI );

const learnMoreURL = useSelect( ( select ) => {
Expand All @@ -97,15 +97,17 @@ export default function FirstPartyModeSetupBanner( { id, Notification } ) {

const onCTAClick = async () => {
setFirstPartyModeEnabled( true );
await saveFirstPartyModeSettings();
const { error } = await saveFirstPartyModeSettings();

if ( error ) {
return { error };
}

setValue( FPM_SHOW_SETUP_SUCCESS_NOTIFICATION, true );
invalidateResolution( 'getQueuedNotifications', [
viewContext,
NOTIFICATION_GROUPS.DEFAULT,
] );

dismissNotification( id );
};

const { triggerSurvey } = useDispatch( CORE_USER );
Expand Down Expand Up @@ -189,6 +191,9 @@ export default function FirstPartyModeSetupBanner( { id, Notification } ) {
'google-site-kit'
) }
onCTAClick={ onCTAClick }
ctaDismissOptions={ {
skipHidingFromQueue: false,
} }
dismissLabel={ __( 'Maybe later', 'google-site-kit' ) }
onDismiss={ onDismiss }
dismissOptions={ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import fetchMock from 'fetch-mock';
*/
import { provideModules } from '../../../../tests/js/utils';
import WithRegistrySetup from '../../../../tests/js/WithRegistrySetup';
import { CORE_SITE } from '../../googlesitekit/datastore/site/constants';
import { CORE_USER } from '../../googlesitekit/datastore/user/constants';
import { withNotificationComponentProps } from '../../googlesitekit/notifications/util/component-props';
import { WEEK_IN_SECONDS } from '../../util';
Expand All @@ -49,10 +50,27 @@ export const Default = Template.bind();
Default.storyName = 'FirstPartyModeSetupBanner';
Default.scenario = {};

export const ErrorOnCTAClick = Template.bind();
ErrorOnCTAClick.storyName = 'ErrorOnCTAClick';
ErrorOnCTAClick.scenario = {};
ErrorOnCTAClick.args = {
setupRegistry: ( registry ) => {
registry.dispatch( CORE_SITE ).receiveError(
{
code: 'test_error',
message: 'Test Error',
data: {},
},
'notificationAction',
[ FPM_SETUP_CTA_BANNER_NOTIFICATION ]
);
},
};

export default {
title: 'Modules/FirstPartyMode/Dashboard/FirstPartyModeSetupBanner',
decorators: [
( Story ) => {
( Story, { args } ) => {
const setupRegistry = ( registry ) => {
provideModules( registry, [
{
Expand Down Expand Up @@ -87,6 +105,8 @@ export default {
status: 200,
}
);

args.setupRegistry?.( registry );
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,47 @@ describe( 'FirstPartyModeSetupBanner', () => {
} );
} );

it( 'should display the error message when the CTA button is clicked and the request fails', async () => {
fetchMock.postOnce( fpmSettingsEndpoint, {
body: JSON.stringify( {
code: 'test_error',
message: 'Test Error',
data: {
reason: 'test_reason',
},
} ),
status: 500,
} );

const { getByRole, getByText, waitForRegistry } = render(
<FPMBannerComponent />,
{
registry,
viewContext: VIEW_CONTEXT_MAIN_DASHBOARD,
}
);

await waitForRegistry();

fetchMock.post( dismissItemEndpoint, {
body: JSON.stringify( [ FPM_SETUP_CTA_BANNER_NOTIFICATION ] ),
status: 200,
} );

fireEvent.click(
getByRole( 'button', {
name: 'Enable First-party mode',
} )
);

await waitFor( () => {
expect( fetchMock ).toHaveFetched( fpmSettingsEndpoint );
expect( fetchMock ).not.toHaveFetched( dismissItemEndpoint );
} );

expect( getByText( 'Error: Test Error' ) ).toBeInTheDocument();
} );

it( 'should set FPM_SHOW_SETUP_SUCCESS_NOTIFICATION to true and invalidate the notifications queue resolution when the CTA button is clicked', async () => {
const { getByRole, waitForRegistry } = render( <FPMBannerComponent />, {
registry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import PropTypes from 'prop-types';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -33,8 +39,10 @@ export default function ActionsCTALinkDismiss( {
ctaLink,
ctaLabel,
onCTAClick,
ctaDismissOptions,
onDismiss = () => {},
dismissLabel = __( 'OK, Got it!', 'google-site-kit' ),
dismissOnCTAClick = true,
dismissExpires = 0,
dismissOptions = {},
} ) {
Expand All @@ -45,24 +53,42 @@ export default function ActionsCTALinkDismiss( {
} );

return (
<div className={ className }>
<CTALink
id={ id }
ctaLink={ ctaLink }
ctaLabel={ ctaLabel }
onCTAClick={ onCTAClick }
dismissExpires={ dismissExpires }
/>
<Fragment>
<div className={ className }>
<CTALink
id={ id }
ctaLink={ ctaLink }
ctaLabel={ ctaLabel }
onCTAClick={ onCTAClick }
dismissOnCTAClick={ dismissOnCTAClick }
dismissExpires={ dismissExpires }
dismissOptions={ ctaDismissOptions }
/>

<Dismiss
id={ id }
primary={ false }
dismissLabel={ dismissLabel }
dismissExpires={ dismissExpires }
disabled={ isNavigatingToCTALink }
onDismiss={ onDismiss }
dismissOptions={ dismissOptions }
/>
</div>
<Dismiss
id={ id }
primary={ false }
dismissLabel={ dismissLabel }
dismissExpires={ dismissExpires }
disabled={ isNavigatingToCTALink }
onDismiss={ onDismiss }
dismissOptions={ dismissOptions }
/>
</div>
</Fragment>
);
}

ActionsCTALinkDismiss.propTypes = {
id: PropTypes.string,
className: PropTypes.string,
ctaLink: PropTypes.string,
ctaLabel: PropTypes.string,
onCTAClick: PropTypes.func,
onDismiss: PropTypes.func,
ctaDismissOptions: PropTypes.object,
dismissLabel: PropTypes.string,
dismissOnCTAClick: PropTypes.bool,
dismissExpires: PropTypes.number,
dismissOptions: PropTypes.object,
};
24 changes: 20 additions & 4 deletions assets/js/googlesitekit/notifications/components/common/CTALink.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { useState } from '@wordpress/element';
import { useDispatch, useSelect } from 'googlesitekit-data';
import { CORE_NOTIFICATIONS } from '../../datastore/constants';
import { CORE_LOCATION } from '../../../datastore/location/constants';
import { CORE_SITE } from '../../../datastore/site/constants';
import useNotificationEvents from '../../hooks/useNotificationEvents';
import { SpinnerButton } from 'googlesitekit-components';

Expand All @@ -39,7 +40,9 @@ export default function CTALink( {
ctaLink,
ctaLabel,
onCTAClick,
dismissExpires = -1,
dismissOnCTAClick = false,
dismissExpires = 0,
dismissOptions = { skipHidingFromQueue: true },
} ) {
const [ isAwaitingCTAResponse, setIsAwaitingCTAResponse ] =
useState( false );
Expand All @@ -53,28 +56,39 @@ export default function CTALink( {
: false;
} );

const { clearError, receiveError } = useDispatch( CORE_SITE );

const { dismissNotification } = useDispatch( CORE_NOTIFICATIONS );
const { navigateTo } = useDispatch( CORE_LOCATION );

const handleCTAClick = async ( event ) => {
clearError( 'notificationAction', [ id ] );

event.persist();
if ( ! event.defaultPrevented && ctaLink ) {
event.preventDefault();
}

setIsAwaitingCTAResponse( true );
await onCTAClick?.( event );

const { error } = ( await onCTAClick?.( event ) ) || {};

if ( isMounted() ) {
setIsAwaitingCTAResponse( false );
}

if ( error ) {
receiveError( error, 'notificationAction', [ id ] );
return;
}

const ctaClickActions = [ trackEvents.confirm() ];

if ( dismissExpires >= 0 ) {
if ( dismissOnCTAClick ) {
ctaClickActions.push(
dismissNotification( id, {
...dismissOptions,
expiresInSeconds: dismissExpires,
skipHidingFromQueue: true,
} )
);
}
Expand Down Expand Up @@ -105,5 +119,7 @@ CTALink.propTypes = {
ctaLink: PropTypes.string,
ctaLabel: PropTypes.string,
onCTAClick: PropTypes.func,
dismissOnCTAClick: PropTypes.bool,
dismissExpires: PropTypes.number,
dismissOptions: PropTypes.object,
};
53 changes: 53 additions & 0 deletions assets/js/googlesitekit/notifications/components/common/Error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Site Kit by Google, Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import PropTypes from 'prop-types';

/*
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useDispatch, useSelect } from 'googlesitekit-data';
import { CORE_SITE } from '../../../datastore/site/constants';
import ErrorText from '../../../../components/ErrorText';

export default function Error( { id } ) {
const ctaError = useSelect( ( select ) => {
return select( CORE_SITE ).getError( 'notificationAction', [ id ] );
} );

const { clearError } = useDispatch( CORE_SITE );

useEffect( () => {
return () => {
clearError( 'notificationAction', [ id ] );
};
}, [ clearError, id ] );

return ctaError ? <ErrorText message={ ctaError.message } /> : null;
}

// eslint-disable-next-line sitekit/acronym-case
Error.propTypes = {
id: PropTypes.string,
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
useBreakpoint,
} from '../../../../hooks/useBreakpoint';
import { Cell, Grid, Row } from '../../../../material-components';
import Error from '../common/Error';

export default function NotificationWithSVG( {
id,
Expand Down Expand Up @@ -77,6 +78,7 @@ export default function NotificationWithSVG( {

{ description }

<Error id={ id } />
{ actions }
</Cell>
<Cell
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading