From a24d459b2579a9e8c1a4a5e83d1a92cd788f9c71 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 27 Nov 2018 14:09:38 +0100 Subject: [PATCH] fix(wallet): add all new invoices to invoice list Add invoices that are created/paid in the background to the activity list. Previously, only invoices created without Zap would be visible within Zap without doing a manual refresh on the activity list. Fix #912 --- app/reducers/invoice.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index 33ecc95f..4de9394e 100644 --- a/app/reducers/invoice.js +++ b/app/reducers/invoice.js @@ -95,8 +95,8 @@ export const fetchInvoices = () => dispatch => { // Receive IPC event for invoices export const receiveInvoices = (event, { invoices }) => dispatch => { - dispatch({ type: RECEIVE_INVOICES, invoices }) invoices.forEach(decorateInvoice) + dispatch({ type: RECEIVE_INVOICES, invoices }) } // Send IPC event for creating an invoice @@ -137,13 +137,13 @@ export const invoiceFailed = (event, { error }) => dispatch => { // Listen for invoice updates pushed from backend from subscribeToInvoices export const invoiceUpdate = (event, { invoice }) => dispatch => { + decorateInvoice(invoice) + dispatch({ type: UPDATE_INVOICE, invoice }) // Fetch new balance dispatch(fetchBalance()) - decorateInvoice(invoice) - if (invoice.settled) { // HTML 5 desktop notification for the invoice update const notifTitle = "You've been Zapped" @@ -171,22 +171,27 @@ const ACTION_HANDLERS = { [INVOICE_SUCCESSFUL]: (state, { invoice }) => ({ ...state, invoiceLoading: false, - invoices: [invoice, ...state.invoices] + invoices: [...state.invoices, invoice] }), [INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null }), [UPDATE_INVOICE]: (state, action) => { + let isNew = true const updatedInvoices = state.invoices.map(invoice => { - if (invoice.r_hash.toString('hex') !== action.invoice.r_hash.toString('hex')) { - return invoice - } - - return { - ...invoice, - ...action.invoice + if (invoice.r_hash.toString('hex') === action.invoice.r_hash.toString('hex')) { + isNew = false + return { + ...invoice, + ...action.invoice + } } + return invoice }) + if (isNew) { + updatedInvoices.push(action.invoice) + } + return { ...state, invoices: updatedInvoices } } }