From 6fdf4b63b9d44f2fc74bf7258fe55252062f19db Mon Sep 17 00:00:00 2001 From: Meghdad <61308304+MeghdadFadaee@users.noreply.github.com> Date: Thu, 23 Jul 2026 00:06:59 +0330 Subject: [PATCH] feat: add live service health status polling --- resources/js/app.js | 59 +++++++++++++++++++++++++++++++ resources/views/welcome.blade.php | 8 ++--- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/resources/js/app.js b/resources/js/app.js index ee0de33..b146083 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1,6 +1,10 @@ const loanForm = document.querySelector('#loan-form'); if (loanForm) { + const serviceStatus = document.querySelector('#service-status'); + const serviceStatusPulse = document.querySelector('#service-status-pulse'); + const serviceStatusDot = document.querySelector('#service-status-dot'); + const serviceStatusLabel = document.querySelector('#service-status-label'); const lookupForm = document.querySelector('#lookup-form'); const resultPanel = document.querySelector('#result-panel'); const processButton = document.querySelector('#process-loan'); @@ -8,7 +12,10 @@ if (loanForm) { const formMessage = document.querySelector('#form-message'); const creditScore = document.querySelector('#credit-score'); const creditScoreOutput = document.querySelector('#credit-score-output'); + const healthCheckInterval = 3_000; let activeLoanId = null; + let healthCheckTimer = null; + let isCheckingService = false; const terminalStatuses = ['APPROVED', 'REJECTED', 'MANUAL_REVIEW']; const statusLabels = { @@ -32,6 +39,58 @@ if (loanForm) { stopped: '', }; + const checkServiceHealth = async () => { + try { + const response = await fetch(serviceStatus.dataset.healthUrl, { + headers: {Accept: 'application/json'}, + cache: 'no-store', + signal: AbortSignal.timeout(5_000), + }); + const payload = await response.json(); + + if (!response.ok || payload.status !== 'UP') { + throw new Error('Service unavailable'); + } + + serviceStatusLabel.textContent = 'Service ready'; + serviceStatusPulse.className = 'absolute inline-flex size-full animate-ping rounded-full bg-emerald-400 opacity-60'; + serviceStatusDot.className = 'relative inline-flex size-2 rounded-full bg-emerald-500'; + } catch { + serviceStatusLabel.textContent = 'Service unavailable'; + serviceStatusPulse.className = 'hidden'; + serviceStatusDot.className = 'relative inline-flex size-2 rounded-full bg-rose-500'; + } + }; + + const scheduleServiceHealthCheck = () => { + window.clearTimeout(healthCheckTimer); + + if (!document.hidden) { + healthCheckTimer = window.setTimeout(pollServiceHealth, healthCheckInterval); + } + }; + + const pollServiceHealth = async () => { + if (isCheckingService) { + return; + } + + isCheckingService = true; + await checkServiceHealth(); + isCheckingService = false; + scheduleServiceHealthCheck(); + }; + + document.addEventListener('visibilitychange', () => { + window.clearTimeout(healthCheckTimer); + + if (!document.hidden) { + pollServiceHealth(); + } + }); + + pollServiceHealth(); + creditScore.addEventListener('input', () => { creditScoreOutput.value = creditScore.value; creditScoreOutput.textContent = creditScore.value; diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 375951f..4c64f32 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -31,12 +31,12 @@ -
+
- - + + - API v1 ready + Checking service