Skip to content

Commit

Permalink
Refactor finishSetup to be more stable across renders.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv committed May 30, 2024
1 parent acad342 commit 9c11d30
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions assets/js/components/setup/ModuleSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
import PropTypes from 'prop-types';
import { useMount } from 'react-use';
import { useCallbackOne } from 'use-memo-one';

/**
* WordPress dependencies
Expand All @@ -40,7 +41,7 @@ import HelpMenu from '../help/HelpMenu';
import { Cell, Grid, Row } from '../../material-components';
import Header from '../Header';
import ModuleSetupFooter from './ModuleSetupFooter';
const { useSelect, useDispatch } = Data;
const { useSelect, useDispatch, useRegistry } = Data;

export default function ModuleSetup( { moduleSlug } ) {
const { navigateTo } = useDispatch( CORE_LOCATION );
Expand All @@ -49,18 +50,7 @@ export default function ModuleSetup( { moduleSlug } ) {
select( CORE_MODULES ).getModule( moduleSlug )
);

const args = {
notification: 'authentication_success',
};

if ( moduleSlug ) {
args.slug = moduleSlug;
}

const adminURL = useSelect( ( select ) =>
select( CORE_SITE ).getAdminURL( 'googlesitekit-dashboard', args )
);

const registry = useRegistry();
/**
* When module setup done, we redirect the user to Site Kit dashboard.
*
Expand All @@ -69,17 +59,32 @@ export default function ModuleSetup( { moduleSlug } ) {
*
* @param {string} [redirectURL] URL to redirect to when complete. Defaults to Site Kit dashboard.
*/
const finishSetup = useCallback(
const finishSetup = useCallbackOne(
async ( redirectURL ) => {
await trackEvent(
'moduleSetup',
'complete_module_setup',
moduleSlug
);

navigateTo( redirectURL || adminURL );
if ( redirectURL ) {
navigateTo( redirectURL );
return;
}

const { select, __experimentalResolveSelect: resolveSelect } =
registry;
await resolveSelect( CORE_SITE ).getSiteInfo();
const adminURL = select( CORE_SITE ).getAdminURL(
'googlesitekit-dashboard',
{
notification: 'authentication_success',
slug: moduleSlug,
}
);
navigateTo( adminURL );
},
[ adminURL, navigateTo, moduleSlug ]
[ registry, navigateTo, moduleSlug ]
);

const onCancelButtonClick = useCallback( async () => {
Expand Down

0 comments on commit 9c11d30

Please sign in to comment.