global.js
146 lines
| 1 | class ConsentFormHandler { |
| 2 | constructor() { |
| 3 | this.initializeEventListeners(); |
| 4 | } |
| 5 | |
| 6 | initializeEventListeners() { |
| 7 | document.addEventListener('click', (e) => { |
| 8 | if (e.target.closest('.avc_consent_form_launcher')) { |
| 9 | this.showConsentForm(e.target.closest('.avc_consent_form_launcher')); |
| 10 | } |
| 11 | }); |
| 12 | |
| 13 | // Close consent form |
| 14 | document.addEventListener('click', (e) => { |
| 15 | if (e.target.closest('.avc_user_consent_close')) { |
| 16 | this.hideConsentForm(); |
| 17 | } |
| 18 | }); |
| 19 | |
| 20 | // Handle consent submission |
| 21 | document.addEventListener('click', (e) => { |
| 22 | if (e.target.closest('.avc_user_consent_button')) { |
| 23 | this.handleConsentSubmission(e.target.closest('.avc_user_consent_button')); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | showConsentForm(launcher) { |
| 29 | launcher.classList.add('avc_hide_launcher'); |
| 30 | document.querySelector('.avc_user_consent_container')?.classList.add('avc_show'); |
| 31 | } |
| 32 | |
| 33 | hideConsentForm() { |
| 34 | document.querySelector('.avc_user_consent_container')?.classList.remove('avc_show'); |
| 35 | } |
| 36 | |
| 37 | async handleConsentSubmission(button) { |
| 38 | try { |
| 39 | this.setLoadingState(button, true); |
| 40 | |
| 41 | const firstResponse = await this.submitUserConsent(); |
| 42 | |
| 43 | if (firstResponse?.data) { |
| 44 | await this.makeChainedRequest(firstResponse.data); |
| 45 | this.redirectWithConsentParam(); |
| 46 | } else { |
| 47 | throw new Error('Invalid response from consent submission'); |
| 48 | } |
| 49 | } catch (error) { |
| 50 | console.error('Consent submission failed:', error); |
| 51 | alert('Something went wrong. Please try again.'); |
| 52 | } finally { |
| 53 | this.setLoadingState(button, false); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | async submitUserConsent() { |
| 58 | const formData = new FormData(); |
| 59 | formData.append('action', 'avcf_user_consent'); |
| 60 | formData.append('avc_nonce', window.avc_site_data?.avc_nonce || ''); |
| 61 | |
| 62 | const response = await fetch(window.avcajax?.ajaxurl || '/wp-admin/admin-ajax.php', { |
| 63 | method: 'POST', |
| 64 | body: formData, |
| 65 | }); |
| 66 | |
| 67 | if (!response.ok) { |
| 68 | throw new Error(`HTTP error! status: ${response.status}`); |
| 69 | } |
| 70 | |
| 71 | return response.json(); |
| 72 | } |
| 73 | |
| 74 | async makeChainedRequest(data) { |
| 75 | const response = await fetch(data.apiurl, { |
| 76 | method: 'POST', |
| 77 | headers: { |
| 78 | 'Content-Type': 'application/json', |
| 79 | 'api-key': data.apikey, |
| 80 | }, |
| 81 | credentials: 'include', |
| 82 | body: JSON.stringify(data), |
| 83 | }); |
| 84 | |
| 85 | if (!response.ok) { |
| 86 | throw new Error(`Chained request failed! status: ${response.status}`); |
| 87 | } |
| 88 | |
| 89 | const result = await response.json(); |
| 90 | |
| 91 | if (!result) { |
| 92 | throw new Error('Second request returned invalid response'); |
| 93 | } |
| 94 | |
| 95 | await this.setConsentStatusOnServer(); |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | redirectWithConsentParam() { |
| 101 | const currentUrl = new URL(window.location.href); |
| 102 | window.location.href = currentUrl.toString(); |
| 103 | } |
| 104 | |
| 105 | setLoadingState(button, isLoading) { |
| 106 | const loader = document.querySelector('.avc_consent_loader'); |
| 107 | |
| 108 | if (isLoading) { |
| 109 | button.classList.add('loading'); |
| 110 | loader?.style.setProperty('display', 'block'); |
| 111 | } else { |
| 112 | button.classList.remove('loading'); |
| 113 | loader?.style.setProperty('display', 'none'); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | async setConsentStatusOnServer() { |
| 118 | const formData = new FormData(); |
| 119 | formData.append('action', 'avcf_set_user_consent_status'); |
| 120 | formData.append('avc_nonce', window.avc_site_data?.avc_nonce || ''); |
| 121 | |
| 122 | const response = await fetch(window.avcajax?.ajaxurl || '/wp-admin/admin-ajax.php', { |
| 123 | method: 'POST', |
| 124 | body: formData, |
| 125 | credentials: 'include', |
| 126 | }); |
| 127 | |
| 128 | if (!response.ok) { |
| 129 | throw new Error(`Failed to update user meta. Status: ${response.status}`); |
| 130 | } |
| 131 | |
| 132 | const result = await response.json(); |
| 133 | if (!result.success) { |
| 134 | throw new Error(result.data?.message || 'Unknown error from consent meta update'); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Initialize when DOM is ready |
| 140 | if (document.readyState === 'loading') { |
| 141 | document.addEventListener('DOMContentLoaded', () => { |
| 142 | new ConsentFormHandler(); |
| 143 | }); |
| 144 | } else { |
| 145 | new ConsentFormHandler(); |
| 146 | } |