fix: update product list when product deleted

This commit is contained in:
Vlad Stan 2023-07-19 14:07:12 +03:00
parent ee3b161b47
commit f41a502d3b
2 changed files with 34 additions and 31 deletions

View file

@ -22,7 +22,6 @@ async function customerStallList(path) {
}, },
created() { created() {
console.log('### stalls', this.stalls)
} }
}) })
} }

View file

@ -11,7 +11,12 @@ const market = async () => {
'wss://nostr.walletofsatoshi.com' 'wss://nostr.walletofsatoshi.com'
] ]
const eventToObj = event => { const eventToObj = event => {
event.content = JSON.parse(event.content) || null try {
event.content = JSON.parse(event.content) || null
} catch {
event.content = null
}
return { return {
...event, ...event,
@ -358,13 +363,14 @@ const market = async () => {
}, },
async updateUiConfig(updateData) { async updateUiConfig(data) {
const { name, about, ui } = updateData const { name, about, ui } = data
this.config = { ...this.config, opts: { ...this.config.opts, name, about, ui } } this.config = { ...this.config, opts: { ...this.config.opts, name, about, ui } }
this.applyUiConfigs(this.config) this.applyUiConfigs(this.config)
}, },
async updateData(events) { async updateData(events) {
console.log('### updateData', events)
if (events.length < 1) { if (events.length < 1) {
this.$q.notify({ this.$q.notify({
message: 'No matches were found!' message: 'No matches were found!'
@ -373,6 +379,13 @@ const market = async () => {
} }
let products = new Map() let products = new Map()
let stalls = new Map() let stalls = new Map()
const deleteEventsIds = events
.filter(e => e.kind === 5)
.map(e => (e.tags || []).filter(t => t[0] === 'e'))
.flat()
.map(t => t[1])
.filter(t => !!t)
this.stalls.forEach(s => stalls.set(s.id, s)) this.stalls.forEach(s => stalls.set(s.id, s))
this.products.forEach(p => products.set(p.id, p)) this.products.forEach(p => products.set(p.id, p))
@ -385,9 +398,11 @@ const market = async () => {
} }
this.merchants.filter(m => m.publicKey === e.pubkey).forEach(m => m.profile = e.content) this.merchants.filter(m => m.publicKey === e.pubkey).forEach(m => m.profile = e.content)
return return
} else if (e.kind == 5) {
console.log('### delete event', e)
} else if (e.kind == 30018) { } else if (e.kind == 30018) {
//it's a product `d` is the prod. id //it's a product `d` is the prod. id
products.set(e.d, { ...e.content, pubkey: e.pubkey, id: e.d, categories: e.t }) products.set(e.d, { ...e.content, pubkey: e.pubkey, id: e.d, categories: e.t, eventId: e.id })
} else if (e.kind == 30017) { } else if (e.kind == 30017) {
// it's a stall `d` is the stall id // it's a stall `d` is the stall id
stalls.set(e.d, { ...e.content, pubkey: e.pubkey, id: e.d, pubkey: e.pubkey }) stalls.set(e.d, { ...e.content, pubkey: e.pubkey, id: e.d, pubkey: e.pubkey })
@ -398,7 +413,7 @@ const market = async () => {
this.products = Array.from(products.values()) this.products = Array.from(products.values())
.map(obj => { .map(obj => {
let stall = this.stalls.find(s => s.id == obj.stall_id) const stall = this.stalls.find(s => s.id == obj.stall_id)
if (!stall) return if (!stall) return
obj.stallName = stall.name obj.stallName = stall.name
obj.images = obj.images || [obj.image] obj.images = obj.images || [obj.image]
@ -410,44 +425,33 @@ const market = async () => {
} }
return obj return obj
}) })
.filter(f => f) .filter(p => p && (deleteEventsIds.indexOf(p.eventId)) === -1)
console.log('### this.products', this.products)
}, },
async initNostr() { async initNostr() {
this.isLoading = true this.isLoading = true
const pool = new NostrTools.SimplePool() this.pool = new NostrTools.SimplePool()
let relays = Array.from(this.relays) const relays = Array.from(this.relays)
const authors = this.merchants.map(m => m.publicKey) const authors = this.merchants.map(m => m.publicKey)
await pool const events = await this.pool.list(relays, [{ kinds: [0, 30017, 30018], authors }])
.list(relays, [ if (!events || events.length == 0) return
{ await this.updateData(events)
kinds: [0, 30017, 30018], // for production kind is 30017
authors
}
])
.then(async events => {
if (!events || events.length == 0) return
await this.updateData(events)
})
const lastEvent = events.sort((a, b) => b.created_at - a.created_at)[0]
this.pool = pool console.log('### since', lastEvent.created_at) // 1685456534, 1689758074
this.poolSubscribe() this.poolSubscribe(lastEvent.created_at)
this.isLoading = false this.isLoading = false
}, },
async poolSubscribe() { async poolSubscribe(since) {
const authors = this.merchants.map(m => m.publicKey) const authors = this.merchants.map(m => m.publicKey)
this.poolSub = this.pool.sub(Array.from(this.relays), [ this.poolSub = this.pool.sub(Array.from(this.relays), [{ kinds: [0, 5, 30017, 30018], authors, since }])
{
kinds: [0, 30017, 30018],
authors,
since: Date.now() / 1000
}
])
this.poolSub.on( this.poolSub.on(
'event', 'event',
event => { event => {
console.log('####### new event', event)
this.updateData([event]) this.updateData([event])
}, },
{ id: 'masterSub' } //pass ID to cancel previous sub { id: 'masterSub' } //pass ID to cancel previous sub