Skip to content

Commit

Permalink
chore(send): remove unnecessary helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 11, 2023
1 parent 06ab774 commit b7dea3f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 64 deletions.
47 changes: 0 additions & 47 deletions src/components/Send/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ServiceConfigContextEntry } from '../../context/ServiceConfigContext'
import { isValidNumber } from '../../utils'

export const initialNumCollaborators = (minValue: number) => {
Expand Down Expand Up @@ -28,49 +27,3 @@ export const isValidAmount = (candidate: number | null, isSweep: boolean) => {
export const isValidNumCollaborators = (candidate: number | null, minNumCollaborators: number) => {
return candidate !== null && isValidNumber(candidate) && candidate >= minNumCollaborators && candidate <= 99
}

export const enhanceDirectPaymentErrorMessageIfNecessary = async (
httpStatus: number,
errorMessage: string,
onBadRequest: (errorMessage: string) => string,
) => {
const tryEnhanceMessage = httpStatus === 400
if (tryEnhanceMessage) {
return onBadRequest(errorMessage)
}

return errorMessage
}

export const enhanceTakerErrorMessageIfNecessary = async (
loadConfigValue: ServiceConfigContextEntry['loadConfigValueIfAbsent'],
httpStatus: number,
errorMessage: string,
onMaxFeeSettingsMissing: (errorMessage: string) => string,
) => {
const tryEnhanceMessage = httpStatus === 409
if (tryEnhanceMessage) {
const abortCtrl = new AbortController()

const configExists = (section: string, field: string) =>
loadConfigValue({
signal: abortCtrl.signal,
key: { section, field },
})
.then((val) => val.value !== null)
.catch(() => false)

const maxFeeSettingsPresent = await Promise.all([
configExists('POLICY', 'max_cj_fee_rel'),
configExists('POLICY', 'max_cj_fee_abs'),
])
.then((arr) => arr.every((e) => e))
.catch(() => false)

if (!maxFeeSettingsPresent) {
return onMaxFeeSettingsMissing(errorMessage)
}
}

return errorMessage
}
23 changes: 6 additions & 17 deletions src/components/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import { JM_MINIMUM_MAKERS_DEFAULT } from '../../constants/config'
import { SATS, formatSats, isValidNumber, scrollToTop } from '../../utils'

import {
enhanceDirectPaymentErrorMessageIfNecessary,
enhanceTakerErrorMessageIfNecessary,
initialNumCollaborators,
isValidAddress,
isValidAmount,
Expand Down Expand Up @@ -354,13 +352,11 @@ export default function Send({ wallet }: SendProps) {
setWaitForUtxosToBeSpent(inputs.map((it: any) => it.outpoint))
success = true
} else {
const message = await Api.Helper.extractErrorMessage(res)
const displayMessage = await enhanceDirectPaymentErrorMessageIfNecessary(
res.status,
message,
(errorMessage) => `${errorMessage} ${t('send.direct_payment_error_message_bad_request')}`,
)
setAlert({ variant: 'danger', message: displayMessage })
const errorMessage = await Api.Helper.extractErrorMessage(res)
const message = `${errorMessage} ${
res.status === 400 ? t('send.direct_payment_error_message_bad_request') : ''
}`
setAlert({ variant: 'danger', message })
}

setIsSending(false)
Expand Down Expand Up @@ -399,14 +395,7 @@ export default function Send({ wallet }: SendProps) {
success = true
} else {
const message = await Api.Helper.extractErrorMessage(res)
const displayMessage = await enhanceTakerErrorMessageIfNecessary(
loadConfigValue,
res.status,
message,
(errorMessage) => `${errorMessage} ${t('send.taker_error_message_max_fees_config_missing')}`,
)

setAlert({ variant: 'danger', message: displayMessage })
setAlert({ variant: 'danger', message })
}

setIsSending(false)
Expand Down

0 comments on commit b7dea3f

Please sign in to comment.