bananas-commerce-admin
Version:
What's this, an admin for apes?
36 lines • 1.29 kB
JavaScript
export function useExchangedData(data, shouldExchange, toCurrency = "SEK", exchangeRates = sekExchangeRates) {
const exchangedData = data.map((item) => {
if (shouldExchange && item.currency.toUpperCase() in exchangeRates) {
return {
...item,
amount: (parseFloat(item.amount) *
exchangeRates[item.currency.toUpperCase()]).toFixed(2),
currency: shouldExchange ? toCurrency : item.currency,
};
}
return {
...item,
amount: item.amount,
currency: item.currency,
};
});
const totalAmount = exchangedData.reduce((acc, item) => {
return acc + parseFloat(item.amount);
}, 0);
const onlyExchangedCurrency = exchangedData.every((item) => item.currency.toUpperCase() === toCurrency.toUpperCase());
const exchangeAvailable = data.some((item) => item.currency.toUpperCase() in exchangeRates &&
item.currency.toUpperCase() !== toCurrency.toUpperCase());
return {
exchangedData,
totalAmount,
onlyExchangedCurrency,
exchangeAvailable,
};
}
const sekExchangeRates = {
EUR: 11.5,
USD: 10.5,
SEK: 1,
GBP: 13.5,
};
//# sourceMappingURL=useExchangedData.js.map