52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
const currentUrl = window.location.href;
|
|
|
|
// handle key presses (close/confirm)
|
|
document.addEventListener("keydown", async function(event) {
|
|
if (event.isComposing || event.key === 'Escape') {
|
|
Array.from(document.getElementsByClassName("popup")).forEach(function(x) {
|
|
x.style.display = 'none';
|
|
});
|
|
document.getElementById("publickeyadd").innerText = "";
|
|
} else if (event.key === 'Enter') {
|
|
if (addPopup.style.display == 'flex') {
|
|
await addConfirm();
|
|
}
|
|
}
|
|
});
|
|
|
|
// add popup
|
|
document.getElementById("add").addEventListener("click", function () {
|
|
addPopup.style.display = 'flex';
|
|
});
|
|
// cancel
|
|
document.getElementById("addcancel").addEventListener("click", function () {
|
|
addPopup.style.display = 'none';
|
|
document.getElementById("publickeyadd").innerText = "";
|
|
});
|
|
//confirm
|
|
document.getElementById("addconfirm").addEventListener("click", async () => {
|
|
await addConfirm();
|
|
});
|
|
|
|
export async function addConfirm() {
|
|
const apiUrl = `${currentUrl}chat/add`;
|
|
const inputFieldPublicKey = document.getElementById("publickeyadd");
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
pubkey: inputFieldPublicKey.value
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to add');
|
|
} else {
|
|
inputFieldPublicKey.value = '';
|
|
location.reload();
|
|
}
|
|
}
|