blocks
1 week ago
vendor
4 years ago
admin-notices.js
1 year ago
admin.js
1 week ago
chart.js
1 week ago
wpp.js
1 week ago
wpp.min.js
1 week ago
admin-notices.js
78 lines
| 1 | 'use strict'; |
| 2 | |
| 3 | document.addEventListener('DOMContentLoaded', () => { |
| 4 | const btnNagDismiss = document.querySelector('.wpp-dismiss-performance-notice'); |
| 5 | const btnNagRemind = document.querySelector('.wpp-remind-performance-notice'); |
| 6 | |
| 7 | if ( btnNagDismiss && btnNagRemind ) { |
| 8 | btnNagDismiss.addEventListener('click', (e) => { |
| 9 | e.preventDefault(); |
| 10 | handleNagClick(e, '1'); |
| 11 | }); |
| 12 | |
| 13 | btnNagRemind.addEventListener('click', (e) => { |
| 14 | e.preventDefault(); |
| 15 | handleNagClick(e, '-1'); |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | function handleNagClick(e, dismissValue) { |
| 20 | const me = e.target; |
| 21 | |
| 22 | btnNagRemind.classList.add('disabled'); |
| 23 | btnNagDismiss.classList.add('disabled'); |
| 24 | |
| 25 | me.parentNode.querySelector('.spinner').classList.add('is-active'); |
| 26 | |
| 27 | ajax( |
| 28 | 'POST', |
| 29 | ajaxurl, |
| 30 | `action=wpp_handle_performance_notice&dismiss=${dismissValue}&token=${wpp_admin_notices_params.nonce_performance_nag}`, |
| 31 | (response) => handleNotice(response, me) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | function handleNotice(response, btn) { |
| 36 | const json = JSON.parse(response); |
| 37 | |
| 38 | if ( json.status === 'success' ) { |
| 39 | const notice = btn.parentNode.parentNode; |
| 40 | notice.parentNode.removeChild(notice); |
| 41 | } else { |
| 42 | alert('Something went wrong, please try again later'); |
| 43 | } |
| 44 | |
| 45 | btnNagRemind.classList.remove('disabled'); |
| 46 | btnNagDismiss.classList.remove('disabled'); |
| 47 | btn.parentNode.querySelector('.spinner').classList.remove('is-active'); |
| 48 | } |
| 49 | |
| 50 | function ajax(method, url, params, callback) { |
| 51 | const validMethods = ['GET', 'POST']; |
| 52 | const headers = { |
| 53 | 'X-Requested-With': 'XMLHttpRequest' |
| 54 | }; |
| 55 | |
| 56 | if ( ! validMethods.includes(method) ) { |
| 57 | method = 'GET'; |
| 58 | } |
| 59 | |
| 60 | if ( method === 'POST' ) { |
| 61 | headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 62 | } |
| 63 | |
| 64 | fetch(url + (method === 'GET' ? '?' + params : ''), { |
| 65 | method, |
| 66 | headers, |
| 67 | body: method === 'POST' ? params : null |
| 68 | }) |
| 69 | .then(response => { |
| 70 | if ( ! response.ok ) { |
| 71 | throw new Error('Network response was not ok'); |
| 72 | } |
| 73 | return response.text(); |
| 74 | }) |
| 75 | .then(data => callback(data)) |
| 76 | .catch(error => console.error('Fetch error:', error)); |
| 77 | } |
| 78 | }); |