Default to last purchased price and quantity when adding transaction item
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
39
Groceries/wwwroot/js/controllers/transaction-item-form.js
Normal file
39
Groceries/wwwroot/js/controllers/transaction-item-form.js
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import { Application } from "/lib/hotwired/stimulus/dist/stimulus.js";
|
||||
|
||||
import ListFilterController from "./controllers/list-filter.js";
|
||||
import ModalController from "./controllers/modal.js";
|
||||
import SearchFormController from "./controllers/search-form.js";
|
||||
import TransactionItemFormController from "./controllers/transaction-item-form.js";
|
||||
|
||||
const app = Application.start();
|
||||
app.register("list-filter", ListFilterController);
|
||||
app.register("modal", ModalController);
|
||||
app.register("search-form", SearchFormController);
|
||||
app.register("transaction-item-form", TransactionItemFormController);
|
||||
|
||||
let timeout;
|
||||
document.addEventListener("turbo:visit", () => {
|
||||
|
Reference in New Issue
Block a user