Default to last purchased price and quantity when adding transaction item

This commit is contained in:
2023-07-28 20:23:31 +01:00
parent 4fc1052829
commit 560c162ee1
7 changed files with 104 additions and 58 deletions

View File

@ -1,17 +0,0 @@
import { Controller } from "/lib/hotwired/stimulus/dist/stimulus.js";
export default class ListFilterController extends Controller {
static targets = ["option"];
filter(event) {
for (const option of this.optionTargets) {
if (!event.target.value) {
option.disabled = false;
continue;
}
const value = option.getAttribute("data-list-filter-value");
option.disabled = value !== event.target.value;
}
}
}

View File

@ -0,0 +1,39 @@
import { Controller } from "/lib/hotwired/stimulus/dist/stimulus.js";
export default class TransactionItemFormController extends Controller {
static targets = ["option", "price", "quantity"];
filterNames(event) {
for (const option of this.optionTargets) {
if (!event.target.value) {
option.disabled = false;
continue;
}
const value = option.getAttribute("data-brand");
option.disabled = value !== event.target.value;
}
}
setPriceAndQuantity(event) {
const { brand, name } = event.target.form.elements;
if (!brand.value || !name.value) {
this.priceTarget.value = "";
this.quantityTarget.value = "1";
return;
}
const option = this.optionTargets.find(option =>
option.getAttribute("data-brand") === brand.value &&
option.value === name.value);
if (option != null) {
if (!this.priceTarget.value) {
this.priceTarget.value = option.getAttribute("data-price");
}
if (!this.quantityTarget.value || this.quantityTarget.value === "1") {
this.quantityTarget.value = option.getAttribute("data-quantity");
}
}
}
}