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

Improve dismissItem behaviour in SiwG. #10095

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions assets/js/components/SettingsNotice/SettingsNotice.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ const SettingsNotice = forwardRef( ( props, ref ) => {
<Button
tertiary
onClick={ () => {
dismissItem( dismiss );
dismissCallback();
if ( typeof dismiss === 'string' ) {
dismissItem( dismiss );
}

dismissCallback?.();
} }
>
{ dismissLabel }
Expand All @@ -119,6 +122,7 @@ SettingsNotice.propTypes = {
LearnMore: PropTypes.elementType,
CTA: PropTypes.elementType,
OuterCTA: PropTypes.elementType,
dismiss: PropTypes.oneOfType( [ PropTypes.string, PropTypes.bool ] ),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why are we allowing dismiss to be a boolean as well? As the only intended type for this prop is supposed to be the string, and default value is already set to the empty string if prop is not passed - the logic won't work with boolean value anyway (if we pass true for example, no dismissal will happen, but we will not get error until we attempt to dismiss it when invariant in the action will throw an error)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct! This was here before when I was testing things out, it can/should just be string now. I've updated it, thanks 😀

dismissLabel: PropTypes.string,
dismissCallback: PropTypes.func,
};
Expand Down
3 changes: 2 additions & 1 deletion assets/js/googlesitekit/datastore/user/dismissed-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const baseActions = {
dismissItem: createValidatedAction(
( slug, options = {} ) => {
const { expiresInSeconds = 0 } = options;
invariant( slug, 'A tour slug is required to dismiss a tour.' );
invariant( slug, 'A slug is required to dismiss an item.' );
invariant( typeof slug === 'string', 'A slug must be a string.' );
invariant(
Number.isInteger( expiresInSeconds ),
'expiresInSeconds must be an integer.'
Expand Down
10 changes: 10 additions & 0 deletions assets/js/googlesitekit/datastore/user/dismissed-items.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ describe( 'core/user dismissed-items', () => {
).toMatchObject( response );
expect( console ).toHaveErrored();
} );

it( 'should dispatch an error if the slug is not a string', async () => {
try {
await registry.dispatch( CORE_USER ).dismissItem( true );
} catch ( error ) {
expect( error.message ).toMatch(
'A slug must be a string.'
);
}
} );
} );

describe( 'removeDismissItems', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { __, _x, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useSelect, useDispatch } from 'googlesitekit-data';
import { useSelect } from 'googlesitekit-data';
import {
CORE_USER,
PERMISSION_MANAGE_OPTIONS,
Expand Down Expand Up @@ -74,8 +74,6 @@ export default function AnyoneCanRegisterDisabledNotice( { className } ) {
)
);

const { dismissItem } = useDispatch( CORE_USER );

if ( isDismissed === true || anyoneCanRegister === true ) {
return null;
}
Expand All @@ -88,10 +86,7 @@ export default function AnyoneCanRegisterDisabledNotice( { className } ) {
) }
type={ TYPE_INFO }
Icon={ InfoIcon }
dismiss
dismissCallback={ () =>
dismissItem( ANYONE_CAN_REGISTER_DISABLED_NOTICE )
}
dismiss={ ANYONE_CAN_REGISTER_DISABLED_NOTICE }
dismissLabel={ __( 'Got it', 'google-site-kit' ) }
notice={ createInterpolateElement(
sprintf(
Expand Down
Loading