From 64f60d1c8c8cd16c9ba6d994e1f7aeb126c7450b Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 13 Aug 2024 15:44:19 +1000 Subject: [PATCH] Fix small bug on edit variant page - make sure the weight is only cleared when needed - make sure the displayed unit is up to date --- .../controllers/edit_variant_controller.js | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/webpacker/controllers/edit_variant_controller.js b/app/webpacker/controllers/edit_variant_controller.js index a01fda48e2..10e5b742f3 100644 --- a/app/webpacker/controllers/edit_variant_controller.js +++ b/app/webpacker/controllers/edit_variant_controller.js @@ -60,13 +60,22 @@ export default class EditVariantController extends Controller { // on variantUnit change we need to check if weight needs to be toggled this.variantUnit.addEventListener("change", this.#toggleWeight.bind(this), { passive: true }); + // make sure the unit is correct when page is reload after an error + this.#updateUnitDisplay(); // update unit price on page load this.#processUnitPrice(); - this.#toggleWeight(); + + if (this.variantUnit.value === "weight") { + return this.#hideWeight(); + } } disconnect() { // Make sure to clean up anything that happened outside + // TODO remove all added event + this.variantUnit.removeEventListener("change", this.#toggleWeight.bind(this), { + passive: true, + }); } toggleOnHand(event) { @@ -160,13 +169,21 @@ export default class EditVariantController extends Controller { this.element.querySelector('[id="variant_unit_price"]').value = unit_price; } + #hideWeight() { + this.weight = this.element.querySelector('[id="variant_weight"]'); + this.weight.parentElement.style.display = "none"; + } + #toggleWeight() { - let display = "block"; if (this.variantUnit.value === "weight") { - display = "none"; + return this.#hideWeight(); } + // Show weight this.weight = this.element.querySelector('[id="variant_weight"]'); - this.weight.parentElement.style.display = display; + this.weight.parentElement.style.display = "block"; + // Clearing weight value to remove calculated weight for a variant with unit set to "weight" + // See Spree::Variant hook update_weight_from_unit_value + this.weight.value = ""; } }