From e90b0e35577cd416796f41c61d3bdf6101d05b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 28 Oct 2021 16:51:36 +0100 Subject: [PATCH] fix: clean up code --- .../src/pages/Transactions/DetailsCard.js | 74 +++++++------------ 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/new-lamassu-admin/src/pages/Transactions/DetailsCard.js b/new-lamassu-admin/src/pages/Transactions/DetailsCard.js index 2a196719..cb436fb8 100644 --- a/new-lamassu-admin/src/pages/Transactions/DetailsCard.js +++ b/new-lamassu-admin/src/pages/Transactions/DetailsCard.js @@ -91,21 +91,22 @@ const DetailsRow = ({ it: tx, timezone }) => { const [action, setAction] = useState({ command: null }) const [errorMessage, setErrorMessage] = useState('') + const isCashIn = tx.txClass === 'cashIn' + const zip = new JSZip() const [fetchSummary] = useLazyQuery(TX_SUMMARY, { onCompleted: data => createCsv(data) }) - const [cancelCashOutTransaction] = useMutation(CANCEL_CASH_OUT_TRANSACTION, { - onError: ({ message }) => setErrorMessage(message ?? 'An error occurred.'), - refetchQueries: () => ['transactions'] - }) - - const [cancelCashInTransaction] = useMutation(CANCEL_CASH_IN_TRANSACTION, { - onError: ({ message }) => setErrorMessage(message ?? 'An error occurred.'), - refetchQueries: () => ['transactions'] - }) + const [cancelTransaction] = useMutation( + isCashIn ? CANCEL_CASH_IN_TRANSACTION : CANCEL_CASH_OUT_TRANSACTION, + { + onError: ({ message }) => + setErrorMessage(message ?? 'An error occurred.'), + refetchQueries: () => ['transactions'] + } + ) const fiat = Number.parseFloat(tx.fiat) const crypto = coinUtils.toUnit(new BigNumber(tx.cryptoAtoms), tx.cryptoCode) @@ -155,6 +156,13 @@ const DetailsRow = ({ it: tx, timezone }) => { ) + const getCancelMessage = () => { + const cashInMessage = `The user will not be able to redeem the inserted bills, even if they subsequently confirm the transaction. If they've already deposited bills, you'll need to reconcile this transaction with them manually.` + const cashOutMessage = `The user will not be able to redeem the cash, even if they subsequently send the required coins. If they've already sent you coins, you'll need to reconcile this transaction with them manually.` + + return isCashIn ? cashInMessage : cashOutMessage + } + return (
@@ -162,9 +170,9 @@ const DetailsRow = ({ it: tx, timezone }) => {
- {tx.txClass === 'cashOut' ? : } + {!isCashIn ? : } - {tx.txClass === 'cashOut' ? 'Cash-out' : 'Cash-in'} + {!isCashIn ? 'Cash-out' : 'Cash-in'}
@@ -259,7 +267,7 @@ const DetailsRow = ({ it: tx, timezone }) => {
- {tx.txClass === 'cashIn' + {isCashIn ? `${Number.parseFloat(tx.cashInFee)} ${tx.fiatCode}` : 'N/A'}
@@ -298,21 +306,7 @@ const DetailsRow = ({ it: tx, timezone }) => { ) : ( errorElements )} - {tx.txClass === 'cashIn' && getStatus(tx) === 'Pending' && ( - - setAction({ - command: 'cancelTx' - }) - }> - Cancel transaction - - )} - {tx.txClass === 'cashOut' && getStatus(tx) === 'Pending' && ( + {getStatus(tx) === 'Pending' && ( { title={`Cancel this transaction?`} errorMessage={errorMessage} toBeConfirmed={tx.machineName} - message={`The user will not be able to redeem the ${ - tx.txClass === 'cashIn' ? `inserted bills` : `cash` - }, even if they subsequently ${ - tx.txClass === 'cashIn' - ? `confirm the transaction` - : `send the required coins` - }. If they've already ${ - tx.txClass === 'cashIn' ? `deposited bills` : `sent you coins` - }, you'll need to reconcile this transaction with them manually.`} + message={getCancelMessage()} onConfirmed={() => { setErrorMessage(null) setAction({ command: null }) - tx.txClass === 'cashIn' - ? cancelCashInTransaction({ - variables: { - id: tx.id - } - }) - : cancelCashOutTransaction({ - variables: { - id: tx.id - } - }) + cancelTransaction({ + variables: { + id: tx.id + } + }) }} onDissmised={() => { setAction({ command: null })