Skip to content

Commit

Permalink
Fix Analytics Dashboard rendering for malformed preferences (woocomme…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalec authored Oct 11, 2024
1 parent 8502f51 commit e0e6886
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-51844-dashboard_sections
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix Analytics Dashboard rendering for malformed preferences.
15 changes: 13 additions & 2 deletions plugins/woocommerce/client/admin/client/dashboard/customizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
* Internal dependencies
*/
import './style.scss';
import defaultSections from './default-sections';
import defaultSections, { DEFAULT_SECTIONS_FILTER } from './default-sections';
import Section from './section';
import ReportFilters from '../analytics/components/report-filters';

Expand All @@ -46,11 +46,22 @@ const DASHBOARD_FILTERS_FILTER = 'woocommerce_admin_dashboard_filters';
const filters = applyFilters( DASHBOARD_FILTERS_FILTER, [] );

const mergeSectionsWithDefaults = ( prefSections ) => {
if ( ! prefSections || prefSections.length === 0 ) {
if (
! prefSections ||
! Array.isArray( prefSections ) ||
prefSections.length === 0
) {
return defaultSections.reduce( ( sections, section ) => {
return [ ...sections, { ...section } ];
}, [] );
}

if ( ! Array.isArray( defaultSections ) ) {
throw new Error(
`The \`defaultSections\` is not an array, please make sure \`${ DEFAULT_SECTIONS_FILTER }\` filter is used correctly.`
);
}

const defaultKeys = defaultSections.map( ( section ) => section.key );
const prefKeys = prefSections.map( ( section ) => section.key );
const keys = new Set( [ ...prefKeys, ...defaultKeys ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const StorePerformance = ( props ) => (
</Suspense>
);

const DEFAULT_SECTIONS_FILTER = 'woocommerce_dashboard_default_sections';
export const DEFAULT_SECTIONS_FILTER = 'woocommerce_dashboard_default_sections';

/**
* An object defining a dashboard section.
Expand Down

0 comments on commit e0e6886

Please sign in to comment.