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 all 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.string,
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