Spaces:
Running
Running
Update index.html
Browse files- index.html +71 -0
index.html
CHANGED
@@ -401,5 +401,76 @@
|
|
401 |
</form>
|
402 |
</div>
|
403 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
404 |
</body>
|
405 |
</html>
|
|
|
401 |
</form>
|
402 |
</div>
|
403 |
</div>
|
404 |
+
<script>
|
405 |
+
// JavaScript for Capital Account Functionality
|
406 |
+
|
407 |
+
// ▼▼▼▼▼ CRITICAL STEP: CONFIGURE YOUR WORKER URL ▼▼▼▼▼
|
408 |
+
const WORKER_URL="YOUR_WORKER_URL_HERE";
|
409 |
+
// ▲▲▲▲▲ CRITICAL STEP: CONFIGURE YOUR WORKER URL ▲▲▲▲▲
|
410 |
+
|
411 |
+
const entryModal = document.getElementById('entryModal');
|
412 |
+
const addEntryBtn = document.getElementById('addEntryBtn');
|
413 |
+
const cancelBtn = document.getElementById('cancelBtn');
|
414 |
+
const ledgerForm = document.getElementById('ledgerForm');
|
415 |
+
|
416 |
+
// NOTE: This assumes the user is already logged in and a JWT is in localStorage.
|
417 |
+
// The floating button will only appear if a session token exists.
|
418 |
+
let sessionToken = localStorage.getItem('accessToken');
|
419 |
+
if (sessionToken) {
|
420 |
+
addEntryBtn.style.display = 'flex';
|
421 |
+
}
|
422 |
+
|
423 |
+
addEntryBtn.addEventListener('click', () => {
|
424 |
+
entryModal.style.display = 'flex';
|
425 |
+
});
|
426 |
+
|
427 |
+
cancelBtn.addEventListener('click', () => {
|
428 |
+
entryModal.style.display = 'none';
|
429 |
+
ledgerForm.reset();
|
430 |
+
});
|
431 |
+
|
432 |
+
ledgerForm.addEventListener('submit', async (e) => {
|
433 |
+
e.preventDefault();
|
434 |
+
const entryData = {
|
435 |
+
date: document.getElementById('date').value,
|
436 |
+
description: document.getElementById('description').value,
|
437 |
+
ref: document.getElementById('ref').value,
|
438 |
+
debit: document.getElementById('debit').value || 0,
|
439 |
+
credit: document.getElementById('credit').value || 0,
|
440 |
+
};
|
441 |
+
|
442 |
+
// Re-check for session token before submitting
|
443 |
+
sessionToken = localStorage.getItem('accessToken');
|
444 |
+
if (!sessionToken) {
|
445 |
+
alert('Your session has expired. Please log in again.');
|
446 |
+
return;
|
447 |
+
}
|
448 |
+
|
449 |
+
try {
|
450 |
+
const response = await fetch(`${WORKER_URL}/api/ledger`, {
|
451 |
+
method: 'POST',
|
452 |
+
headers: {
|
453 |
+
'Content-Type': 'application/json',
|
454 |
+
'Authorization': `Bearer ${sessionToken}`
|
455 |
+
},
|
456 |
+
body: JSON.stringify(entryData),
|
457 |
+
});
|
458 |
+
|
459 |
+
if (!response.ok) {
|
460 |
+
// Try to get error message from backend
|
461 |
+
const errorResult = await response.json();
|
462 |
+
throw new Error(errorResult.error || 'Authorization failed. Please log in again.');
|
463 |
+
}
|
464 |
+
|
465 |
+
const result = await response.json();
|
466 |
+
alert(`${result.message}\nNew Balance: ${result.newBalance.toFixed(2)}`);
|
467 |
+
entryModal.style.display = 'none';
|
468 |
+
ledgerForm.reset();
|
469 |
+
|
470 |
+
} catch (error) {
|
471 |
+
alert(error.message);
|
472 |
+
}
|
473 |
+
});
|
474 |
+
</script>
|
475 |
</body>
|
476 |
</html>
|