2024-10-14 23:09:27 +02:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
const TRADE_TYPES = {
|
|
|
|
BUY: "buy",
|
|
|
|
SELL: "sell",
|
|
|
|
TRANSFER_IN: "transfer_in",
|
|
|
|
TRANSFER_OUT: "transfer_out",
|
2024-10-14 23:09:27 +02:00
|
|
|
INTEREST: "interest",
|
|
|
|
};
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
const FIELD_VISIBILITY = {
|
2024-10-14 23:09:27 +02:00
|
|
|
[TRADE_TYPES.BUY]: { ticker: true, qty: true, price: true },
|
|
|
|
[TRADE_TYPES.SELL]: { ticker: true, qty: true, price: true },
|
|
|
|
[TRADE_TYPES.TRANSFER_IN]: { amount: true, transferAccount: true },
|
|
|
|
[TRADE_TYPES.TRANSFER_OUT]: { amount: true, transferAccount: true },
|
|
|
|
[TRADE_TYPES.INTEREST]: { amount: true },
|
|
|
|
};
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
// Connects to data-controller="trade-form"
|
|
|
|
export default class extends Controller {
|
2024-10-14 23:09:27 +02:00
|
|
|
static targets = [
|
|
|
|
"typeInput",
|
|
|
|
"tickerInput",
|
|
|
|
"amountInput",
|
|
|
|
"transferAccountInput",
|
|
|
|
"qtyInput",
|
|
|
|
"priceInput",
|
|
|
|
];
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
connect() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this.handleTypeChange = this.handleTypeChange.bind(this);
|
|
|
|
this.typeInputTarget.addEventListener("change", this.handleTypeChange);
|
|
|
|
this.updateFields(this.typeInputTarget.value || TRADE_TYPES.BUY);
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this.typeInputTarget.removeEventListener("change", this.handleTypeChange);
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTypeChange(event) {
|
2024-10-14 23:09:27 +02:00
|
|
|
this.updateFields(event.target.value);
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateFields(type) {
|
2024-10-14 23:09:27 +02:00
|
|
|
const visibleFields = FIELD_VISIBILITY[type] || {};
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
Object.entries(this.fieldTargets).forEach(([field, target]) => {
|
2024-10-14 23:09:27 +02:00
|
|
|
const isVisible = visibleFields[field] || false;
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
// Update visibility
|
2024-10-14 23:09:27 +02:00
|
|
|
target.hidden = !isVisible;
|
2024-08-09 20:11:27 -04:00
|
|
|
|
|
|
|
// Update required status based on visibility
|
|
|
|
if (isVisible) {
|
2024-10-14 23:09:27 +02:00
|
|
|
target.setAttribute("required", "");
|
2024-08-09 20:11:27 -04:00
|
|
|
} else {
|
2024-10-14 23:09:27 +02:00
|
|
|
target.removeAttribute("required");
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
});
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get fieldTargets() {
|
|
|
|
return {
|
|
|
|
ticker: this.tickerInputTarget,
|
|
|
|
amount: this.amountInputTarget,
|
|
|
|
transferAccount: this.transferAccountInputTarget,
|
|
|
|
qty: this.qtyInputTarget,
|
2024-10-14 23:09:27 +02:00
|
|
|
price: this.priceInputTarget,
|
|
|
|
};
|
2024-08-09 20:11:27 -04:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
}
|