Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 87b13c9

Browse files
committed
Clean up amount label computation
1 parent daebaf5 commit 87b13c9

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/computed/invoice.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { computed, extendObservable } from 'mobx';
2-
import { toSatoshis, toAmountLabel } from '../helper';
2+
import { toLabel } from '../helper';
33

44
const ComputedInvoice = store => {
55
extendObservable(store, {
6-
invoiceAmountLabel: computed(() => {
7-
const { invoice, settings } = store;
8-
const satoshis = toSatoshis(invoice.amount, settings.unit);
9-
return toAmountLabel(satoshis, store.settings);
10-
}),
6+
invoiceAmountLabel: computed(() =>
7+
toLabel(store.invoice.amount, store.settings)
8+
),
119
});
1210
};
1311

src/computed/payment.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { computed, extendObservable } from 'mobx';
2-
import { toSatoshis, toAmountLabel } from '../helper';
2+
import { toSatoshis, toAmountLabel, toLabel } from '../helper';
33

44
const ComputedPayment = store => {
55
extendObservable(store, {
6-
paymentAmountLabel: computed(() => {
7-
const { payment, settings } = store;
8-
const satoshis = toSatoshis(payment.amount, settings.unit);
9-
return toAmountLabel(satoshis, store.settings);
10-
}),
11-
paymentFeeLabel: computed(() => {
12-
const { payment, settings } = store;
13-
const satoshis = toSatoshis(payment.fee, settings.unit);
14-
return toAmountLabel(satoshis, store.settings);
15-
}),
6+
paymentAmountLabel: computed(() =>
7+
toLabel(store.payment.amount, store.settings)
8+
),
9+
paymentFeeLabel: computed(() => toLabel(store.payment.fee, store.settings)),
1610
paymentTotalLabel: computed(() => {
1711
const { payment, settings } = store;
1812
const satAmount = toSatoshis(payment.amount, settings.unit);

src/helper.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ export const toAmountLabel = (satoshis, settings) => {
126126
: formatNumber(toAmount(satoshis, settings.unit));
127127
};
128128

129+
/**
130+
* Convert a string formatted BTC amount either to fiat or the selected BTC unit.
131+
* The output should be used throughout the UI for value labels.
132+
* @param {string} amount The amount e.g. '0.0001'
133+
* @param {Object} settings Contains the current exchange rate
134+
* @return {string} The corresponding value label
135+
*/
136+
export const toLabel = (amount, settings) => {
137+
const satoshis = toSatoshis(amount, settings.unit);
138+
return toAmountLabel(satoshis, settings);
139+
};
140+
129141
/**
130142
* Split '-' separated words and convert to uppercase
131143
* @param {string} value The input string

0 commit comments

Comments
 (0)