Skip to content

Instantly share code, notes, and snippets.

@lhuria94
Created July 1, 2018 14:30
Show Gist options
  • Save lhuria94/cdd753311d3b8abe1d318f9af4d4dfde to your computer and use it in GitHub Desktop.
Save lhuria94/cdd753311d3b8abe1d318f9af4d4dfde to your computer and use it in GitHub Desktop.
Refactored Reverse Calc method
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
});
}
@lhuria94
Copy link
Author

lhuria94 commented Jul 1, 2018

Old one:

handleReverseLookUp (event) {
    const {
      exchangeAmountPrecision,
      currencyData,
      conf,
      exchangeRatePrecision
    } = this.props;
    const {currency, convertType} = this.state;
    const {sendCurrency} = conf;
    let amount;

    // Calculate exchange rates incase of reverse lookup.
    const cashExchangeRate = typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`].cash !== 'undefined'
      ? convertToFloat(currencyData[currency][`from_${sendCurrency.toLowerCase()}`].cash.rate, exchangeRatePrecision)
      : 0;
    const cardExchangeRate = typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`].card !== 'undefined'
      ? convertToFloat(currencyData[currency][`from_${sendCurrency.toLowerCase()}`].card.rate, exchangeRatePrecision)
      : 0;

    if (event.target.name === 'cash') {
      amount = convertToFloat((1 / cashExchangeRate * event.target.value),
        exchangeAmountPrecision);

      // Calculate discounted rates incase of reverse lookup (Only for Cash).
      const checkCashDiscountRatesReverse = (typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`].cash !== 'undefined' && typeof currencyData[currency][`from_${sendCurrency.toLowerCase()}`].cash.discountRateConditions !== 'undefined')
        ? checkDiscountRateReverse(currencyData[currency][`from_${sendCurrency.toLowerCase()}`].cash.discountRateConditions, cashExchangeRate, amount, exchangeAmountPrecision, exchangeRatePrecision, event.target.value)
        : 0;

      // Check if discounted amount is available, else pass the non-discounted amount.
      amount = checkCashDiscountRatesReverse.amount > 0
        ? checkCashDiscountRatesReverse.amount : amount;
      const cardAmount = calculateExchangeAmount(cardExchangeRate,
        amount, exchangeAmountPrecision);

      this.setState({
        cashAmount: event.target.value,
        cardAmount
      });
    } else {
      amount = convertToFloat((1 / cardExchangeRate * event.target.value),
        exchangeAmountPrecision);
      const cashAmount = calculateExchangeAmount(cashExchangeRate,
        amount, exchangeAmountPrecision);

      this.setState({
        cardAmount: event.target.value,
        cashAmount
      });
    }

    const exchangeRateInfo = calculateExchangeRate(currencyData, currency, sendCurrency,
      amount, exchangeRatePrecision, exchangeAmountPrecision, convertType);

    this.setState({
      amount,
      cashExchangeRate,
      cardExchangeRate,
      cashDiscountInfo: exchangeRateInfo.checkCashDiscountRates
    });
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment