diff --git a/layouts/partials/highfive.html b/layouts/partials/highfive.html index fa43c50..6a78263 100644 --- a/layouts/partials/highfive.html +++ b/layouts/partials/highfive.html @@ -26,14 +26,29 @@ .highfive-btn { transition: opacity 0.2s ease; } + +.highfive-btn.no-js { + opacity: 0.6; + pointer-events: none; + cursor: not-allowed; +} +{{ $apiUrl := printf "https://api.brandonrozek.com/highfive%s" .RelPermalink }} +{{ $resource := resources.GetRemote $apiUrl }} +{{ $initialCount := 0 }} +{{ if $resource }} + {{ $highfiveData := $resource | transform.Unmarshal }} + {{ if $highfiveData.count }} + {{ $initialCount = $highfiveData.count }} + {{ end }} +{{ end }} +
Give me a high five (it's free): - - - (0) + + ({{ $initialCount }})
@@ -62,7 +77,7 @@ // State let isLoading = false; - let currentCount = 0; + let currentCount = {{ $initialCount }}; /** * Initialize the high five functionality @@ -78,8 +93,9 @@ return; } + // Enable button now that JavaScript is available + elements.button.classList.remove('no-js'); elements.button.addEventListener('click', handleHighFiveClick); - loadHighFiveCount(); } /** @@ -92,6 +108,12 @@ headers: { 'Content-Type': 'application/json' } }); + // Handle 409 Conflict (already high-fived) as a special case + if (response.status === 409) { + const data = await response.json(); + return { ...data, alreadyHighFived: true }; + } + if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } @@ -128,8 +150,8 @@ const newCount = data.count || 0; currentCount = newCount; - // Check if user already gave a high five - if (data.message === 'Already logged') { + // Check if user already gave a high five (HTTP 409 response) + if (data.alreadyHighFived) { updateButtonContent('check', newCount); showMessage('👋 Already high-fived!', 'success'); setButtonState('disabled');