privateuserh commited on
Commit
dd5c54f
·
verified ·
1 Parent(s): 97a8031

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +72 -43
index.html CHANGED
@@ -402,75 +402,104 @@
402
  </div>
403
  </div>
404
  <script>
405
- // JavaScript for Capital Account Functionality
406
 
407
- // ▼▼▼▼▼ CRITICAL STEP: CONFIGURE YOUR WORKER URL ▼▼▼▼▼
408
  const WORKER_URL="https://partnership-ledger-api.aiagents.workers.dev/";
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
  <div class="modal-overlay" id="loginModal">
476
  <div class="modal-content">
 
402
  </div>
403
  </div>
404
  <script>
405
+ // Complete JavaScript for Login and Capital Account Functionality
406
 
407
+ // ▼▼▼▼▼ CRITICAL: CONFIGURE YOUR WORKER URL ▼▼▼▼▼
408
  const WORKER_URL="https://partnership-ledger-api.aiagents.workers.dev/";
409
+ // ▲▲▲▲▲ CRITICAL: CONFIGURE YOUR WORKER URL ▲▲▲▲▲
410
+
411
+ // Add the Turnstile script to the page dynamically
412
+ const turnstileScript = document.createElement('script');
413
+ turnstileScript.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
414
+ turnstileScript.async = true;
415
+ turnstileScript.defer = true;
416
+ document.head.appendChild(turnstileScript);
417
+
418
+ // Get all the elements we need to control
419
+ const loginModal = document.getElementById('loginModal');
420
  const entryModal = document.getElementById('entryModal');
421
  const addEntryBtn = document.getElementById('addEntryBtn');
422
  const cancelBtn = document.getElementById('cancelBtn');
423
+ const loginForm = document.getElementById('loginForm');
424
  const ledgerForm = document.getElementById('ledgerForm');
425
+ const welcomeDiv = document.getElementById('welcome');
426
+ const partnerNameSpan = document.getElementById('partnerName');
427
+ const logoutBtn = document.getElementById('logoutBtn');
428
  let sessionToken = localStorage.getItem('accessToken');
 
 
 
 
 
 
 
429
 
430
+ // This function checks if a user is already logged in on page load
431
+ const checkLoginState = () => {
432
+ if (sessionToken) {
433
+ loginModal.style.display = 'none';
434
+ addEntryBtn.style.display = 'flex'; // Show the button
435
+ welcomeDiv.style.display = 'block'; // Show the welcome message
436
+ partnerNameSpan.textContent = localStorage.getItem('partnerName');
437
+ } else {
438
+ loginModal.style.display = 'flex'; // Show the login form
439
+ addEntryBtn.style.display = 'none';
440
+ welcomeDiv.style.display = 'none';
441
+ }
442
+ };
443
 
444
+ // Event listener for the LOGIN form
445
+ loginForm.addEventListener('submit', async (e) => {
446
  e.preventDefault();
447
+ const formData = new FormData(loginForm);
448
+ const data = Object.fromEntries(formData.entries());
449
+ try {
450
+ const response = await fetch(`${WORKER_URL}/api/login`, {
451
+ method: 'POST',
452
+ headers: { 'Content-Type': 'application/json' },
453
+ body: JSON.stringify(data)
454
+ });
455
+ if (!response.ok) throw new Error('Login failed. Check your ID and try again.');
456
+
457
+ const result = await response.json();
458
+ localStorage.setItem('accessToken', result.accessToken);
459
+ localStorage.setItem('partnerName', result.partnerName);
460
+ sessionToken = result.accessToken;
461
+ checkLoginState(); // Re-check the state to show the button
462
+
463
+ } catch (error) {
464
+ alert(error.message);
465
+ try { turnstile.reset(); } catch (e) {}
466
  }
467
+ });
468
 
469
+ // Event listener for the LEDGER form (the one in the floating button's modal)
470
+ ledgerForm.addEventListener('submit', async (e) => {
471
+ e.preventDefault();
472
+ const entryData = { date: document.getElementById('date').value, description: document.getElementById('description').value, ref: document.getElementById('ref').value, debit: document.getElementById('debit').value || 0, credit: document.getElementById('credit').value || 0 };
473
  try {
474
  const response = await fetch(`${WORKER_URL}/api/ledger`, {
475
  method: 'POST',
476
+ headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${sessionToken}`},
 
 
 
477
  body: JSON.stringify(entryData),
478
  });
479
+ if (!response.ok) throw new Error('Authorization failed. Please log in again.');
480
 
 
 
 
 
 
 
481
  const result = await response.json();
482
  alert(`${result.message}\nNew Balance: ${result.newBalance.toFixed(2)}`);
483
  entryModal.style.display = 'none';
484
  ledgerForm.reset();
 
485
  } catch (error) {
486
  alert(error.message);
487
  }
488
  });
489
+
490
+ // Event listeners for logout and modal buttons
491
+ logoutBtn.addEventListener('click', () => {
492
+ localStorage.removeItem('accessToken');
493
+ localStorage.removeItem('partnerName');
494
+ sessionToken = null;
495
+ checkLoginState(); // Re-check state to show the login form again
496
+ });
497
+
498
+ addEntryBtn.addEventListener('click', () => entryModal.style.display = 'flex');
499
+ cancelBtn.addEventListener('click', () => { entryModal.style.display = 'none'; ledgerForm.reset(); });
500
+
501
+ // Run the check when the page first loads
502
+ checkLoginState();
503
  </script>
504
  <div class="modal-overlay" id="loginModal">
505
  <div class="modal-content">