From 683b3a51f701a3046ec849398442584643d4aab4 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Sun, 1 Feb 2026 14:19:43 +0100 Subject: [PATCH] improve views in listing --- js/components/pages/page-listing.js | 7 +++++-- js/services/directus.js | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/js/components/pages/page-listing.js b/js/components/pages/page-listing.js index 14e168c..276e096 100644 --- a/js/components/pages/page-listing.js +++ b/js/components/pages/page-listing.js @@ -30,8 +30,11 @@ class PageListing extends HTMLElement { this.listing = await directus.getListing(this.listingId) this.loadFavoriteState() - // Increment view counter (async, don't wait) - directus.incrementListingViews(this.listingId) + // Increment view counter and update local state + const newViews = await directus.incrementListingViews(this.listingId) + if (newViews !== null) { + this.listing.views = newViews + } // Load other listings from same seller if (this.listing?.user_created) { diff --git a/js/services/directus.js b/js/services/directus.js index 9306f10..507eda0 100644 --- a/js/services/directus.js +++ b/js/services/directus.js @@ -335,7 +335,7 @@ class DirectusService { // Check if user already viewed this listing (session-based) const viewedKey = `dgray_viewed_${id}` if (sessionStorage.getItem(viewedKey)) { - return false + return null } try { @@ -344,18 +344,19 @@ class DirectusService { fields: ['views'] }) const currentViews = listing.data?.views || 0 + const newViews = currentViews + 1 // Increment views await this.patch(`/items/listings/${id}`, { - views: currentViews + 1 + views: newViews }) // Mark as viewed for this session sessionStorage.setItem(viewedKey, 'true') - return true + return newViews } catch (error) { console.error('Failed to increment views:', error) - return false + return null } }