Skip to content

Commit

Permalink
Harden updates of submenu items
Browse files Browse the repository at this point in the history
  • Loading branch information
pierlon committed Feb 22, 2021
1 parent 70941a5 commit 8d90f2d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions assets/src/amp-validation/counts/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isPlainObject } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -24,8 +19,6 @@ import './style.css';
* @param {number} count Count to set.
*/
function updateMenuItem( itemEl, count ) {
count = Math.abs( count );

if ( isNaN( count ) || count === 0 ) {
itemEl.parentNode.parentNode.removeChild( itemEl.parentNode );
} else {
Expand All @@ -37,18 +30,22 @@ function updateMenuItem( itemEl, count ) {
/**
* Updates the 'Validated URLs' and 'Error Index' menu items with their respective unreviewed count.
*
* @param {any} counts Counts for menu items.
* @param {Object} counts Counts for menu items.
* @param {number} counts.validated_urls Unreviewed validated URLs count.
* @param {number} counts.errors Unreviewed validation errors count.
*/
function updateMenuItemCounts( counts ) {
const { validated_urls: newValidatedUrlCount, errors: newErrorCount } = counts;

const errorCountEl = document.getElementById( 'new-error-index-count' );
const validatedUrlsCountEl = document.getElementById( 'new-validation-url-count' );
if ( errorCountEl ) {
updateMenuItem( errorCountEl, newErrorCount );
}

updateMenuItem( errorCountEl, newErrorCount );
updateMenuItem( validatedUrlsCountEl, newValidatedUrlCount );
const validatedUrlsCountEl = document.getElementById( 'new-validation-url-count' );
if ( validatedUrlsCountEl ) {
updateMenuItem( validatedUrlsCountEl, newValidatedUrlCount );
}
}

/**
Expand Down

0 comments on commit 8d90f2d

Please sign in to comment.