Created
July 1, 2018 14:30
-
-
Save lhuria94/cdd753311d3b8abe1d318f9af4d4dfde to your computer and use it in GitHub Desktop.
Refactored Reverse Calc method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleReverseLookUp (event) { | |
const { | |
exchangeAmountPrecision, | |
currencyData, | |
conf, | |
exchangeRatePrecision | |
} = this.props; | |
const {currency} = this.state; | |
const {sendCurrency} = conf; | |
const el = event.target; | |
// Calculate active convert type exchange rate incase of reverse lookup. | |
const exchangeRate = typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`][el.name] !== 'undefined' | |
? convertToFloat(currencyData[currency][`from_${sendCurrency.toLowerCase()}`][el.name].rate, exchangeRatePrecision) | |
: 0; | |
let amount = convertToFloat((1 / exchangeRate * el.value), | |
exchangeAmountPrecision); | |
// Will result empty object if active convert type is not cash. | |
const checkCashDiscountRatesReverse = (typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`][el.name] !== 'undefined' && typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`][el.name].discountRateConditions !== 'undefined') | |
? checkDiscountRateReverse(currencyData[currency][`from_${sendCurrency.toLowerCase()}`][el.name].discountRateConditions, exchangeRate, amount, exchangeAmountPrecision, exchangeRatePrecision, el.value) | |
: 0; | |
// Check if cash discount is available, else return as it is. | |
amount = checkCashDiscountRatesReverse.amount > 0 | |
? checkCashDiscountRatesReverse.amount : amount; | |
const exchangeRateInfo = calculateExchangeRate(currencyData, currency, sendCurrency, | |
amount, exchangeRatePrecision, exchangeAmountPrecision, el.name); | |
const cashAmount = calculateExchangeAmount(exchangeRateInfo.cashExchangeRate, | |
amount, exchangeAmountPrecision); | |
const cardAmount = calculateExchangeAmount(exchangeRateInfo.cardExchangeRate, | |
amount, exchangeAmountPrecision); | |
this.setState({ | |
cashAmount: el.name === 'cash' ? el.value : cashAmount, | |
cardAmount: el.name === 'card' ? el.value : cardAmount, | |
amount, | |
cashExchangeRate: exchangeRateInfo.cashExchangeRate, | |
cardExchangeRate: exchangeRateInfo.cardExchangeRate, | |
cashDiscountInfo: exchangeRateInfo.checkCashDiscountRates | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Old one: