merge bugs fixes for anon zapping

This commit is contained in:
keyan 2023-08-12 09:21:40 -05:00
commit 9727fb4838
4 changed files with 57 additions and 7 deletions

View File

@ -82,8 +82,8 @@ export default function FeeButton ({ parentId, hasImgLink, baseFee, ChildButton,
const cost = baseFee * (hasImgLink ? 10 : 1) * Math.pow(10, repetition) + Number(boost)
useEffect(() => {
formik.setFieldValue('cost', cost)
}, [cost])
formik?.setFieldValue('cost', cost)
}, [formik?.getFieldProps('cost').value, cost])
const show = alwaysShow || !formik?.isSubmitting
return (
@ -142,8 +142,8 @@ export function EditFeeButton ({ paidSats, hadImgLink, hasImgLink, ChildButton,
const cost = (addImgLink ? paidSats * 9 : 0) + Number(boost)
useEffect(() => {
formik.setFieldValue('cost', cost)
}, [cost])
formik?.setFieldValue('cost', cost)
}, [formik?.getFieldProps('cost').value, cost])
const show = alwaysShow || !formik?.isSubmitting
return (

View File

@ -12,6 +12,7 @@ import { useMe } from './me'
import { useShowModal } from './modal'
import { sleep } from '../lib/time'
import FundError, { isInsufficientFundsError } from './fund-error'
import { usePaymentTokens } from './payment-tokens'
export function Invoice ({ invoice, onConfirmation, successVerb }) {
let variant = 'default'
@ -161,15 +162,20 @@ export const useInvoiceable = (fn, options = defaultOptions) => {
}`)
const showModal = useShowModal()
const [fnArgs, setFnArgs] = useState()
const { addPaymentToken, removePaymentToken } = usePaymentTokens()
// fix for bug where `showModal` runs the code for two modals and thus executes `onConfirmation` twice
let errorCount = 0
const onConfirmation = useCallback(
(onClose, hmac) => {
return async ({ id, satsReceived, hash }) => {
addPaymentToken(hash, hmac, satsReceived)
await sleep(500)
const repeat = () =>
fn(satsReceived, ...fnArgs, hash, hmac)
.then(() => {
removePaymentToken(hash, hmac)
})
.then(onClose)
.catch((error) => {
console.error(error)

View File

@ -0,0 +1,40 @@
import React, { useCallback, useContext, useEffect, useState } from 'react'
export const PaymentTokenContext = React.createContext()
const fetchTokensFromLocalStorage = () => {
const tokens = JSON.parse(window.localStorage.getItem('payment-tokens') || '[]')
return tokens
}
export function PaymentTokenProvider ({ children }) {
const [tokens, setTokens] = useState([])
useEffect(() => {
setTokens(fetchTokensFromLocalStorage())
}, [])
const addPaymentToken = useCallback((hash, hmac, amount) => {
const token = hash + '|' + hmac
const newTokens = [...tokens, { token, amount }]
window.localStorage.setItem('payment-tokens', JSON.stringify(newTokens))
setTokens(newTokens)
}, [tokens])
const removePaymentToken = useCallback((hash, hmac) => {
const token = hash + '|' + hmac
const newTokens = tokens.filter(({ token: t }) => t !== token)
window.localStorage.setItem('payment-tokens', JSON.stringify(newTokens))
setTokens(newTokens)
}, [tokens])
return (
<PaymentTokenContext.Provider value={{ tokens, addPaymentToken, removePaymentToken }}>
{children}
</PaymentTokenContext.Provider>
)
}
export function usePaymentTokens () {
return useContext(PaymentTokenContext)
}

View File

@ -14,6 +14,7 @@ import { ServiceWorkerProvider } from '../components/serviceworker'
import { SSR } from '../lib/constants'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { PaymentTokenProvider } from '../components/payment-tokens'
NProgress.configure({
showSpinner: false
@ -89,11 +90,14 @@ function MyApp ({ Component, pageProps: { ...props } }) {
<ServiceWorkerProvider>
<PriceProvider price={price}>
<LightningProvider>
<ShowModalProvider>
<Component ssrData={ssrData} {...otherProps} />
</ShowModalProvider>
<PaymentTokenProvider>
<ShowModalProvider>
<Component ssrData={ssrData} {...otherProps} />
</ShowModalProvider>
</PaymentTokenProvider>
</LightningProvider>
</PriceProvider>
</ServiceWorkerProvider>
</MeProvider>
</ApolloProvider>