CensoredManager/assets/js/create.js
mctaylors 5f64caf6bc
Initial commit
Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
2024-06-06 22:39:59 +05:00

43 lines
No EOL
1.7 KiB
JavaScript

const token = docCookies.getItem("token");
const notificationElement = document.getElementById("notification");
const balanceElement = document.getElementById("balance");
let balance;
window.addEventListener("load", async function () {
refreshBalance(await getBalance(token));
});
function refreshBalance(from) {
balance = from;
balanceElement.innerText = balance;
}
document.getElementById("create-form").addEventListener("submit", async (e) => {
e.preventDefault();
const act = parseInt(document.getElementById("act").value);
if (act <= 0) {
notificationElement.innerText = "Activation amount cannot be lower than 0!";
notificationElement.style.color = "red";
return;
}
const amount = parseInt(document.getElementById("amount").value);
const bottleneck = Math.floor(balance / act);
if (amount <= 0 || amount > bottleneck) {
notificationElement.innerText = `Incorrect coin amount. (1-${bottleneck} allowed)`;
notificationElement.style.color = "red";
return;
}
const createResult = await createCheque(token, amount, act);
if (createResult['status']) {
notificationElement.innerHTML = `Cheque has been created.
<button type="button" onclick="navigator.clipboard.writeText(${createResult['id']})">Copy ID</button>
<button type="button" onclick="navigator.clipboard.writeText(
'https://t.me/${telegram_bot}?start=${createResult['id']}')">Copy link</button>`;
notificationElement.style.color = "green";
refreshBalance(createResult['balance']);
} else {
notificationElement.innerText = "Incorrect ID!";
notificationElement.style.color = "red";
}
});