Skip to content

Commit

Permalink
refactor: reset loading state if service is not enabled on Send page
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed May 30, 2022
1 parent 368a58a commit e44d9a5
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/components/Send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,8 @@ export default function Send() {

const isCoinjoinInProgress = useMemo(() => serviceInfo && serviceInfo.coinjoinInProgress, [serviceInfo])
const isMakerRunning = useMemo(() => serviceInfo && serviceInfo.makerRunning, [serviceInfo])
const waitForTakerToFinish = useMemo(() => isCoinjoinInProgress, [isCoinjoinInProgress])
const isOperationDisabled = useMemo(
() => isCoinjoinInProgress || isMakerRunning,
[isCoinjoinInProgress, isMakerRunning]
)

const [alert, setAlert] = useState(null)
const [isLoading, setIsLoading] = useState(!isOperationDisabled)
const [isSending, setIsSending] = useState(false)
const [isCoinjoin, setIsCoinjoin] = useState(IS_COINJOIN_DEFAULT_VAL)
const [minNumCollaborators, setMinNumCollaborators] = useState(MINIMUM_MAKERS_DEFAULT_VAL)
Expand All @@ -198,6 +192,12 @@ export default function Send() {
const [paymentSuccessfulInfoAlert, setPaymentSuccessfulInfoAlert] = useState(null)
const [takerStartedInfoAlert, setTakerStartedInfoAlert] = useState(null)

const isOperationDisabled = useMemo(
() => isCoinjoinInProgress || isMakerRunning || waitForUtxosToBeSpent.length > 0,
[isCoinjoinInProgress, isMakerRunning, waitForUtxosToBeSpent]
)
const [isLoading, setIsLoading] = useState(!isOperationDisabled)

useEffect(() => {
setTakerStartedInfoAlert((current) => (isCoinjoinInProgress ? current : null))
}, [isCoinjoinInProgress])
Expand Down Expand Up @@ -263,7 +263,6 @@ export default function Send() {
useEffect(() => {
if (waitForUtxosToBeSpent.length === 0) return

setIsLoading(true)
const abortCtrl = new AbortController()

// Delaying the poll requests gives the wallet some time to synchronize
Expand All @@ -279,15 +278,13 @@ export default function Send() {
const outputs = data.data.utxos.utxos.map((it) => it.utxo)
const utxosStillPresent = waitForUtxosToBeSpent.filter((it) => outputs.includes(it))
setWaitForUtxosToBeSpent([...utxosStillPresent])
setIsLoading(utxosStillPresent.length > 0)
})
.catch((err) => {
if (abortCtrl.signal.aborted) return

// Stop waiting for wallet synchronization on errors, but inform
// the user that loading the wallet info failed
setWaitForUtxosToBeSpent([])
setIsLoading(false)

const message = err.message || t('send.error_loading_wallet_failed')
setAlert({ variant: 'danger', message })
Expand All @@ -301,8 +298,10 @@ export default function Send() {
}, [waitForUtxosToBeSpent, reloadCurrentWalletInfo, t])

useEffect(() => {
if (waitForTakerToFinish) return
if (waitForUtxosToBeSpent.length > 0) return
if (isOperationDisabled) {
setIsLoading(false)
return
}

const abortCtrl = new AbortController()

Expand Down Expand Up @@ -339,7 +338,7 @@ export default function Send() {
)

return () => abortCtrl.abort()
}, [waitForTakerToFinish, waitForUtxosToBeSpent, wallet, reloadCurrentWalletInfo, reloadServiceInfo, t])
}, [isOperationDisabled, wallet, reloadCurrentWalletInfo, reloadServiceInfo, t])

const sendPayment = async (account, destination, amount_sats) => {
setAlert(null)
Expand Down

0 comments on commit e44d9a5

Please sign in to comment.