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