mirror of
https://github.com/we-promise/sure.git
synced 2026-04-10 15:54:48 +00:00
Add exchange rate feature with multi-currency transactions and transfers support (#1099)
Co-authored-by: Pedro J. Aramburu <pedro@joakin.dev>
This commit is contained in:
committed by
GitHub
parent
8e81e967fc
commit
f699660479
60
app/javascript/controllers/transaction_form_controller.js
Normal file
60
app/javascript/controllers/transaction_form_controller.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import ExchangeRateFormController from "controllers/exchange_rate_form_controller";
|
||||
|
||||
// Connects to data-controller="transaction-form"
|
||||
export default class extends ExchangeRateFormController {
|
||||
static targets = [
|
||||
...ExchangeRateFormController.targets,
|
||||
"account",
|
||||
"currency"
|
||||
];
|
||||
|
||||
hasRequiredExchangeRateTargets() {
|
||||
if (!this.hasAccountTarget || !this.hasCurrencyTarget || !this.hasDateTarget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
getExchangeRateContext() {
|
||||
if (!this.hasRequiredExchangeRateTargets()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const accountId = this.accountTarget.value;
|
||||
const currency = this.currencyTarget.value;
|
||||
const date = this.dateTarget.value;
|
||||
|
||||
if (!accountId || !currency) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const accountCurrency = this.accountCurrenciesValue[accountId];
|
||||
if (!accountCurrency) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
fromCurrency: currency,
|
||||
toCurrency: accountCurrency,
|
||||
date
|
||||
};
|
||||
}
|
||||
|
||||
isCurrentExchangeRateState(fromCurrency, toCurrency, date) {
|
||||
if (!this.hasRequiredExchangeRateTargets()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentAccountId = this.accountTarget.value;
|
||||
const currentCurrency = this.currencyTarget.value;
|
||||
const currentDate = this.dateTarget.value;
|
||||
const currentAccountCurrency = this.accountCurrenciesValue[currentAccountId];
|
||||
|
||||
return fromCurrency === currentCurrency && toCurrency === currentAccountCurrency && date === currentDate;
|
||||
}
|
||||
|
||||
onCurrencyChange() {
|
||||
this.checkCurrencyDifference();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user