@orderingstack/front-components-product-configurator 1.4.2-beta.0 → 1.4.2-beta.10

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,17 +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
- console.log("12345", productFilters);
6859
- const commonFilters = Object.keys(stateFiltersWithFltCtx).filter(
6860
- (filterName) => Object.keys(productFilters).includes(filterName) && productFilters[filterName].includes(stateFiltersWithFltCtx[filterName])
6861
- );
6862
- return commonFilters.length > 0;
6857
+ const stateFiltersWithFltCtx = (stateFilter == null ? void 0 : stateFilter[fltCtx]) || {};
6858
+ if (!productFilters || !Object.keys(productFilters).length || !stateFiltersWithFltCtx) {
6859
+ return true;
6863
6860
  }
6864
- 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;
6865
6874
  };
6866
6875
  const getProductToChangeInForm = (product, isFilterValidated) => {
6867
6876
  const { items, filter } = product;
@@ -6908,8 +6917,8 @@ function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6908
6917
  const initialSelected = useStore((state) => state.initialSelected);
6909
6918
  const currency = useStore((state) => state.currency);
6910
6919
  const { setValue, getValues } = useFormContext();
6911
- const { selected = {} } = getValues();
6912
- const isFilterValidated = validateProductFilter(product, stateFilter);
6920
+ const { selected = {}, filter = {} } = getValues();
6921
+ const isFilterValidated = validateProductFilter(product, filter);
6913
6922
  const isOnSubmitted = useStore((state) => state.isOnSubmitted);
6914
6923
  const setValidationMap = useStore((state) => state.setValidationMap);
6915
6924
  const [selCnt, qtyCnt] = getSelCtxCntFromSelectedState(
@@ -8795,17 +8804,63 @@ function Counter(props) {
8795
8804
  })]
8796
8805
  });
8797
8806
  }
8798
- function useEmitFilter(product) {
8799
- const { watch } = useFormContext();
8800
- const setFilter = useStore((state) => state.setFilter);
8807
+ function useEmitFilter(product, isRadio, nestedLvl) {
8808
+ const { watch, setValue, getValues, unregister } = useFormContext();
8801
8809
  const { id, selCtx, emit = {}, fltCtx } = product;
8802
8810
  const value = watch(`selected.${selCtx}.${id}`);
8803
- if (id === "MI1001001") {
8804
- console.log("use emit filter", value, fltCtx, emit);
8805
- }
8806
8811
  useEffect(() => {
8812
+ var _a, _b, _c;
8813
+ if (!nestedLvl || nestedLvl > 1) {
8814
+ return;
8815
+ }
8816
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8817
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8818
+ const fltCtxPath = `filter.${fltCtx}`;
8819
+ if (isRadio && value && fltCtx) {
8820
+ if (isEmpty(emit)) {
8821
+ const groupValues = getValues(groupCntPath);
8822
+ (_a = Object.keys(groupValues || {})) == null ? void 0 : _a.forEach((x2) => {
8823
+ unregister(`${groupCntPath}.${x2}`);
8824
+ const groupsKeys = Object.keys(getValues(groupsPath) || {});
8825
+ if (isEmpty(groupsKeys)) {
8826
+ unregister(`${fltCtxPath}.${x2}`);
8827
+ } else {
8828
+ groupsKeys.forEach((groupSelCtx) => {
8829
+ if (groupSelCtx !== selCtx && !getValues(`${groupsPath}.${groupSelCtx}.${x2}`)) {
8830
+ unregister(`${fltCtxPath}.${x2}`);
8831
+ }
8832
+ });
8833
+ }
8834
+ });
8835
+ return;
8836
+ } else {
8837
+ const productGroupCntKeys = getValues(groupCntPath);
8838
+ const groupsCntKeys = getValues(groupsPath);
8839
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((x2) => {
8840
+ var _a2;
8841
+ (_a2 = Object.keys(productGroupCntKeys || {})) == null ? void 0 : _a2.forEach((productGroupCntKey) => {
8842
+ if (!emit[productGroupCntKey]) {
8843
+ unregister(`${fltCtxPath}.${productGroupCntKey}`);
8844
+ unregister(`${groupCntPath}.${productGroupCntKey}`);
8845
+ }
8846
+ });
8847
+ Object.keys(groupsCntKeys || {}).forEach((groupSelCtx) => {
8848
+ if (groupSelCtx !== selCtx && x2 in getValues(groupsPath == null ? void 0 : groupsPath[groupSelCtx])) {
8849
+ unregister(`${groupsPath}.${groupSelCtx}.${x2}`);
8850
+ }
8851
+ });
8852
+ setValue(`${fltCtxPath}.${x2}`, emit[x2]);
8853
+ setValue(`${groupCntPath}.${x2}`, { cnt: 1, value: emit[x2] });
8854
+ });
8855
+ return;
8856
+ }
8857
+ }
8807
8858
  if (value && fltCtx && !isEmpty(emit)) {
8808
- setFilter(fltCtx, emit);
8859
+ (_c = Object.keys(emit)) == null ? void 0 : _c.forEach((x2) => {
8860
+ setValue(`filter.${fltCtx}.${x2}`, emit[x2]);
8861
+ const groupKeyCnt = getValues(`${groupCntPath}.${x2}.cnt`) || 0;
8862
+ setValue(`${groupCntPath}.${x2}`, { cnt: groupKeyCnt + 1, value: emit[x2] });
8863
+ });
8809
8864
  }
8810
8865
  }, [value]);
8811
8866
  return {};
@@ -8837,58 +8892,105 @@ const removeProductFromState = (selCtx, getValues, setValue) => {
8837
8892
  }, {})
8838
8893
  );
8839
8894
  };
8840
- const selectUnselectProduct = (setValue, getValues, items, isChecked, state) => {
8895
+ const selectUnselectProduct = (setValue, getValues, product, isChecked, unregister, nestedLvl) => {
8896
+ var _a, _b;
8897
+ const { items, state, emit, fltCtx, selCtx } = product;
8841
8898
  const { selected = {} } = state || {};
8899
+ console.log("selected/unselected product", product, isChecked);
8842
8900
  if (isChecked) {
8843
8901
  addProductToState(selected, setValue);
8844
8902
  const checkSubProducts = (items2) => {
8903
+ var _a2;
8845
8904
  if (!Array.isArray(items2) || isEmpty(state == null ? void 0 : state.selected))
8846
8905
  return;
8847
- items2.map((product) => {
8848
- if (product.state && "selected" in product.state) {
8906
+ (_a2 = items2 == null ? void 0 : items2.filter((x2) => {
8907
+ var _a3, _b2;
8908
+ return !!((_b2 = (_a3 = state.selected) == null ? void 0 : _a3[`${x2.selCtx}`]) == null ? void 0 : _b2[`${x2.id}`]);
8909
+ })) == null ? void 0 : _a2.map((product2) => {
8910
+ if (product2.state && "selected" in product2.state) {
8849
8911
  const {
8850
8912
  state: { selected: selected2 }
8851
- } = product;
8913
+ } = product2;
8852
8914
  addProductToState(selected2, setValue);
8853
8915
  }
8854
- if (Array.isArray(product.items)) {
8855
- checkSubProducts(product.items);
8916
+ if (Array.isArray(product2.items)) {
8917
+ checkSubProducts(product2.items);
8856
8918
  }
8857
8919
  });
8858
8920
  };
8859
8921
  checkSubProducts(items);
8860
8922
  } else {
8861
- Object.keys(selected).forEach((selCtx) => {
8862
- removeProductFromState(selCtx, getValues, setValue);
8923
+ Object.keys(selected).forEach((selCtx2) => {
8924
+ removeProductFromState(selCtx2, getValues, setValue);
8863
8925
  });
8864
8926
  const checkSubProducts = (items2) => {
8865
8927
  if (!Array.isArray(items2))
8866
8928
  return;
8867
- items2.map((product) => {
8868
- const { selCtx } = product;
8869
- const changedSelCtx = getValues(`selected.${selCtx}`);
8929
+ items2.map((product2) => {
8930
+ const { selCtx: selCtx2 } = product2;
8931
+ const changedSelCtx = getValues(`selected.${selCtx2}`);
8870
8932
  if (changedSelCtx) {
8871
- removeProductFromState(selCtx, getValues, setValue);
8933
+ removeProductFromState(selCtx2, getValues, setValue);
8872
8934
  }
8873
- checkSubProducts(product.items);
8935
+ checkSubProducts(product2.items);
8874
8936
  });
8875
8937
  };
8876
8938
  checkSubProducts(items);
8939
+ (_a = Object.keys(emit)) == null ? void 0 : _a.forEach((x2) => {
8940
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
8941
+ const cnt = getValues(formPath) || 0;
8942
+ if (cnt < 2) {
8943
+ unregister(formPath);
8944
+ } else {
8945
+ setValue(formPath, cnt - 1);
8946
+ }
8947
+ });
8948
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
8949
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8950
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8951
+ const fltCtxPath = `filter.${fltCtx}`;
8952
+ (_b = Object.keys(product.emit)) == null ? void 0 : _b.forEach((emitKey) => {
8953
+ var _a2, _b2, _c;
8954
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
8955
+ if (groupValue) {
8956
+ if (groupValue > 1) {
8957
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
8958
+ } else {
8959
+ unregister(`${groupCntPath}.${emitKey}`);
8960
+ const groupKeysObj = getValues(groupsPath) || {};
8961
+ const otherGroupsCntKeys = (_a2 = Object.keys(groupKeysObj)) == null ? void 0 : _a2.filter((x2) => x2 !== selCtx);
8962
+ if (!otherGroupsCntKeys) {
8963
+ unregister(`${fltCtxPath}.${emitKey}`);
8964
+ } else {
8965
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
8966
+ var _a3;
8967
+ return !!((_a3 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a3[`${emitKey}`]);
8968
+ });
8969
+ if (!sameKeyOtherGroup) {
8970
+ unregister(`${fltCtxPath}.${emitKey}`);
8971
+ } else {
8972
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
8973
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
8974
+ }
8975
+ }
8976
+ }
8977
+ }
8978
+ });
8979
+ }
8877
8980
  }
8878
8981
  };
8879
- function useMenuProduct(product, parentAttrs) {
8982
+ function useMenuProduct(product, parentAttrs, nestedLvl) {
8880
8983
  var _a;
8881
- const { id, selCtx } = product;
8984
+ const { id, selCtx, emit = {}, fltCtx } = product;
8882
8985
  const {
8883
8986
  allowMultiple: parentAllowMultiple,
8884
8987
  selMin: parentSelMin,
8885
8988
  selMax: parentSelMax
8886
8989
  } = parentAttrs;
8887
- useEmitFilter(product);
8888
8990
  const { isInputEnabled: isInputEnabled2, isInputCounterIncrementEnabled: isInputCounterIncrementEnabled2 } = useValidate();
8889
8991
  const refreshForm = useStore((state) => state.refreshForm);
8890
8992
  const currency = useStore((state) => state.currency);
8891
- const { watch, getValues, setValue, register } = useFormContext();
8993
+ const { watch, getValues, setValue, register, unregister } = useFormContext();
8892
8994
  const value = watch(`selected.${selCtx}.${id}`);
8893
8995
  const { selected = {} } = getValues();
8894
8996
  const valueNumber = Number(value || 0);
@@ -8896,7 +8998,8 @@ function useMenuProduct(product, parentAttrs) {
8896
8998
  const isCheckbox = !isRadio;
8897
8999
  const isCounter = isApiBooleanTrue(parentAllowMultiple);
8898
9000
  const isDecrementEnabled = valueNumber > (isRadio ? 1 : 0);
8899
- const isIncrementEnabled = isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
9001
+ const isIncrementEnabled = useEmitFilter(product, isRadio, nestedLvl);
9002
+ isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
8900
9003
  const isChecked = Number(((_a = selected == null ? void 0 : selected[selCtx]) == null ? void 0 : _a[id]) || 0) > 0;
8901
9004
  const isCheckboxEnabled = isInputEnabled2(product) || valueNumber;
8902
9005
  const registerId = `${isRadio ? "radio" : "selected"}.${selCtx}.${id}`;
@@ -8909,22 +9012,72 @@ function useMenuProduct(product, parentAttrs) {
8909
9012
  setValue(`selected.${selCtx}.${id}`, `${valueNumber + 1}`);
8910
9013
  };
8911
9014
  const handleCounterDecrementQty = () => {
9015
+ var _a2, _b;
8912
9016
  const decrementedValue = valueNumber - 1;
8913
9017
  setValue(`selected.${selCtx}.${id}`, `${decrementedValue}`);
8914
9018
  if (isCheckbox && decrementedValue === 0) {
8915
9019
  setValue(`selected.${selCtx}.${id}`, false);
9020
+ (_a2 = Object.keys(emit)) == null ? void 0 : _a2.forEach((x2) => {
9021
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
9022
+ const cnt = getValues(formPath) || 0;
9023
+ if (cnt < 2) {
9024
+ unregister(formPath);
9025
+ } else {
9026
+ setValue(formPath, cnt - 1);
9027
+ }
9028
+ });
9029
+ const checkSubProducts = (items) => {
9030
+ if (!Array.isArray(items))
9031
+ return;
9032
+ items.map((product2) => {
9033
+ const { selCtx: selCtx2 } = product2;
9034
+ const changedSelCtx = getValues(`selected.${selCtx2}`);
9035
+ if (changedSelCtx) {
9036
+ removeProductFromState(selCtx2, getValues, setValue);
9037
+ }
9038
+ checkSubProducts(product2.items);
9039
+ });
9040
+ };
9041
+ checkSubProducts(product.items);
9042
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
9043
+ const groupsPath = `filterWithCnt.${fltCtx}`;
9044
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
9045
+ const fltCtxPath = `filter.${fltCtx}`;
9046
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((emitKey) => {
9047
+ var _a3, _b2, _c;
9048
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
9049
+ if (groupValue) {
9050
+ if (groupValue > 1) {
9051
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
9052
+ } else {
9053
+ unregister(`${groupCntPath}.${emitKey}`);
9054
+ const groupKeysObj = getValues(groupsPath) || {};
9055
+ const otherGroupsCntKeys = (_a3 = Object.keys(groupKeysObj)) == null ? void 0 : _a3.filter((x2) => x2 !== selCtx);
9056
+ if (!otherGroupsCntKeys) {
9057
+ unregister(`${fltCtxPath}.${emitKey}`);
9058
+ } else {
9059
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
9060
+ var _a4;
9061
+ return !!((_a4 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a4[`${emitKey}`]);
9062
+ });
9063
+ if (!sameKeyOtherGroup) {
9064
+ unregister(`${fltCtxPath}.${emitKey}`);
9065
+ } else {
9066
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
9067
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
9068
+ }
9069
+ }
9070
+ }
9071
+ }
9072
+ });
9073
+ }
8916
9074
  }
8917
9075
  };
8918
9076
  const handleOnInputChange = (e2) => {
8919
9077
  const {
8920
9078
  target: { checked, name: selCtx2, defaultValue: productId }
8921
9079
  } = e2;
8922
- for (const key of Object.keys(selected || {})) {
8923
- if (key !== (product == null ? void 0 : product.selCtx) && (key == null ? void 0 : key.includes(product.selCtx)) && isRadio) {
8924
- setValue(`selected.${key}`, false);
8925
- }
8926
- }
8927
- selectUnselectProduct(setValue, getValues, product.items, checked, product.state);
9080
+ selectUnselectProduct(setValue, getValues, product, checked, unregister, nestedLvl);
8928
9081
  if (isRadio) {
8929
9082
  console.log("handleOnInputChange radio", selCtx2, productId);
8930
9083
  const selectedContextName = "selected." + selCtx2.replace("radio.", "").split(".")[0];
@@ -8973,7 +9126,7 @@ const MenuProduct = (props) => {
8973
9126
  handleCounterIncrementQty,
8974
9127
  handleCounterDecrementQty,
8975
9128
  currency
8976
- } = useMenuProduct(product, parentAttrs);
9129
+ } = useMenuProduct(product, parentAttrs, nestedLvl);
8977
9130
  const {
8978
9131
  MenuProductUI
8979
9132
  } = useComponents();
@@ -9208,14 +9361,15 @@ const DisplayState = () => {
9208
9361
  const {
9209
9362
  watch
9210
9363
  } = useFormContext();
9211
- const form = useFormContext();
9212
9364
  const selected = watch("selected");
9213
- const filter = form.getValues("filter");
9365
+ const filter = watch("filter");
9366
+ const filterWithCnt = watch("filterWithCnt");
9214
9367
  const validationMap = useStore((state) => state.validationMap);
9215
9368
  console.log({
9216
9369
  selected,
9217
9370
  validationMap,
9218
- filter
9371
+ filter,
9372
+ filterWithCnt
9219
9373
  });
9220
9374
  return /* @__PURE__ */ jsxs("div", {
9221
9375
  className: "flex flex-col items-center py-4",
@@ -10870,6 +11024,7 @@ function ProductConfigurator(props) {
10870
11024
  const {
10871
11025
  encodedProduct,
10872
11026
  initSelected,
11027
+ initFilter,
10873
11028
  filter,
10874
11029
  productQuantity,
10875
11030
  setIsOnSubmittedOnTrue,
@@ -10894,7 +11049,8 @@ function ProductConfigurator(props) {
10894
11049
  });
10895
11050
  const methods = useForm({
10896
11051
  defaultValues: {
10897
- selected: initSelected
11052
+ selected: initSelected,
11053
+ filter: initFilter
10898
11054
  }
10899
11055
  });
10900
11056
  const {
@@ -10912,7 +11068,6 @@ function ProductConfigurator(props) {
10912
11068
  });
10913
11069
  } else {
10914
11070
  console.log("selected", getValues("selected"));
10915
- console.log("filter", getValues("filter"));
10916
11071
  const decodedCleanedSelected = decodeSelected(cleanSelected(getValues("selected")));
10917
11072
  console.log("decodedCleanedSelected: ", decodedCleanedSelected);
10918
11073
  handleAddToCart({
@@ -10955,5 +11110,5 @@ function ProductConfigurator(props) {
10955
11110
  })
10956
11111
  });
10957
11112
  }
10958
- export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus, index_esm as hookForm, useProductData };
11113
+ export { ContextType, MainView, ProductConfigurator, ProductKinds, ProductTypes, SelMinSelMaxStatus, getProductToChangeInForm, index_esm as hookForm, selectProductsInForm, unselectProductsInForm, useProductData, validateProductFilter };
10959
11114
  //# sourceMappingURL=index.es.js.map