Fix setup wizard free features checkbox re-check itself (https://github.com/woocommerce/woocommerce-admin/pull/8169)

* Fix setup wizard free features checkbox re-check itself

* Add changelog
This commit is contained in:
Chi-Hsuan Huang 2022-01-17 10:03:32 +08:00 committed by GitHub
parent 666f740659
commit 409f077a83
3 changed files with 38 additions and 27 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: Fix
Fix setup wizard free features checkbox re-check itself. #8169

View File

@ -191,21 +191,26 @@ export const ExtensionSection = ( {
); );
}; };
export const createInstallExtensionOptions = ( extensions = [] ) => { export const createInstallExtensionOptions = ( {
const defaultInstallExtensionOptions = { install_extensions: true }; installableExtensions,
return extensions.reduce( ( acc, curr ) => { prevInstallExtensionOptions,
const plugins = curr.plugins.reduce( } ) => {
( pluginAcc, { key, selected } ) => { return installableExtensions.reduce( ( acc, curr ) => {
return { ...pluginAcc, [ key ]: selected ?? true }; const plugins = curr.plugins.reduce( ( pluginAcc, plugin ) => {
}, // If the option exists in the previous state, use that so the option won't be reset.
{} if ( prevInstallExtensionOptions.hasOwnProperty( plugin.key ) ) {
); return pluginAcc;
}
return {
...pluginAcc,
[ plugin.key ]: true,
};
}, {} );
return { return {
...acc, ...acc,
...plugins, ...plugins,
}; };
}, defaultInstallExtensionOptions ); }, prevInstallExtensionOptions );
}; };
export const SelectiveExtensionsBundle = ( { export const SelectiveExtensionsBundle = ( {
@ -216,9 +221,9 @@ export const SelectiveExtensionsBundle = ( {
industry, industry,
} ) => { } ) => {
const [ showExtensions, setShowExtensions ] = useState( false ); const [ showExtensions, setShowExtensions ] = useState( false );
const [ installExtensionOptions, setInstallExtensionOptions ] = useState( const [ installExtensionOptions, setInstallExtensionOptions ] = useState( {
createInstallExtensionOptions() install_extensions: true,
); } );
const { const {
freeExtensions: freeExtensionBundleByCategory, freeExtensions: freeExtensionBundleByCategory,
isResolving, isResolving,
@ -263,11 +268,16 @@ export const SelectiveExtensionsBundle = ( {
useEffect( () => { useEffect( () => {
if ( ! isInstallingActivating ) { if ( ! isInstallingActivating ) {
setInstallExtensionOptions( setInstallExtensionOptions( ( currInstallExtensionOptions ) =>
createInstallExtensionOptions( installableExtensions ) createInstallExtensionOptions( {
installableExtensions,
prevInstallExtensionOptions: currInstallExtensionOptions,
} )
); );
} }
}, [ installableExtensions, isInstallingActivating ] ); // Disable reason: This effect should only called when the installableExtensions are changed.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ installableExtensions ] );
const getCheckboxChangeHandler = ( key ) => { const getCheckboxChangeHandler = ( key ) => {
return ( checked ) => { return ( checked ) => {

View File

@ -103,34 +103,31 @@ describe( 'BusinessDetails', () => {
describe( 'createInstallExtensionOptions', () => { describe( 'createInstallExtensionOptions', () => {
test( 'selected by default', () => { test( 'selected by default', () => {
const extensions = [ const installableExtensions = [
{ {
plugins: [ plugins: [
{ {
key: 'visible-and-not-selected', key: 'visible-and-not-selected',
selected: false,
isVisible: () => true, isVisible: () => true,
}, },
{ {
key: 'visible-and-selected', key: 'visible-and-selected',
selected: true,
isVisible: () => true, isVisible: () => true,
}, },
{ {
key: 'this-should-not-show-at-all', key: 'this-should-not-show-at-all',
selected: true,
isVisible: () => false, isVisible: () => false,
}, },
], ],
}, },
]; ];
const values = createInstallExtensionOptions( const values = createInstallExtensionOptions( {
extensions, installableExtensions,
'US', prevInstallExtensionOptions: {
'', 'visible-and-not-selected': false,
[] },
); } );
expect( values ).toEqual( expect( values ).toEqual(
expect.objectContaining( { expect.objectContaining( {