assets
5 months ago
add-form-notice.js
5 months ago
connection-notice.js
5 months ago
connection-notice.scss
9 months ago
notice.scss
5 months ago
connection-notice.js
58 lines
| 1 | import './notice.scss'; |
| 2 | |
| 3 | (function () { |
| 4 | |
| 5 | const getNotice = () => { |
| 6 | return document.getElementById('hostinger-reach-connection-notice'); |
| 7 | } |
| 8 | |
| 9 | const handleAction = (choice) => { |
| 10 | const data = new FormData(); |
| 11 | data.append('action', hostinger_reach_connection_notice_data.action); |
| 12 | data.append('nonce', hostinger_reach_connection_notice_data.nonce); |
| 13 | data.append('choice', choice); |
| 14 | |
| 15 | fetch(hostinger_reach_connection_notice_data.ajaxurl, { |
| 16 | method: 'POST', |
| 17 | body: data |
| 18 | }) |
| 19 | .then(response =>{ |
| 20 | return response.json(); |
| 21 | }) |
| 22 | .then(result => { |
| 23 | if (result?.success && result.data?.redirect_url) { |
| 24 | window.location.href = result.data.redirect_url; |
| 25 | } |
| 26 | |
| 27 | const notice = getNotice(); |
| 28 | const closer = getNotice().querySelector('.notice-dismiss'); |
| 29 | if ( closer ) { |
| 30 | closer.click(); |
| 31 | } |
| 32 | |
| 33 | if ( notice ) { |
| 34 | notice.style.display = 'none'; |
| 35 | } |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | const init = () => { |
| 40 | const notice = getNotice(); |
| 41 | |
| 42 | if (!notice || !hostinger_reach_connection_notice_data) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | const actions = notice.querySelectorAll('.hostinger-reach-action-button'); |
| 47 | if (actions?.length) { |
| 48 | actions.forEach(( action ) => { |
| 49 | action.addEventListener('click', function (e) { |
| 50 | handleAction(e.target.dataset.action); |
| 51 | }); |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | document.addEventListener('DOMContentLoaded', init); |
| 57 | })(); |
| 58 |