function for merging data after retry
This commit is contained in:
parent
105f7b07e5
commit
404cf188b3
@ -98,7 +98,12 @@ export function usePaidMutation (mutation,
|
|||||||
error: e instanceof InvoiceCanceledError && e.actionError ? e : undefined
|
error: e instanceof InvoiceCanceledError && e.actionError ? e : undefined
|
||||||
})
|
})
|
||||||
|
|
||||||
const dataKey = Object.keys(data)[0]
|
const mergeData = obj => ({
|
||||||
|
[Object.keys(data)[0]]: {
|
||||||
|
...data?.[Object.keys(data)[0]],
|
||||||
|
...obj
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// should we wait for the invoice to be paid?
|
// should we wait for the invoice to be paid?
|
||||||
if (response?.paymentMethod === 'OPTIMISTIC' && !forceWaitForPayment) {
|
if (response?.paymentMethod === 'OPTIMISTIC' && !forceWaitForPayment) {
|
||||||
@ -107,23 +112,13 @@ export function usePaidMutation (mutation,
|
|||||||
// don't wait to pay the invoice
|
// don't wait to pay the invoice
|
||||||
waitForPayment(invoice, { persistOnNavigate, waitFor, updateOnFallback }).then((invoice) => {
|
waitForPayment(invoice, { persistOnNavigate, waitFor, updateOnFallback }).then((invoice) => {
|
||||||
// invoice might have been retried during payment
|
// invoice might have been retried during payment
|
||||||
data = {
|
data = mergeData({ invoice })
|
||||||
[dataKey]: {
|
|
||||||
...data[dataKey],
|
|
||||||
invoice
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onPaid?.(client.cache, { data })
|
onPaid?.(client.cache, { data })
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
console.error('usePaidMutation: failed to pay invoice', e)
|
console.error('usePaidMutation: failed to pay invoice', e)
|
||||||
if (e.invoice) {
|
if (e.invoice) {
|
||||||
// update the failed invoice for the Apollo cache update
|
// update the failed invoice for the Apollo cache update
|
||||||
data = {
|
data = mergeData({ invoice: e.invoice })
|
||||||
[dataKey]: {
|
|
||||||
...data[dataKey],
|
|
||||||
invoice: e.invoice
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// onPayError is called after the invoice fails to pay
|
// onPayError is called after the invoice fails to pay
|
||||||
// useful for updating invoiceActionState to FAILED
|
// useful for updating invoiceActionState to FAILED
|
||||||
@ -141,12 +136,7 @@ export function usePaidMutation (mutation,
|
|||||||
const { data: { paidAction } } = await getPaidAction({ variables: { invoiceId: parseInt(invoice.id) } })
|
const { data: { paidAction } } = await getPaidAction({ variables: { invoiceId: parseInt(invoice.id) } })
|
||||||
// create new data object
|
// create new data object
|
||||||
// ( hmac is only returned on invoice creation so we need to add it back to the data )
|
// ( hmac is only returned on invoice creation so we need to add it back to the data )
|
||||||
data = {
|
data = mergeData({ ...paidAction, invoice: { ...paidAction.invoice, hmac: invoice.hmac } })
|
||||||
[dataKey]: {
|
|
||||||
...paidAction,
|
|
||||||
invoice: { ...paidAction.invoice, hmac: invoice.hmac }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// we need to run update functions on mutations now that we have the data
|
// we need to run update functions on mutations now that we have the data
|
||||||
update?.(client.cache, { data })
|
update?.(client.cache, { data })
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user