Skip to content

Commit

Permalink
Stats: Fix stats setting role toggles not sticking (#40853)
Browse files Browse the repository at this point in the history
* fix toggles

* changelog

* includes is not necessary

* make the condition check more robust

* use a const array
  • Loading branch information
kangzj authored Jan 6, 2025
1 parent 7c21388 commit be744e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
51 changes: 16 additions & 35 deletions projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ class SiteStatsComponent extends React.Component {
wpcom_reader_views_enabled: props.getOptionValue( 'wpcom_reader_views_enabled' ),
};

if ( roles ) {
this.addCustomCountRolesState( countRoles );
this.addCustomRolesState( roles );
const defaultRoles = [ 'administrator', 'editor', 'author', 'contributor', 'subscriber' ];

if ( roles?.length > 0 ) {
roles.forEach( role => {
if ( ! defaultRoles.includes( role ) ) {
this.state[ `roles_${ role }` ] = true;
}
} );
}

if ( countRoles?.length > 0 ) {
countRoles.forEach( role => {
if ( ! defaultRoles.includes( role ) ) {
this.state[ `count_roles_${ role }` ] = true;
}
} );
}
}

Expand Down Expand Up @@ -107,38 +120,6 @@ class SiteStatsComponent extends React.Component {
return () => this.updateOptions( role, setting );
};

/**
* Allows for custom roles 'count logged in page views' stats settings to be added to the current state.
*
* @param {Array} countRoles - All roles (including custom) that have 'count logged in page views' enabled.
*/
addCustomCountRolesState( countRoles ) {
countRoles.forEach( role => {
if (
! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes(
countRoles
)
) {
this.setState( { [ `count_roles_${ role }` ]: includes( countRoles, role, false ) } );
}
} );
}

/**
* Allows for custom roles 'allow stats reports' stats settings to be added to the current state.
*
* @param {Array} roles - All roles (including custom) that have 'allow stats reports' enabled.
*/
addCustomRolesState( roles ) {
roles.forEach( role => {
if (
! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role )
) {
this.setState( { [ `roles_${ role }` ]: includes( roles, role, false ) } );
}
} );
}

handleStatsOptionToggle( option_slug ) {
return () => this.props.updateFormStateModuleOption( 'stats', option_slug );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Fix custom roles settings are not sticking for Jetpack Stats

0 comments on commit be744e8

Please sign in to comment.