index.js
34 lines
| 1 | // Code to handle showing/hiding admin notices (based on dismissed status in localStorage) |
| 2 | |
| 3 | document.addEventListener( 'DOMContentLoaded', () => { |
| 4 | // Select dismissable notices |
| 5 | const notices = document.querySelectorAll( 'div[data-give-dismissible]' ); |
| 6 | |
| 7 | // Apply for every dismissible GiveWP notice |
| 8 | notices.forEach( ( notice ) => { |
| 9 | // Generate storage id for notice |
| 10 | const storageId = `give-dismissed-${ notice.dataset.giveDismissible }`; |
| 11 | |
| 12 | // Retrieve timestamp of notice dismissal, if it has already happened |
| 13 | const storedItem = window.localStorage.getItem( storageId ); |
| 14 | |
| 15 | // If notice has not yet been dismissed continue |
| 16 | if ( ! storedItem ) { |
| 17 | // Show the notice, if it has not been dismissed |
| 18 | notice.classList.remove( 'hidden' ); |
| 19 | |
| 20 | // On dismissal click, add a record to local storage to that it remains hidden in the future |
| 21 | notice.addEventListener( 'click', ( e ) => { |
| 22 | if ( e.target.classList.contains( 'notice-dismiss' ) ) { |
| 23 | window.localStorage.setItem( storageId, Date.now() ); |
| 24 | } |
| 25 | |
| 26 | if ( e.target.classList.contains( 'give-donor-dashboard-upgrade-notice__dismiss-link' ) ) { |
| 27 | notice.classList.add( 'hidden' ); |
| 28 | window.localStorage.setItem( storageId, Date.now() ); |
| 29 | } |
| 30 | } ); |
| 31 | } |
| 32 | } ); |
| 33 | } ); |
| 34 |