secajs/public/javascript/createProfile.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-02-28 12:42:13 +01:00
const createProfileConfirmation = document.querySelector(
'#create-profile-success'
);
2025-02-20 16:33:46 +01:00
function debounce(func, wait) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
2025-02-20 20:01:50 +01:00
const validateNymInput = debounce(() => {
2025-02-20 16:33:46 +01:00
const nymValue = nymInput.value.trim();
const isValid = nymValue.length >= 3 && nymValue.length <= 128;
if (isValid) {
nymInputValidationWarning.style.display = 'none';
} else {
nymInputValidationWarning.style.display = 'block';
}
}, 500);
2025-02-20 16:33:46 +01:00
2025-02-20 20:01:50 +01:00
const checkIfSubmittable = debounce((allInputs) => {
const nymIsFilled = allInputs.nymInput.value !== '';
let atLeastOneContactIsFilled = false;
2025-02-20 16:33:46 +01:00
for (const contactInput of allInputs.contactInputs) {
if (contactInput.value !== '') {
atLeastOneContactIsFilled = true;
}
}
const buttonShouldBeDisabled = !(nymIsFilled && atLeastOneContactIsFilled);
submitProfileButton.disabled = buttonShouldBeDisabled;
2025-02-20 23:13:33 +01:00
}, 500);
2025-02-20 20:01:50 +01:00
async function createProfile(allInputs) {
const contactDetails = [];
for (const someInput of allInputs.contactInputs) {
contactDetails.push({
type: someInput.getAttribute('data-type'),
value: someInput.value,
});
}
const encryptedContactDetails = await window.nostr.nip04.encrypt(
await window.nostr.getPublicKey(),
JSON.stringify(contactDetails)
);
await fetch('/api/set-contact-details', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ encryptedContactDetails }),
});
const nym = allInputs.nymInput.value;
await fetch('/api/set-nym', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ nym }),
});
2025-02-20 23:13:33 +01:00
2025-02-28 12:42:13 +01:00
createProfileConfirmation.classList.add('revealed');
2025-02-20 23:13:33 +01:00
setTimeout(() => {
window.location.href = '/home';
2025-02-28 12:42:13 +01:00
}, 5000);
2025-02-20 20:01:50 +01:00
}
function onLoadErrands(allInputs, submitProfileButton) {
allInputs.nymInput.addEventListener('input', validateNymInput);
for (const someInput of allInputs.allInputs) {
someInput.addEventListener('input', () => {
checkIfSubmittable(allInputs);
});
}
checkIfSubmittable(allInputs);
2025-02-20 20:01:50 +01:00
submitProfileButton.addEventListener('click', () => {
createProfile(allInputs);
});
2025-02-20 16:33:46 +01:00
}
const nymInput = document.getElementById('nym-input');
const nymInputValidationWarning = document.getElementById(
'nym-input-validation-warning'
);
const phoneInput = document.getElementById('phone-input');
const whatsappInput = document.getElementById('whatsapp-input');
const telegramInput = document.getElementById('telegram-input');
const emailInput = document.getElementById('email-input');
const nostrInput = document.getElementById('nostr-input');
const signalInput = document.getElementById('signal-input');
const submitProfileButton = document.getElementById('submit-profile-button');
const allInputs = {
nymInput: nymInput,
contactInputs: [
phoneInput,
whatsappInput,
telegramInput,
emailInput,
nostrInput,
signalInput,
],
allInputs: [
nymInput,
phoneInput,
whatsappInput,
telegramInput,
emailInput,
nostrInput,
signalInput,
],
2025-02-20 16:33:46 +01:00
};
2025-02-20 20:01:50 +01:00
onLoadErrands(allInputs, submitProfileButton);