| 1 |
jQuery(document).ready(function () { |
| 2 |
const slug = "yaymail"; |
| 3 |
const REST_URL = |
| 4 |
window[`${slug}LicenseData`].apiSettings.restUrl; |
| 5 |
const REST_NONCE = window[`${slug}LicenseData`].apiSettings.restNonce; |
| 6 |
const POST_OPTIONS = { |
| 7 |
method: "POST", |
| 8 |
headers: { |
| 9 |
"Content-type": "application/json", |
| 10 |
"x-wp-nonce": REST_NONCE |
| 11 |
}, |
| 12 |
}; |
| 13 |
|
| 14 |
jQuery(".yaycommerce-license-layout").on( |
| 15 |
"click", |
| 16 |
`.yaycommerce-activate-license-button[data-plugin*='${slug}']`, |
| 17 |
handleActivate |
| 18 |
); |
| 19 |
jQuery(".yaycommerce-license-layout").on( |
| 20 |
"click", |
| 21 |
`.yaycommerce-update-license[data-plugin*='${slug}']`, |
| 22 |
handleUpdate |
| 23 |
); |
| 24 |
jQuery(".yaycommerce-license-layout").on( |
| 25 |
"click", |
| 26 |
`.yaycommerce-remove-license[data-plugin*='${slug}']`, |
| 27 |
handleRemove |
| 28 |
); |
| 29 |
jQuery(".yaycommerce-license-layout").on( |
| 30 |
"click", |
| 31 |
`#${slug}_license_card .yaycommerce-license-message .yaycommerce-license-message__close`, |
| 32 |
function( ) {hideMessage(slug) } |
| 33 |
); |
| 34 |
|
| 35 |
async function handleActivate(event) { |
| 36 |
event.preventDefault(); |
| 37 |
clearMessages(); |
| 38 |
const { plugin } = jQuery(this).data(); |
| 39 |
beforeCallAPI(plugin, "activate"); |
| 40 |
hideMessage(plugin); |
| 41 |
const licenseKey = jQuery(`#${plugin}_license_input`).val(); |
| 42 |
|
| 43 |
const response = await fetch(`${REST_URL}/license/activate`, { |
| 44 |
...POST_OPTIONS, |
| 45 |
body: JSON.stringify({ |
| 46 |
license_key: licenseKey, |
| 47 |
plugin, |
| 48 |
}), |
| 49 |
}).then((response) => response.json()); |
| 50 |
afterCallAPI(plugin, "activate"); |
| 51 |
if (response.success) { |
| 52 |
replaceSuccessfullContent(response); |
| 53 |
} |
| 54 |
showMessage(response, "activate"); |
| 55 |
} |
| 56 |
|
| 57 |
async function handleUpdate(event) { |
| 58 |
event.preventDefault(); |
| 59 |
clearMessages(); |
| 60 |
const { plugin } = jQuery(this).data(); |
| 61 |
beforeCallAPI(plugin, "update"); |
| 62 |
const response = await fetch(`${REST_URL}/license/update`, { |
| 63 |
...POST_OPTIONS, |
| 64 |
body: JSON.stringify({ |
| 65 |
plugin, |
| 66 |
}), |
| 67 |
}).then((response) => response.json()); |
| 68 |
afterCallAPI(plugin, "update"); |
| 69 |
if (response.success) { |
| 70 |
replaceSuccessfullContent(response); |
| 71 |
} else { |
| 72 |
replaceActivatorContent(response); |
| 73 |
} |
| 74 |
showMessage(response, "update"); |
| 75 |
} |
| 76 |
async function handleRemove(event) { |
| 77 |
event.preventDefault(); |
| 78 |
clearMessages(); |
| 79 |
const { plugin } = jQuery(this).data(); |
| 80 |
beforeCallAPI(plugin, "remove"); |
| 81 |
const response = await fetch(`${REST_URL}/license/delete`, { |
| 82 |
...POST_OPTIONS, |
| 83 |
body: JSON.stringify({ |
| 84 |
plugin, |
| 85 |
}), |
| 86 |
}).then((response) => response.json()); |
| 87 |
afterCallAPI(plugin, "remove"); |
| 88 |
replaceActivatorContent(response); |
| 89 |
showMessage({success: true}, "remove") |
| 90 |
} |
| 91 |
|
| 92 |
function replaceSuccessfullContent(data) { |
| 93 |
jQuery(`#${data.slug}_license_card`).replaceWith( data.html ); |
| 94 |
} |
| 95 |
function replaceActivatorContent(data) { |
| 96 |
jQuery(`#${data.slug}_license_card`).replaceWith( data.html ); |
| 97 |
} |
| 98 |
|
| 99 |
function hideMessage(slug) { |
| 100 |
jQuery(`#${slug}_license_card .yaycommerce-license-message`).removeClass( |
| 101 |
"show" |
| 102 |
); |
| 103 |
jQuery(`#${slug}_license_card .yaycommerce-license-message`).html(""); |
| 104 |
} |
| 105 |
function clearMessages() { |
| 106 |
jQuery(".message").removeClass("active"); |
| 107 |
} |
| 108 |
function showMessage(data, action) { |
| 109 |
const { slug, success, message } = data; |
| 110 |
if ( success ) { |
| 111 |
const id = `message-${action}-success`; |
| 112 |
document.getElementById(id).classList.add("active"); |
| 113 |
setTimeout(() => { |
| 114 |
document.getElementById(id).classList.remove("active"); |
| 115 |
}, 2000); |
| 116 |
} else { |
| 117 |
const id = `message-${action}-error`; |
| 118 |
document.getElementById(id).classList.add("active"); |
| 119 |
setTimeout(() => { |
| 120 |
document.getElementById(id).classList.remove("active"); |
| 121 |
}, 2000); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
function beforeCallAPI(plugin, action) { |
| 127 |
if (action === "activate") { |
| 128 |
jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`) |
| 129 |
.find(".activate-loading") |
| 130 |
.css("display", "inline-flex"); |
| 131 |
} |
| 132 |
|
| 133 |
if (action === "update") { |
| 134 |
jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`) |
| 135 |
.find(".activate-loading") |
| 136 |
.css("display", "inline-flex"); |
| 137 |
} |
| 138 |
|
| 139 |
if (action === "remove") { |
| 140 |
jQuery(`.yaycommerce-remove-license[data-plugin=${plugin}]`) |
| 141 |
.find(".activate-loading") |
| 142 |
.css("display", "inline-flex"); |
| 143 |
} |
| 144 |
|
| 145 |
jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`).attr( |
| 146 |
"disabled", |
| 147 |
true |
| 148 |
); |
| 149 |
jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`).attr( |
| 150 |
"disabled", |
| 151 |
true |
| 152 |
); |
| 153 |
jQuery(`.yaycommerce-remove-license[data-plugin="${plugin}"]`).attr( |
| 154 |
"disabled", |
| 155 |
true |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
function afterCallAPI(plugin, action) { |
| 160 |
if (action === "activate") { |
| 161 |
jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`) |
| 162 |
.find(".activate-loading") |
| 163 |
.hide(); |
| 164 |
} |
| 165 |
|
| 166 |
if (action === "update") { |
| 167 |
jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`) |
| 168 |
.find(".activate-loading") |
| 169 |
.hide(); |
| 170 |
} |
| 171 |
|
| 172 |
if (action === "remove") { |
| 173 |
jQuery(`.yaycommerce-remove-license[data-plugin=${plugin}]`) |
| 174 |
.find(".activate-loading") |
| 175 |
.hide(); |
| 176 |
} |
| 177 |
jQuery(`.yaycommerce-activate-license-button[data-plugin=${plugin}]`).attr( |
| 178 |
"disabled", |
| 179 |
false |
| 180 |
); |
| 181 |
jQuery(`.yaycommerce-update-license[data-plugin=${plugin}]`).attr( |
| 182 |
"disabled", |
| 183 |
false |
| 184 |
); |
| 185 |
jQuery(`.yaycommerce-remove-license[data-plugin="${plugin}"]`).attr( |
| 186 |
"disabled", |
| 187 |
false |
| 188 |
); |
| 189 |
} |
| 190 |
}); |