notice-response.js
69 lines
| 1 | /* global srfmNoticeResponse */ |
| 2 | ( function () { |
| 3 | const notices = { |
| 4 | 'srfm-getting-started-notice': { |
| 5 | primary: 'go_to_dashboard', |
| 6 | snooze: 'maybe_later', |
| 7 | dismiss: 'dismissed', |
| 8 | }, |
| 9 | 'srfm-plugin-review-notice': { |
| 10 | primary: 'rate_sureforms', |
| 11 | snooze: 'maybe_later', |
| 12 | dismiss: 'dismissed', |
| 13 | }, |
| 14 | }; |
| 15 | |
| 16 | function getAction( el, noticeId ) { |
| 17 | const config = notices[ noticeId ]; |
| 18 | if ( ! config ) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | if ( |
| 23 | el.classList.contains( 'button-primary' ) || |
| 24 | ( el.classList.contains( 'astra-notice-close' ) && |
| 25 | el.getAttribute( 'target' ) === '_blank' ) |
| 26 | ) { |
| 27 | return config.primary; |
| 28 | } |
| 29 | if ( el.hasAttribute( 'data-repeat-notice-after' ) ) { |
| 30 | return config.snooze; |
| 31 | } |
| 32 | if ( el.classList.contains( 'astra-notice-close' ) ) { |
| 33 | return config.dismiss; |
| 34 | } |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | function sendResponse( noticeId, button ) { |
| 39 | const body = new FormData(); |
| 40 | body.append( 'action', 'srfm_notice_response' ); |
| 41 | body.append( 'nonce', srfmNoticeResponse.nonce ); |
| 42 | body.append( 'notice_id', noticeId ); |
| 43 | body.append( 'button', button ); |
| 44 | |
| 45 | fetch( srfmNoticeResponse.ajaxurl, { method: 'POST', body } ).catch( |
| 46 | () => {} |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | Object.keys( notices ).forEach( function ( noticeId ) { |
| 51 | const container = document.getElementById( noticeId ); |
| 52 | if ( ! container ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | container.addEventListener( 'click', function ( e ) { |
| 57 | const link = e.target.closest( 'a' ); |
| 58 | if ( ! link ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | const action = getAction( link, noticeId ); |
| 63 | if ( action ) { |
| 64 | sendResponse( noticeId, action ); |
| 65 | } |
| 66 | } ); |
| 67 | } ); |
| 68 | }() ); |
| 69 |