feat: dinamically refresh orders

This commit is contained in:
Vlad Stan 2023-03-29 18:00:01 +03:00
parent 4974192b21
commit 8d1998c33b
4 changed files with 40 additions and 12 deletions

View file

@ -141,6 +141,18 @@ async function orderList(path) {
LNbits.utils.notifyApiError(error) 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 () { updateOrderShipped: async function () {
this.selectedOrder.shipped = !this.selectedOrder.shipped this.selectedOrder.shipped = !this.selectedOrder.shipped
try { try {
@ -163,6 +175,16 @@ async function orderList(path) {
} }
this.showShipDialog = false 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) { showShipOrderDialog: function (order) {
this.selectedOrder = order this.selectedOrder = order
this.shippingMessage = order.shipped this.shippingMessage = order.shipped

View file

@ -107,21 +107,26 @@ const merchant = async () => {
filterOrdersForCustomer: function (customerPubkey) { filterOrdersForCustomer: function (customerPubkey) {
this.orderPubkey = customerPubkey this.orderPubkey = customerPubkey
}, },
waitForNotifications: function () { waitForNotifications: async function () {
try { try {
const scheme = location.protocol === 'http:' ? 'ws' : 'wss' const scheme = location.protocol === 'http:' ? 'ws' : 'wss'
const port = location.port ? `:${location.port}` : '' const port = location.port ? `:${location.port}` : ''
const wsUrl = `${scheme}://${document.domain}${port}/api/v1/ws/${this.merchant.id}` const wsUrl = `${scheme}://${document.domain}${port}/api/v1/ws/${this.merchant.id}`
const wsConnection = new WebSocket(wsUrl) const wsConnection = new WebSocket(wsUrl)
console.log('### waiting for events') console.log('### waiting for events')
wsConnection.onmessage = e => { wsConnection.onmessage = async e => {
console.log('### e', e) console.log('### e', e)
this.$q.notify({ const data = JSON.parse(e.data)
timeout: 5000, if (data.type === 'new-order') {
type: 'positive', this.$q.notify({
message: 'New Update', timeout: 5000,
caption: `something here` 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) { } catch (error) {
this.$q.notify({ this.$q.notify({

View file

@ -64,6 +64,7 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<order-list <order-list
ref="orderListRef"
:adminkey="g.user.wallets[0].adminkey" :adminkey="g.user.wallets[0].adminkey"
:inkey="g.user.wallets[0].inkey" :inkey="g.user.wallets[0].inkey"
:customer-pubkey-filter="orderPubkey" :customer-pubkey-filter="orderPubkey"

View file

@ -647,10 +647,10 @@ async def api_delete_product(
######################################## ORDERS ######################################## ######################################## ORDERS ########################################
nostrmarket_ext.get("/api/v1/order/{order_id}") @nostrmarket_ext.get("/api/v1/order/{order_id}")
async def api_get_order(
order_id: str, wallet: WalletTypeInfo = Depends(require_invoice_key)
async def api_get_order(order_id: str, wallet: WalletTypeInfo = Depends(get_key_type)): ):
try: try:
merchant = await get_merchant_for_user(wallet.wallet.user) merchant = await get_merchant_for_user(wallet.wallet.user)
assert merchant, "Merchant cannot be found" assert merchant, "Merchant cannot be found"