From 8d1998c33b9a319d5cba8192fe823eabab7fc6b0 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 29 Mar 2023 18:00:01 +0300 Subject: [PATCH] feat: dinamically refresh orders --- static/components/order-list/order-list.js | 22 ++++++++++++++++++++++ static/js/index.js | 21 +++++++++++++-------- templates/nostrmarket/index.html | 1 + views_api.py | 8 ++++---- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/static/components/order-list/order-list.js b/static/components/order-list/order-list.js index ae50fc0..927fe38 100644 --- a/static/components/order-list/order-list.js +++ b/static/components/order-list/order-list.js @@ -141,6 +141,18 @@ async function orderList(path) { LNbits.utils.notifyApiError(error) } }, + getOrder: async function (orderId) { + try { + const {data} = await LNbits.api.request( + 'GET', + `/nostrmarket/api/v1/order/${orderId}`, + this.inkey + ) + return {...data, expanded: false} + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, updateOrderShipped: async function () { this.selectedOrder.shipped = !this.selectedOrder.shipped try { @@ -163,6 +175,16 @@ async function orderList(path) { } this.showShipDialog = false }, + addOrder: async function (data) { + if ( + !this.search.publicKey || + this.search.publicKey === data.customerPubkey + ) { + const order = await this.getOrder(data.orderId) + this.orders.unshift(order) + } + }, + showShipOrderDialog: function (order) { this.selectedOrder = order this.shippingMessage = order.shipped diff --git a/static/js/index.js b/static/js/index.js index 8b8b882..29ea63f 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -107,21 +107,26 @@ const merchant = async () => { filterOrdersForCustomer: function (customerPubkey) { this.orderPubkey = customerPubkey }, - waitForNotifications: function () { + waitForNotifications: async function () { try { const scheme = location.protocol === 'http:' ? 'ws' : 'wss' const port = location.port ? `:${location.port}` : '' const wsUrl = `${scheme}://${document.domain}${port}/api/v1/ws/${this.merchant.id}` const wsConnection = new WebSocket(wsUrl) console.log('### waiting for events') - wsConnection.onmessage = e => { + wsConnection.onmessage = async e => { console.log('### e', e) - this.$q.notify({ - timeout: 5000, - type: 'positive', - message: 'New Update', - caption: `something here` - }) + const data = JSON.parse(e.data) + if (data.type === 'new-order') { + this.$q.notify({ + timeout: 5000, + type: 'positive', + message: 'New Order' + }) + await this.$refs.orderListRef.addOrder(data) + } else if (data.type === 'new-customer') { + } else if (data.type === 'new-direct-message') { + } } } catch (error) { this.$q.notify({ diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index b9b8a82..3cf3f96 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -64,6 +64,7 @@