@orderingstack/front-components-product-configurator 1.4.2-beta.2 → 1.4.2-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -6686,6 +6686,7 @@ function useProductConfigurator(product) {
6686
6686
  return {
6687
6687
  encodedProduct,
6688
6688
  initSelected,
6689
+ initFilter,
6689
6690
  filter,
6690
6691
  setCurrency,
6691
6692
  setIsOnSubmittedOnTrue,
@@ -6851,16 +6852,25 @@ var ENotificationType;
6851
6852
  ENotificationType2["ORDER_UPSELL"] = "OrderUpsell";
6852
6853
  })(ENotificationType || (ENotificationType = {}));
6853
6854
  const validateProductFilter = (product, stateFilter) => {
6855
+ var _a;
6854
6856
  const { filter: productFilters = {}, fltCtx = "" } = product;
6855
- const stateFiltersWithFltCtx = stateFilter[fltCtx];
6856
- const hasProductFilters = !!Object.keys(productFilters).length;
6857
- if (hasProductFilters && stateFiltersWithFltCtx) {
6858
- const commonFilters = Object.keys(stateFiltersWithFltCtx).filter(
6859
- (filterName) => Object.keys(productFilters).includes(filterName) && productFilters[filterName].includes(stateFiltersWithFltCtx[filterName])
6860
- );
6861
- return commonFilters.length > 0;
6857
+ const stateFiltersWithFltCtx = (stateFilter == null ? void 0 : stateFilter[fltCtx]) || {};
6858
+ if (!productFilters || !Object.keys(productFilters).length || !stateFiltersWithFltCtx) {
6859
+ return true;
6862
6860
  }
6863
- return true;
6861
+ const commonFilters = (_a = Object.keys(stateFiltersWithFltCtx)) == null ? void 0 : _a.filter(
6862
+ (filterName) => {
6863
+ var _a2;
6864
+ const matchingFilters = (_a2 = Object.keys((productFilters == null ? void 0 : productFilters[filterName]) || {})) == null ? void 0 : _a2.filter((x2) => {
6865
+ var _a3;
6866
+ if (((_a3 = productFilters == null ? void 0 : productFilters[filterName]) == null ? void 0 : _a3[x2]) === (stateFiltersWithFltCtx == null ? void 0 : stateFiltersWithFltCtx[filterName])) {
6867
+ return true;
6868
+ }
6869
+ });
6870
+ return (matchingFilters == null ? void 0 : matchingFilters.length) > 0;
6871
+ }
6872
+ );
6873
+ return (commonFilters == null ? void 0 : commonFilters.length) > 0;
6864
6874
  };
6865
6875
  const getProductToChangeInForm = (product, isFilterValidated) => {
6866
6876
  const { items, filter } = product;
@@ -6885,12 +6895,14 @@ const getProductToChangeInForm = (product, isFilterValidated) => {
6885
6895
  return { productsToSelect, productsToUnselect };
6886
6896
  };
6887
6897
  const unselectProductsInForm = (productsToUnselect, setValueInForm) => {
6898
+ console.log("LOG1 productsToUnselect", productsToUnselect);
6888
6899
  productsToUnselect.map(({ selCtx, id }) => {
6889
6900
  setValueInForm(`selected.${selCtx}.${id}`, false);
6890
6901
  });
6891
6902
  };
6892
- const selectProductsInForm = (productToUnselect, setValueInForm, initialSelectedState) => {
6893
- productToUnselect.map(({ selCtx, id }) => {
6903
+ const selectProductsInForm = (productToSelect, setValueInForm, initialSelectedState) => {
6904
+ console.log("LOG1 productToSelect", productToSelect);
6905
+ productToSelect.map(({ selCtx, id }) => {
6894
6906
  var _a;
6895
6907
  const productInInitialState = (_a = initialSelectedState[selCtx]) == null ? void 0 : _a[id];
6896
6908
  if (productInInitialState) {
@@ -6903,12 +6915,12 @@ const selectProductsInForm = (productToUnselect, setValueInForm, initialSelected
6903
6915
  };
6904
6916
  function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6905
6917
  const { selCtx, attrs = {} } = product;
6906
- const stateFilter = useStore((state) => state.filter);
6918
+ useStore((state) => state.filter);
6907
6919
  const initialSelected = useStore((state) => state.initialSelected);
6908
6920
  const currency = useStore((state) => state.currency);
6909
6921
  const { setValue, getValues } = useFormContext();
6910
- const { selected = {} } = getValues();
6911
- const isFilterValidated = validateProductFilter(product, stateFilter);
6922
+ const { selected = {}, filter = {} } = getValues();
6923
+ const isFilterValidated = validateProductFilter(product, filter);
6912
6924
  const isOnSubmitted = useStore((state) => state.isOnSubmitted);
6913
6925
  const setValidationMap = useStore((state) => state.setValidationMap);
6914
6926
  const [selCnt, qtyCnt] = getSelCtxCntFromSelectedState(
@@ -6931,9 +6943,11 @@ function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6931
6943
  product,
6932
6944
  isFilterValidated
6933
6945
  );
6946
+ console.log("LOG1 useMenuOptions useEffect productsToUnselect", productsToUnselect);
6947
+ console.log("LOG1 useMenuOptions useEffect productsToSelect", productsToSelect);
6934
6948
  unselectProductsInForm(productsToUnselect, setValue);
6935
6949
  selectProductsInForm(productsToSelect, setValue, initialSelected);
6936
- }, [stateFilter]);
6950
+ }, [isFilterValidated, selected]);
6937
6951
  return {
6938
6952
  isFilterValidated,
6939
6953
  selected,
@@ -8794,14 +8808,63 @@ function Counter(props) {
8794
8808
  })]
8795
8809
  });
8796
8810
  }
8797
- function useEmitFilter(product) {
8798
- const { watch } = useFormContext();
8799
- const setFilter = useStore((state) => state.setFilter);
8811
+ function useEmitFilter(product, isRadio, nestedLvl) {
8812
+ const { watch, setValue, getValues, unregister } = useFormContext();
8800
8813
  const { id, selCtx, emit = {}, fltCtx } = product;
8801
8814
  const value = watch(`selected.${selCtx}.${id}`);
8802
8815
  useEffect(() => {
8816
+ var _a, _b, _c;
8817
+ if (!nestedLvl || nestedLvl > 1) {
8818
+ return;
8819
+ }
8820
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8821
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8822
+ const fltCtxPath = `filter.${fltCtx}`;
8823
+ if (isRadio && value && fltCtx) {
8824
+ if (isEmpty(emit)) {
8825
+ const groupValues = getValues(groupCntPath);
8826
+ (_a = Object.keys(groupValues || {})) == null ? void 0 : _a.forEach((x2) => {
8827
+ unregister(`${groupCntPath}.${x2}`);
8828
+ const groupsKeys = Object.keys(getValues(groupsPath) || {});
8829
+ if (isEmpty(groupsKeys)) {
8830
+ unregister(`${fltCtxPath}.${x2}`);
8831
+ } else {
8832
+ groupsKeys.forEach((groupSelCtx) => {
8833
+ if (groupSelCtx !== selCtx && !getValues(`${groupsPath}.${groupSelCtx}.${x2}`)) {
8834
+ unregister(`${fltCtxPath}.${x2}`);
8835
+ }
8836
+ });
8837
+ }
8838
+ });
8839
+ return;
8840
+ } else {
8841
+ const productGroupCntKeys = getValues(groupCntPath);
8842
+ const groupsCntKeys = getValues(groupsPath);
8843
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((x2) => {
8844
+ var _a2;
8845
+ (_a2 = Object.keys(productGroupCntKeys || {})) == null ? void 0 : _a2.forEach((productGroupCntKey) => {
8846
+ if (!emit[productGroupCntKey]) {
8847
+ unregister(`${fltCtxPath}.${productGroupCntKey}`);
8848
+ unregister(`${groupCntPath}.${productGroupCntKey}`);
8849
+ }
8850
+ });
8851
+ Object.keys(groupsCntKeys || {}).forEach((groupSelCtx) => {
8852
+ if (groupSelCtx !== selCtx && x2 in getValues(groupsPath == null ? void 0 : groupsPath[groupSelCtx])) {
8853
+ unregister(`${groupsPath}.${groupSelCtx}.${x2}`);
8854
+ }
8855
+ });
8856
+ setValue(`${fltCtxPath}.${x2}`, emit[x2]);
8857
+ setValue(`${groupCntPath}.${x2}`, { cnt: 1, value: emit[x2] });
8858
+ });
8859
+ return;
8860
+ }
8861
+ }
8803
8862
  if (value && fltCtx && !isEmpty(emit)) {
8804
- setFilter(fltCtx, emit);
8863
+ (_c = Object.keys(emit)) == null ? void 0 : _c.forEach((x2) => {
8864
+ setValue(`filter.${fltCtx}.${x2}`, emit[x2]);
8865
+ const groupKeyCnt = getValues(`${groupCntPath}.${x2}.cnt`) || 0;
8866
+ setValue(`${groupCntPath}.${x2}`, { cnt: groupKeyCnt + 1, value: emit[x2] });
8867
+ });
8805
8868
  }
8806
8869
  }, [value]);
8807
8870
  return {};
@@ -8833,62 +8896,105 @@ const removeProductFromState = (selCtx, getValues, setValue) => {
8833
8896
  }, {})
8834
8897
  );
8835
8898
  };
8836
- const selectUnselectProduct = (setValue, getValues, items, isChecked, state) => {
8899
+ const selectUnselectProduct = (setValue, getValues, product, isChecked, unregister, nestedLvl) => {
8900
+ var _a, _b;
8901
+ const { items, state, emit, fltCtx, selCtx } = product;
8837
8902
  const { selected = {} } = state || {};
8903
+ console.log("selected/unselected product", product, isChecked);
8838
8904
  if (isChecked) {
8839
8905
  addProductToState(selected, setValue);
8840
8906
  const checkSubProducts = (items2) => {
8841
- var _a;
8907
+ var _a2;
8842
8908
  if (!Array.isArray(items2) || isEmpty(state == null ? void 0 : state.selected))
8843
8909
  return;
8844
- (_a = items2 == null ? void 0 : items2.filter((x2) => {
8845
- var _a2, _b;
8846
- return !!((_b = (_a2 = state.selected) == null ? void 0 : _a2[`${x2.selCtx}`]) == null ? void 0 : _b[`${x2.id}`]);
8847
- })) == null ? void 0 : _a.map((product) => {
8848
- if (product.state && "selected" in product.state) {
8910
+ (_a2 = items2 == null ? void 0 : items2.filter((x2) => {
8911
+ var _a3, _b2;
8912
+ return !!((_b2 = (_a3 = state.selected) == null ? void 0 : _a3[`${x2.selCtx}`]) == null ? void 0 : _b2[`${x2.id}`]);
8913
+ })) == null ? void 0 : _a2.map((product2) => {
8914
+ if (product2.state && "selected" in product2.state) {
8849
8915
  const {
8850
8916
  state: { selected: selected2 }
8851
- } = product;
8917
+ } = product2;
8852
8918
  addProductToState(selected2, setValue);
8853
8919
  }
8854
- if (Array.isArray(product.items)) {
8855
- checkSubProducts(product.items);
8920
+ if (Array.isArray(product2.items)) {
8921
+ checkSubProducts(product2.items);
8856
8922
  }
8857
8923
  });
8858
8924
  };
8859
8925
  checkSubProducts(items);
8860
8926
  } else {
8861
- Object.keys(selected).forEach((selCtx) => {
8862
- removeProductFromState(selCtx, getValues, setValue);
8927
+ Object.keys(selected).forEach((selCtx2) => {
8928
+ removeProductFromState(selCtx2, getValues, setValue);
8863
8929
  });
8864
8930
  const checkSubProducts = (items2) => {
8865
8931
  if (!Array.isArray(items2))
8866
8932
  return;
8867
- items2.map((product) => {
8868
- const { selCtx } = product;
8869
- const changedSelCtx = getValues(`selected.${selCtx}`);
8933
+ items2.map((product2) => {
8934
+ const { selCtx: selCtx2 } = product2;
8935
+ const changedSelCtx = getValues(`selected.${selCtx2}`);
8870
8936
  if (changedSelCtx) {
8871
- removeProductFromState(selCtx, getValues, setValue);
8937
+ removeProductFromState(selCtx2, getValues, setValue);
8872
8938
  }
8873
- checkSubProducts(product.items);
8939
+ checkSubProducts(product2.items);
8874
8940
  });
8875
8941
  };
8876
8942
  checkSubProducts(items);
8943
+ (_a = Object.keys(emit)) == null ? void 0 : _a.forEach((x2) => {
8944
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
8945
+ const cnt = getValues(formPath) || 0;
8946
+ if (cnt < 2) {
8947
+ unregister(formPath);
8948
+ } else {
8949
+ setValue(formPath, cnt - 1);
8950
+ }
8951
+ });
8952
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
8953
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8954
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8955
+ const fltCtxPath = `filter.${fltCtx}`;
8956
+ (_b = Object.keys(product.emit)) == null ? void 0 : _b.forEach((emitKey) => {
8957
+ var _a2, _b2, _c;
8958
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
8959
+ if (groupValue) {
8960
+ if (groupValue > 1) {
8961
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
8962
+ } else {
8963
+ unregister(`${groupCntPath}.${emitKey}`);
8964
+ const groupKeysObj = getValues(groupsPath) || {};
8965
+ const otherGroupsCntKeys = (_a2 = Object.keys(groupKeysObj)) == null ? void 0 : _a2.filter((x2) => x2 !== selCtx);
8966
+ if (!otherGroupsCntKeys) {
8967
+ unregister(`${fltCtxPath}.${emitKey}`);
8968
+ } else {
8969
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
8970
+ var _a3;
8971
+ return !!((_a3 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a3[`${emitKey}`]);
8972
+ });
8973
+ if (!sameKeyOtherGroup) {
8974
+ unregister(`${fltCtxPath}.${emitKey}`);
8975
+ } else {
8976
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
8977
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
8978
+ }
8979
+ }
8980
+ }
8981
+ }
8982
+ });
8983
+ }
8877
8984
  }
8878
8985
  };
8879
- function useMenuProduct(product, parentAttrs) {
8986
+ function useMenuProduct(product, parentAttrs, nestedLvl) {
8880
8987
  var _a;
8881
- const { id, selCtx } = product;
8988
+ const { id, selCtx, emit = {}, fltCtx } = product;
8882
8989
  const {
8883
8990
  allowMultiple: parentAllowMultiple,
8884
8991
  selMin: parentSelMin,
8885
8992
  selMax: parentSelMax
8886
8993
  } = parentAttrs;
8887
- useEmitFilter(product);
8888
8994
  const { isInputEnabled: isInputEnabled2, isInputCounterIncrementEnabled: isInputCounterIncrementEnabled2 } = useValidate();
8889
8995
  const refreshForm = useStore((state) => state.refreshForm);
8890
8996
  const currency = useStore((state) => state.currency);
8891
- const { watch, getValues, setValue, register } = useFormContext();
8997
+ const { watch, getValues, setValue, register, unregister } = useFormContext();
8892
8998
  const value = watch(`selected.${selCtx}.${id}`);
8893
8999
  const { selected = {} } = getValues();
8894
9000
  const valueNumber = Number(value || 0);
@@ -8896,7 +9002,8 @@ function useMenuProduct(product, parentAttrs) {
8896
9002
  const isCheckbox = !isRadio;
8897
9003
  const isCounter = isApiBooleanTrue(parentAllowMultiple);
8898
9004
  const isDecrementEnabled = valueNumber > (isRadio ? 1 : 0);
8899
- const isIncrementEnabled = isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
9005
+ const isIncrementEnabled = useEmitFilter(product, isRadio, nestedLvl);
9006
+ isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
8900
9007
  const isChecked = Number(((_a = selected == null ? void 0 : selected[selCtx]) == null ? void 0 : _a[id]) || 0) > 0;
8901
9008
  const isCheckboxEnabled = isInputEnabled2(product) || valueNumber;
8902
9009
  const registerId = `${isRadio ? "radio" : "selected"}.${selCtx}.${id}`;
@@ -8909,10 +9016,20 @@ function useMenuProduct(product, parentAttrs) {
8909
9016
  setValue(`selected.${selCtx}.${id}`, `${valueNumber + 1}`);
8910
9017
  };
8911
9018
  const handleCounterDecrementQty = () => {
9019
+ var _a2, _b;
8912
9020
  const decrementedValue = valueNumber - 1;
8913
9021
  setValue(`selected.${selCtx}.${id}`, `${decrementedValue}`);
8914
9022
  if (isCheckbox && decrementedValue === 0) {
8915
9023
  setValue(`selected.${selCtx}.${id}`, false);
9024
+ (_a2 = Object.keys(emit)) == null ? void 0 : _a2.forEach((x2) => {
9025
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
9026
+ const cnt = getValues(formPath) || 0;
9027
+ if (cnt < 2) {
9028
+ unregister(formPath);
9029
+ } else {
9030
+ setValue(formPath, cnt - 1);
9031
+ }
9032
+ });
8916
9033
  const checkSubProducts = (items) => {
8917
9034
  if (!Array.isArray(items))
8918
9035
  return;
@@ -8926,18 +9043,45 @@ function useMenuProduct(product, parentAttrs) {
8926
9043
  });
8927
9044
  };
8928
9045
  checkSubProducts(product.items);
9046
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
9047
+ const groupsPath = `filterWithCnt.${fltCtx}`;
9048
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
9049
+ const fltCtxPath = `filter.${fltCtx}`;
9050
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((emitKey) => {
9051
+ var _a3, _b2, _c;
9052
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
9053
+ if (groupValue) {
9054
+ if (groupValue > 1) {
9055
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
9056
+ } else {
9057
+ unregister(`${groupCntPath}.${emitKey}`);
9058
+ const groupKeysObj = getValues(groupsPath) || {};
9059
+ const otherGroupsCntKeys = (_a3 = Object.keys(groupKeysObj)) == null ? void 0 : _a3.filter((x2) => x2 !== selCtx);
9060
+ if (!otherGroupsCntKeys) {
9061
+ unregister(`${fltCtxPath}.${emitKey}`);
9062
+ } else {
9063
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
9064
+ var _a4;
9065
+ return !!((_a4 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a4[`${emitKey}`]);
9066
+ });
9067
+ if (!sameKeyOtherGroup) {
9068
+ unregister(`${fltCtxPath}.${emitKey}`);
9069
+ } else {
9070
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
9071
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
9072
+ }
9073
+ }
9074
+ }
9075
+ }
9076
+ });
9077
+ }
8929
9078
  }
8930
9079
  };
8931
9080
  const handleOnInputChange = (e2) => {
8932
9081
  const {
8933
9082
  target: { checked, name: selCtx2, defaultValue: productId }
8934
9083
  } = e2;
8935
- for (const key of Object.keys(selected || {})) {
8936
- if (key !== (product == null ? void 0 : product.selCtx) && (key == null ? void 0 : key.includes(product.selCtx)) && isRadio) {
8937
- setValue(`selected.${key}`, false);
8938
- }
8939
- }
8940
- selectUnselectProduct(setValue, getValues, product.items, checked, product.state);
9084
+ selectUnselectProduct(setValue, getValues, product, checked, unregister, nestedLvl);
8941
9085
  if (isRadio) {
8942
9086
  console.log("handleOnInputChange radio", selCtx2, productId);
8943
9087
  const selectedContextName = "selected." + selCtx2.replace("radio.", "").split(".")[0];
@@ -8986,7 +9130,7 @@ const MenuProduct = (props) => {
8986
9130
  handleCounterIncrementQty,
8987
9131
  handleCounterDecrementQty,
8988
9132
  currency
8989
- } = useMenuProduct(product, parentAttrs);
9133
+ } = useMenuProduct(product, parentAttrs, nestedLvl);
8990
9134
  const {
8991
9135
  MenuProductUI
8992
9136
  } = useComponents();
@@ -9222,10 +9366,14 @@ const DisplayState = () => {
9222
9366
  watch
9223
9367
  } = useFormContext();
9224
9368
  const selected = watch("selected");
9369
+ const filter = watch("filter");
9370
+ const filterWithCnt = watch("filterWithCnt");
9225
9371
  const validationMap = useStore((state) => state.validationMap);
9226
9372
  console.log({
9227
9373
  selected,
9228
- validationMap
9374
+ validationMap,
9375
+ filter,
9376
+ filterWithCnt
9229
9377
  });
9230
9378
  return /* @__PURE__ */ jsxs("div", {
9231
9379
  className: "flex flex-col items-center py-4",
@@ -10964,5 +11112,5 @@ function ProductConfigurator(props) {
10964
11112
  })
10965
11113
  });
10966
11114
  }
10967
- export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus, index_esm as hookForm, useProductData };
11115
+ export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus, getProductToChangeInForm, index_esm as hookForm, selectProductsInForm, unselectProductsInForm, useProductData, validateProductFilter };
10968
11116
  //# sourceMappingURL=index.es.js.map