Skip to content

Commit

Permalink
fix(sweep): wait for scheduler start/stop (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni authored Oct 3, 2022
1 parent c79a969 commit 509a15e
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/components/Jam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export default function Jam() {

const [alert, setAlert] = useState(null)
const [isLoading, setIsLoading] = useState(true)
const [collaborativeOperationRunning, setCollaborativeOperationRunning] = useState(false)
const [isWaitingSchedulerStart, setIsWaitingSchedulerStart] = useState(false)
const [isWaitingSchedulerStop, setIsWaitingSchedulerStop] = useState(false)
const collaborativeOperationRunning = useMemo(
() => serviceInfo?.coinjoinInProgress || serviceInfo?.makerRunning || false,
[serviceInfo]
)

const schedulerPreconditionSummary = useMemo(
() => buildCoinjoinRequirementSummary(walletInfo?.data.utxos.utxos || []),
Expand Down Expand Up @@ -119,9 +124,12 @@ export default function Jam() {
}, [reloadServiceInfo, reloadCurrentWalletInfo, t])

useEffect(() => {
setCollaborativeOperationRunning(serviceInfo?.coinjoinInProgress || serviceInfo?.makerRunning || false)
if (!serviceInfo) return

setIsWaitingSchedulerStart((current) => (current && serviceInfo.schedule ? false : current))
setIsWaitingSchedulerStop((current) => (current && !serviceInfo.schedule ? false : current))

if (serviceInfo?.schedule && process.env.NODE_ENV === 'development') {
if (serviceInfo.schedule && process.env.NODE_ENV === 'development') {
console.table(serviceInfo.schedule)
}
}, [serviceInfo])
Expand All @@ -133,6 +141,7 @@ export default function Jam() {

setAlert(null)
setIsLoading(true)
setIsWaitingSchedulerStart(true)

const destinations = [values.dest1, values.dest2, values.dest3]

Expand All @@ -158,11 +167,15 @@ export default function Jam() {
return Api.postSchedulerStart({ signal: abortCtrl.signal, walletName: wallet.name, token: wallet.token }, body)
.then((res) => (res.ok ? true : Api.Helper.throwError(res, t('scheduler.error_starting_schedule_failed'))))
.then((_) => reloadServiceInfo({ signal: abortCtrl.signal }))
.then((_) => setCollaborativeOperationRunning(true))
.catch((err) => {
if (abortCtrl.signal.aborted) return
setAlert({ variant: 'danger', message: err.message })
setIsWaitingSchedulerStart(false)
})
.finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})
.finally(() => setIsLoading(false))
}

const stopSchedule = async () => {
Expand All @@ -172,29 +185,34 @@ export default function Jam() {

setAlert(null)
setIsLoading(true)
setIsWaitingSchedulerStop(true)

const abortCtrl = new AbortController()
return Api.getTakerStop({ signal: abortCtrl.signal, walletName: wallet.name, token: wallet.token })
.then((res) => (res.ok ? true : Api.Helper.throwError(res, t('scheduler.error_stopping_schedule_failed'))))
.then((_) => setCollaborativeOperationRunning(false))
.then((_) =>
Promise.all([
reloadServiceInfo({ signal: abortCtrl.signal }),
reloadCurrentWalletInfo({ signal: abortCtrl.signal }),
])
)
.catch((err) => {
if (abortCtrl.signal.aborted) return
setAlert({ variant: 'danger', message: err.message })
setIsWaitingSchedulerStop(false)
})
.finally(() => {
if (abortCtrl.signal.aborted) return
setIsLoading(false)
})
.finally(() => setIsLoading(false))
}

return (
<>
<PageTitle title={t('scheduler.title')} subtitle={t('scheduler.subtitle')} />
{alert && <rb.Alert variant={alert.variant}>{alert.message}</rb.Alert>}

{isLoading ? (
{isLoading || isWaitingSchedulerStart || isWaitingSchedulerStop ? (
<rb.Placeholder as="div" animation="wave">
<rb.Placeholder xs={12} className={styles['input-loader']} />
</rb.Placeholder>
Expand Down

0 comments on commit 509a15e

Please sign in to comment.