Enhance product handling in MerchantStore and useMarket

- Enriched products with stall names in MerchantStore, ensuring each product displays the associated stall name or 'Unknown Stall' if not available.
- Updated product addition logic in useMarket to include stall names, improving clarity and consistency across the application.
- Enhanced error handling for product loading to maintain robust functionality.

These changes improve the user experience by providing clearer product information in the market interface.
This commit is contained in:
padreug 2025-09-25 22:44:38 +02:00
parent 2e12315a35
commit f2080abce5
2 changed files with 30 additions and 8 deletions

View file

@ -516,7 +516,17 @@ const loadStallProducts = async () => {
inkey, inkey,
activeStall.value.id! activeStall.value.id!
) )
stallProducts.value = products || [] // Enrich products with stall name
const enrichedProducts = (products || []).map(product => ({
...product,
stallName: activeStall.value?.name || 'Unknown Stall'
}))
stallProducts.value = enrichedProducts
// Also add them to the market store so they appear in the main market
enrichedProducts.forEach(product => {
marketStore.addProduct(product)
})
} catch (error) { } catch (error) {
console.error('Failed to load products:', error) console.error('Failed to load products:', error)
stallProducts.value = [] stallProducts.value = []

View file

@ -286,13 +286,19 @@ export function useMarket() {
productGroups.forEach((productEvents, productId) => { productGroups.forEach((productEvents, productId) => {
// Sort by created_at and take the most recent // Sort by created_at and take the most recent
const latestEvent = productEvents.sort((a: any, b: any) => b.created_at - a.created_at)[0] const latestEvent = productEvents.sort((a: any, b: any) => b.created_at - a.created_at)[0]
try { try {
const productData = JSON.parse(latestEvent.content) const productData = JSON.parse(latestEvent.content)
const stallId = productData.stall_id || 'unknown'
// Look up the stall name from the stalls array
const stall = marketStore.stalls.find(s => s.id === stallId)
const stallName = stall?.name || 'Unknown Stall'
const product = { const product = {
id: productId, id: productId,
stall_id: productData.stall_id || 'unknown', stall_id: stallId,
stallName: productData.stallName || 'Unknown Stall', stallName: stallName,
name: productData.name || 'Unnamed Product', name: productData.name || 'Unnamed Product',
description: productData.description || '', description: productData.description || '',
price: productData.price || 0, price: productData.price || 0,
@ -303,7 +309,7 @@ export function useMarket() {
createdAt: latestEvent.created_at, createdAt: latestEvent.created_at,
updatedAt: latestEvent.created_at updatedAt: latestEvent.created_at
} }
marketStore.addProduct(product) marketStore.addProduct(product)
} catch (err) { } catch (err) {
// Silently handle parse errors // Silently handle parse errors
@ -468,10 +474,16 @@ export function useMarket() {
const productId = event.tags.find((tag: any) => tag[0] === 'd')?.[1] const productId = event.tags.find((tag: any) => tag[0] === 'd')?.[1]
if (productId) { if (productId) {
const productData = JSON.parse(event.content) const productData = JSON.parse(event.content)
const stallId = productData.stall_id || 'unknown'
// Look up the stall name from the stalls array
const stall = marketStore.stalls.find(s => s.id === stallId)
const stallName = stall?.name || 'Unknown Stall'
const product = { const product = {
id: productId, id: productId,
stall_id: productData.stall_id || 'unknown', stall_id: stallId,
stallName: productData.stallName || 'Unknown Stall', stallName: stallName,
pubkey: event.pubkey, pubkey: event.pubkey,
name: productData.name || 'Unnamed Product', name: productData.name || 'Unnamed Product',
description: productData.description || '', description: productData.description || '',
@ -483,7 +495,7 @@ export function useMarket() {
createdAt: event.created_at, createdAt: event.created_at,
updatedAt: event.created_at updatedAt: event.created_at
} }
marketStore.addProduct(product) marketStore.addProduct(product)
} }
} catch (err) { } catch (err) {