improve views in listing
This commit is contained in:
@@ -30,8 +30,11 @@ class PageListing extends HTMLElement {
|
|||||||
this.listing = await directus.getListing(this.listingId)
|
this.listing = await directus.getListing(this.listingId)
|
||||||
this.loadFavoriteState()
|
this.loadFavoriteState()
|
||||||
|
|
||||||
// Increment view counter (async, don't wait)
|
// Increment view counter and update local state
|
||||||
directus.incrementListingViews(this.listingId)
|
const newViews = await directus.incrementListingViews(this.listingId)
|
||||||
|
if (newViews !== null) {
|
||||||
|
this.listing.views = newViews
|
||||||
|
}
|
||||||
|
|
||||||
// Load other listings from same seller
|
// Load other listings from same seller
|
||||||
if (this.listing?.user_created) {
|
if (this.listing?.user_created) {
|
||||||
|
|||||||
@@ -335,7 +335,7 @@ class DirectusService {
|
|||||||
// Check if user already viewed this listing (session-based)
|
// Check if user already viewed this listing (session-based)
|
||||||
const viewedKey = `dgray_viewed_${id}`
|
const viewedKey = `dgray_viewed_${id}`
|
||||||
if (sessionStorage.getItem(viewedKey)) {
|
if (sessionStorage.getItem(viewedKey)) {
|
||||||
return false
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -344,18 +344,19 @@ class DirectusService {
|
|||||||
fields: ['views']
|
fields: ['views']
|
||||||
})
|
})
|
||||||
const currentViews = listing.data?.views || 0
|
const currentViews = listing.data?.views || 0
|
||||||
|
const newViews = currentViews + 1
|
||||||
|
|
||||||
// Increment views
|
// Increment views
|
||||||
await this.patch(`/items/listings/${id}`, {
|
await this.patch(`/items/listings/${id}`, {
|
||||||
views: currentViews + 1
|
views: newViews
|
||||||
})
|
})
|
||||||
|
|
||||||
// Mark as viewed for this session
|
// Mark as viewed for this session
|
||||||
sessionStorage.setItem(viewedKey, 'true')
|
sessionStorage.setItem(viewedKey, 'true')
|
||||||
return true
|
return newViews
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to increment views:', error)
|
console.error('Failed to increment views:', error)
|
||||||
return false
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user