diff --git a/public/js/components/navbar.js b/public/js/components/navbar.js index b00e700f..5fd9b4c4 100644 --- a/public/js/components/navbar.js +++ b/public/js/components/navbar.js @@ -95,6 +95,9 @@ class TractatusNavbar { ℹ️ About + + 🤝 Support (Koha) + @@ -102,10 +105,11 @@ class TractatusNavbar { `; - // Insert navbar at the beginning of body (or replace existing nav) - const existingNav = document.querySelector('nav'); - if (existingNav) { - existingNav.outerHTML = navHTML; + // Always insert navbar at the very beginning of body + // Check if there's already a tractatus navbar (to avoid duplicates) + const existingNavbar = document.querySelector('nav.bg-white.border-b.border-gray-200.sticky'); + if (existingNavbar) { + existingNavbar.outerHTML = navHTML; } else { document.body.insertAdjacentHTML('afterbegin', navHTML); } diff --git a/public/js/koha-donation.js b/public/js/koha-donation.js index 70ad50d3..b04fdb3a 100644 --- a/public/js/koha-donation.js +++ b/public/js/koha-donation.js @@ -287,3 +287,129 @@ async function handleFormSubmit(e) { submitBtn.textContent = 'Proceed to Secure Payment'; } } + +/** + * Initialize manage subscription functionality + */ +document.addEventListener('DOMContentLoaded', function() { + const manageBtn = document.getElementById('manage-subscription-btn'); + const emailInput = document.getElementById('manage-email'); + + if (manageBtn && emailInput) { + manageBtn.addEventListener('click', handleManageSubscription); + emailInput.addEventListener('keypress', function(e) { + if (e.key === 'Enter') { + e.preventDefault(); + handleManageSubscription(); + } + }); + } +}); + +/** + * Handle manage subscription button click + */ +async function handleManageSubscription() { + const emailInput = document.getElementById('manage-email'); + const manageBtn = document.getElementById('manage-subscription-btn'); + const errorDiv = document.getElementById('manage-error'); + const loadingDiv = document.getElementById('manage-loading'); + + const email = emailInput.value.trim(); + + // Validate email + if (!email) { + showManageError('Please enter your email address.'); + emailInput.focus(); + return; + } + + if (!isValidEmail(email)) { + showManageError('Please enter a valid email address.'); + emailInput.focus(); + return; + } + + // Hide error, show loading + hideManageError(); + showManageLoading(); + manageBtn.disabled = true; + + try { + const response = await fetch('/api/koha/portal', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ email }) + }); + + const data = await response.json(); + + if (data.success && data.data.url) { + // Redirect to Stripe Customer Portal + window.location.href = data.data.url; + } else if (response.status === 404) { + showManageError('No subscription found for this email address. Please check your email and try again.'); + manageBtn.disabled = false; + hideManageLoading(); + } else { + throw new Error(data.error || 'Failed to access subscription portal'); + } + + } catch (error) { + console.error('Manage subscription error:', error); + showManageError('An error occurred. Please try again or contact support@agenticgovernance.digital'); + manageBtn.disabled = false; + hideManageLoading(); + } +} + +/** + * Validate email format + */ +function isValidEmail(email) { + const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return re.test(email); +} + +/** + * Show error message + */ +function showManageError(message) { + const errorDiv = document.getElementById('manage-error'); + if (errorDiv) { + errorDiv.textContent = message; + errorDiv.classList.remove('hidden'); + } +} + +/** + * Hide error message + */ +function hideManageError() { + const errorDiv = document.getElementById('manage-error'); + if (errorDiv) { + errorDiv.classList.add('hidden'); + } +} + +/** + * Show loading indicator + */ +function showManageLoading() { + const loadingDiv = document.getElementById('manage-loading'); + if (loadingDiv) { + loadingDiv.classList.remove('hidden'); + } +} + +/** + * Hide loading indicator + */ +function hideManageLoading() { + const loadingDiv = document.getElementById('manage-loading'); + if (loadingDiv) { + loadingDiv.classList.add('hidden'); + } +} diff --git a/public/koha.html b/public/koha.html index d7624756..bd0c6176 100644 --- a/public/koha.html +++ b/public/koha.html @@ -3,8 +3,8 @@
-