Add toggleOnHand action

It replicate the behavior of setOnDemand angular directive
This commit is contained in:
Gaetan Craig-Riou
2024-08-06 15:43:42 +10:00
parent 25171413ef
commit cda57fdb44
2 changed files with 13 additions and 29 deletions

View File

@@ -1,19 +0,0 @@
angular.module("admin.products").directive "setOnDemand", ->
link: (scope, element, attr) ->
onHand = element.context.querySelector("#variant_on_hand")
onDemand = element.context.querySelector("#variant_on_demand")
disableOnHandIfOnDemand = ->
if onDemand.checked
onHand.disabled = 'disabled'
onHand.dataStock = onHand.value
onHand.value = t('admin.products.variants.infinity')
disableOnHandIfOnDemand()
onDemand.addEventListener 'change', (event) ->
disableOnHandIfOnDemand()
if !onDemand.checked
onHand.removeAttribute('disabled')
onHand.value = onHand.dataStock

View File

@@ -6,6 +6,8 @@ import UnitPrices from "js/services/unit_prices";
//
// TODO refactor so we can extract what's common with Bulk product page
export default class EditVariantController extends Controller {
static targets = ["onHand"];
connect() {
this.unitPrices = new UnitPrices();
// idea: create a helper that includes a nice getter/setter for Rails model attr values, just pass it the attribute name.
@@ -67,6 +69,17 @@ export default class EditVariantController extends Controller {
// Make sure to clean up anything that happened outside
}
toggleOnHand(event) {
if (event.target.checked === true) {
this.onHandTarget.dataStock = this.onHandTarget.value;
this.onHandTarget.value = I18n.t("admin.products.variants.infinity");
this.onHandTarget.disabled = "disabled";
} else {
this.onHandTarget.removeAttribute("disabled");
this.onHandTarget.value = this.onHandTarget.dataStock;
}
}
// private
// Extract variant_unit and variant_unit_scale from dropdown variant_unit_with_scale,
@@ -156,14 +169,4 @@ export default class EditVariantController extends Controller {
this.weight = this.element.querySelector('[id="variant_weight"]');
this.weight.parentElement.style.display = display;
}
//#showWeight() {
// this.weight = this.element.querySelector('[id="variant_weight"]');
// this.weight.parentElement.style.display= "block"
//}
//#hideWeight() {
// this.weight = this.element.querySelector('[id="variant_weight"]');
// this.weight.parentElement.style.display= "none"
//}
}