docfast/public/app.js

104 lines
3.5 KiB
JavaScript

function openSignup() {
document.getElementById('signupModal').classList.add('active');
document.getElementById('signupForm').style.display = 'block';
document.getElementById('keyResult').style.display = 'none';
document.getElementById('signupError').style.display = 'none';
document.getElementById('signupBtn').textContent = 'Get API Key';
document.getElementById('signupBtn').disabled = false;
}
function closeSignup() {
document.getElementById('signupModal').classList.remove('active');
}
// Close on overlay click
document.getElementById('signupModal').addEventListener('click', function(e) {
if (e.target === this) closeSignup();
});
async function submitSignup() {
var errEl = document.getElementById('signupError');
var btn = document.getElementById('signupBtn');
btn.textContent = 'Creating...';
btn.disabled = true;
errEl.style.display = 'none';
try {
var res = await fetch('/v1/signup/free', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({})
});
if (res.status === 429) {
errEl.textContent = 'Too many requests. Please try again in a few minutes.';
errEl.style.display = 'block';
btn.textContent = 'Get API Key';
btn.disabled = false;
return;
}
var data = await res.json();
if (!res.ok) {
errEl.textContent = data.error || 'Something went wrong. Please try again.';
errEl.style.display = 'block';
btn.textContent = 'Get API Key';
btn.disabled = false;
return;
}
// Show key
document.getElementById('signupForm').style.display = 'none';
document.getElementById('keyResult').style.display = 'block';
document.getElementById('apiKeyDisplay').textContent = data.apiKey;
} catch (err) {
errEl.textContent = 'Network error. Please try again.';
errEl.style.display = 'block';
btn.textContent = 'Get API Key';
btn.disabled = false;
}
}
function copyKey() {
var key = document.getElementById('apiKeyDisplay').textContent;
var hint = document.querySelector('.copy-hint');
function showCopied() {
hint.textContent = '✓ Copied!';
setTimeout(function() { hint.textContent = 'Click to copy'; }, 2000);
}
try {
navigator.clipboard.writeText(key).then(showCopied).catch(function() {
var ta = document.createElement('textarea');
ta.value = key; ta.style.position = 'fixed'; ta.style.opacity = '0';
document.body.appendChild(ta); ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
showCopied();
});
} catch(e) {
showCopied();
}
}
async function checkout() {
try {
var res = await fetch('/v1/billing/checkout', { method: 'POST' });
var data = await res.json();
if (data.url) window.location.href = data.url;
else alert('Something went wrong. Please try again.');
} catch (err) {
alert('Something went wrong. Please try again.');
}
}
// BUG-005 fix: attach all click handlers via JS instead of inline onclick
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('btn-signup').addEventListener('click', openSignup);
document.getElementById('btn-signup-2').addEventListener('click', openSignup);
document.getElementById('btn-checkout').addEventListener('click', checkout);
document.getElementById('btn-close-signup').addEventListener('click', closeSignup);
document.getElementById('signupBtn').addEventListener('click', submitSignup);
document.getElementById('apiKeyDisplay').addEventListener('click', copyKey);
});