-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21781 from Yoast/21724-new-general-page-updating-…
…counters-properly-new 21724 new general page updating counters properly new
- Loading branch information
Showing
12 changed files
with
851 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
|
||
export { useNotificationCountSync } from "./use-notification-count-sync"; | ||
export { default as useSelectGeneralPage } from "./use-select-general-page"; |
16 changes: 16 additions & 0 deletions
16
packages/js/src/general/hooks/use-notification-count-sync.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useSelect } from "@wordpress/data"; | ||
import { useEffect } from "@wordpress/element"; | ||
import { updateNotificationsCount } from "../../shared-admin/helpers"; | ||
import { STORE_NAME } from "../constants"; | ||
|
||
/** | ||
* Sync the notification count with the Yoast menu and admin bar. | ||
* @returns {void} | ||
*/ | ||
export const useNotificationCountSync = () => { | ||
const activeAlertCount = useSelect( ( select ) => select( STORE_NAME ).selectActiveAlertsCount(), [] ); | ||
|
||
useEffect( () => { | ||
updateNotificationsCount( activeAlertCount ); | ||
}, [ activeAlertCount ] ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { fixWordPressMenuScrolling } from "./fix-wordpress-menu-scrolling"; | ||
export { updateNotificationsCount } from "./notifications-count"; |
41 changes: 41 additions & 0 deletions
41
packages/js/src/shared-admin/helpers/notifications-count.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { _n, sprintf } from "@wordpress/i18n"; | ||
|
||
/** | ||
* Update the text content of an element if it exists. | ||
* @param {HTMLElement} root The root element. | ||
* @param {string} selector The selector. | ||
* @param {string} text The text. | ||
* @returns {HTMLElement|null} The element or null. | ||
*/ | ||
const updateTextContentIfElementExists = ( root, selector, text ) => { | ||
const element = root.querySelector( selector ); | ||
if ( element ) { | ||
element.textContent = text; | ||
} | ||
return element; | ||
}; | ||
|
||
/** | ||
* Update the notification total in the Yoast SEO menu and the admin bar badges. | ||
* @param {number} total The total number of notifications. | ||
* @returns {void} | ||
*/ | ||
export const updateNotificationsCount = ( total ) => { | ||
// Note: these translation is the same as on the server side. | ||
/* translators: Hidden accessibility text; %s: number of notifications. */ | ||
const screenReaderText = sprintf( _n( "%s notification", "%s notifications", total, "wordpress-seo" ), total ); | ||
|
||
const menuItems = document.querySelectorAll( "#toplevel_page_wpseo_dashboard .update-plugins" ); | ||
for ( const menuItem of menuItems ) { | ||
menuItem.className = `update-plugins count-${ total }`; | ||
updateTextContentIfElementExists( menuItem, ".plugin-count", String( total ) ); | ||
updateTextContentIfElementExists( menuItem, ".screen-reader-text", screenReaderText ); | ||
} | ||
|
||
const adminBarItems = document.querySelectorAll( "#wp-admin-bar-wpseo-menu .yoast-issue-counter" ); | ||
for ( const adminBar of adminBarItems ) { | ||
adminBar.classList.toggle( "yst-hidden", total === 0 ); | ||
updateTextContentIfElementExists( adminBar, ".yoast-issues-count", String( total ) ); | ||
updateTextContentIfElementExists( adminBar, ".screen-reader-text", screenReaderText ); | ||
} | ||
}; |
Oops, something went wrong.