Browse Source

fix nao perder informacao da variacao do produto na loja

Gustavo Zanatta 1 tháng trước cách đây
mục cha
commit
76e98d4d41
1 tập tin đã thay đổi với 40 bổ sung3 xóa
  1. 40 3
      src/pages/loja/StoreItemFormPage.vue

+ 40 - 3
src/pages/loja/StoreItemFormPage.vue

@@ -340,12 +340,19 @@ const statusOptions = [
 const selectedCategory = ref(null);
 
 const variationType = ref("tamanho");
+const previousVariationType = ref("tamanho");
 const variations = ref([]);
 const newVariationLabel = ref("");
 const newVariationValue = ref(null);
 const newVariationStock = ref(null);
 const addingVariation = ref(false);
 
+const savedVariationStates = reactive({
+  tamanho: null,
+  cor: null,
+  modelo: null,
+});
+
 const variationTypeOptions = computed(() => [
   { label: t("loja.variation_tamanho"), value: "tamanho" },
   { label: t("loja.variation_cor"),     value: "cor"     },
@@ -370,12 +377,41 @@ const syncSizeVariations = () => {
     }));
 };
 
-const onVariationTypeChange = () => {
-  variations.value = [];
+const onVariationTypeChange = (newType) => {
+  const prevType = previousVariationType.value;
+
+  if (prevType === "tamanho") {
+    savedVariationStates.tamanho = sizeSlots.map((s) => ({
+      label: s.label, enabled: s.enabled, value: s.value, stock: s.stock,
+    }));
+  } else {
+    savedVariationStates[prevType] = variations.value.map((v) => ({ ...v }));
+  }
+
+  if (newType === "tamanho") {
+    const saved = savedVariationStates.tamanho;
+    sizeSlots.forEach((s, i) => {
+      if (saved) {
+        s.enabled = saved[i].enabled;
+        s.value   = saved[i].value;
+        s.stock   = saved[i].stock;
+      } else {
+        s.enabled = false;
+        s.value   = null;
+        s.stock   = null;
+      }
+    });
+    syncSizeVariations();
+  } else {
+    variations.value = savedVariationStates[newType]
+      ? [...savedVariationStates[newType]]
+      : [];
+  }
+
   newVariationLabel.value = "";
   newVariationValue.value = null;
   newVariationStock.value = null;
-  sizeSlots.forEach((s) => { s.enabled = false; s.value = null; s.stock = null; });
+  previousVariationType.value = newType;
 };
 
 
@@ -511,6 +547,7 @@ onMounted(async () => {
     if (item.variations?.length) {
       const type = item.variations[0].variation_type;
       variationType.value = type;
+      previousVariationType.value = type;
 
       if (type === "tamanho") {
         const labelMap = { P: 0, M: 1, G: 2, GG: 3 };