2024-04-20 14:07:06 +02:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
import { CurrenciesService } from "services/currencies_service";
|
|
|
|
|
|
|
|
// Connects to data-controller="money-field"
|
|
|
|
// when currency select change, update the input value with the correct placeholder and step
|
|
|
|
export default class extends Controller {
|
2024-07-16 14:08:24 -04:00
|
|
|
static targets = ["amount", "currency", "symbol"];
|
2024-04-20 14:07:06 +02:00
|
|
|
|
2024-07-16 14:08:24 -04:00
|
|
|
handleCurrencyChange(e) {
|
|
|
|
const selectedCurrency = e.target.value;
|
2024-04-20 14:07:06 +02:00
|
|
|
this.updateAmount(selectedCurrency);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAmount(currency) {
|
2024-07-16 14:08:24 -04:00
|
|
|
(new CurrenciesService).get(currency).then((currency) => {
|
|
|
|
console.log(currency)
|
|
|
|
this.amountTarget.step = currency.step;
|
|
|
|
|
|
|
|
if (isFinite(this.amountTarget.value)) {
|
|
|
|
this.amountTarget.value = parseFloat(this.amountTarget.value).toFixed(currency.default_precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.symbolTarget.innerText = currency.symbol;
|
2024-04-20 14:07:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|