UNPKG

bananas-commerce-admin

Version:

What's this, an admin for apes?

56 lines (48 loc) 1.33 kB
import { PurchaseAmountItem } from "../../../types"; export function useExchangedData( data: PurchaseAmountItem[], shouldExchange: boolean, toCurrency: string = "SEK", exchangeRates: Record<string, number> = sekExchangeRates, ) { const exchangedData = data.map((item) => { if (shouldExchange && item.currency.toUpperCase() in exchangeRates) { return { ...item, amount: ( parseFloat(item.amount) * exchangeRates[item.currency.toUpperCase() as keyof typeof exchangeRates] ).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, };