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

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;
@@ -6903,12 +6913,12 @@ const selectProductsInForm = (productToUnselect, setValueInForm, initialSelected
6903
6913
  };
6904
6914
  function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6905
6915
  const { selCtx, attrs = {} } = product;
6906
- const stateFilter = useStore((state) => state.filter);
6916
+ useStore((state) => state.filter);
6907
6917
  const initialSelected = useStore((state) => state.initialSelected);
6908
6918
  const currency = useStore((state) => state.currency);
6909
6919
  const { setValue, getValues } = useFormContext();
6910
- const { selected = {} } = getValues();
6911
- const isFilterValidated = validateProductFilter(product, stateFilter);
6920
+ const { selected = {}, filter = {} } = getValues();
6921
+ const isFilterValidated = validateProductFilter(product, filter);
6912
6922
  const isOnSubmitted = useStore((state) => state.isOnSubmitted);
6913
6923
  const setValidationMap = useStore((state) => state.setValidationMap);
6914
6924
  const [selCnt, qtyCnt] = getSelCtxCntFromSelectedState(
@@ -6927,13 +6937,16 @@ function useMenuOptions(product, nestedLvl, parentKind, parentSelCtx) {
6927
6937
  parentKind
6928
6938
  });
6929
6939
  useEffect(() => {
6940
+ console.log("isFilterValidated useEffect");
6930
6941
  const { productsToSelect, productsToUnselect } = getProductToChangeInForm(
6931
6942
  product,
6932
6943
  isFilterValidated
6933
6944
  );
6945
+ console.log({ productsToSelect });
6946
+ console.log({ productsToUnselect });
6934
6947
  unselectProductsInForm(productsToUnselect, setValue);
6935
6948
  selectProductsInForm(productsToSelect, setValue, initialSelected);
6936
- }, [stateFilter]);
6949
+ }, [isFilterValidated, initialSelected]);
6937
6950
  return {
6938
6951
  isFilterValidated,
6939
6952
  selected,
@@ -8794,14 +8807,63 @@ function Counter(props) {
8794
8807
  })]
8795
8808
  });
8796
8809
  }
8797
- function useEmitFilter(product) {
8798
- const { watch } = useFormContext();
8799
- const setFilter = useStore((state) => state.setFilter);
8810
+ function useEmitFilter(product, isRadio, nestedLvl) {
8811
+ const { watch, setValue, getValues, unregister } = useFormContext();
8800
8812
  const { id, selCtx, emit = {}, fltCtx } = product;
8801
8813
  const value = watch(`selected.${selCtx}.${id}`);
8802
8814
  useEffect(() => {
8815
+ var _a, _b, _c;
8816
+ if (!nestedLvl || nestedLvl > 1) {
8817
+ return;
8818
+ }
8819
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8820
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8821
+ const fltCtxPath = `filter.${fltCtx}`;
8822
+ if (isRadio && value && fltCtx) {
8823
+ if (isEmpty(emit)) {
8824
+ const groupValues = getValues(groupCntPath);
8825
+ (_a = Object.keys(groupValues || {})) == null ? void 0 : _a.forEach((x2) => {
8826
+ unregister(`${groupCntPath}.${x2}`);
8827
+ const groupsKeys = Object.keys(getValues(groupsPath) || {});
8828
+ if (isEmpty(groupsKeys)) {
8829
+ unregister(`${fltCtxPath}.${x2}`);
8830
+ } else {
8831
+ groupsKeys.forEach((groupSelCtx) => {
8832
+ if (groupSelCtx !== selCtx && !getValues(`${groupsPath}.${groupSelCtx}.${x2}`)) {
8833
+ unregister(`${fltCtxPath}.${x2}`);
8834
+ }
8835
+ });
8836
+ }
8837
+ });
8838
+ return;
8839
+ } else {
8840
+ const productGroupCntKeys = getValues(groupCntPath);
8841
+ const groupsCntKeys = getValues(groupsPath);
8842
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((x2) => {
8843
+ var _a2;
8844
+ (_a2 = Object.keys(productGroupCntKeys || {})) == null ? void 0 : _a2.forEach((productGroupCntKey) => {
8845
+ if (!emit[productGroupCntKey]) {
8846
+ unregister(`${fltCtxPath}.${productGroupCntKey}`);
8847
+ unregister(`${groupCntPath}.${productGroupCntKey}`);
8848
+ }
8849
+ });
8850
+ Object.keys(groupsCntKeys || {}).forEach((groupSelCtx) => {
8851
+ if (groupSelCtx !== selCtx && x2 in getValues(groupsPath == null ? void 0 : groupsPath[groupSelCtx])) {
8852
+ unregister(`${groupsPath}.${groupSelCtx}.${x2}`);
8853
+ }
8854
+ });
8855
+ setValue(`${fltCtxPath}.${x2}`, emit[x2]);
8856
+ setValue(`${groupCntPath}.${x2}`, { cnt: 1, value: emit[x2] });
8857
+ });
8858
+ return;
8859
+ }
8860
+ }
8803
8861
  if (value && fltCtx && !isEmpty(emit)) {
8804
- setFilter(fltCtx, emit);
8862
+ (_c = Object.keys(emit)) == null ? void 0 : _c.forEach((x2) => {
8863
+ setValue(`filter.${fltCtx}.${x2}`, emit[x2]);
8864
+ const groupKeyCnt = getValues(`${groupCntPath}.${x2}.cnt`) || 0;
8865
+ setValue(`${groupCntPath}.${x2}`, { cnt: groupKeyCnt + 1, value: emit[x2] });
8866
+ });
8805
8867
  }
8806
8868
  }, [value]);
8807
8869
  return {};
@@ -8833,62 +8895,105 @@ const removeProductFromState = (selCtx, getValues, setValue) => {
8833
8895
  }, {})
8834
8896
  );
8835
8897
  };
8836
- const selectUnselectProduct = (setValue, getValues, items, isChecked, state) => {
8898
+ const selectUnselectProduct = (setValue, getValues, product, isChecked, unregister, nestedLvl) => {
8899
+ var _a, _b;
8900
+ const { items, state, emit, fltCtx, selCtx } = product;
8837
8901
  const { selected = {} } = state || {};
8902
+ console.log("selected/unselected product", product, isChecked);
8838
8903
  if (isChecked) {
8839
8904
  addProductToState(selected, setValue);
8840
8905
  const checkSubProducts = (items2) => {
8841
- var _a;
8906
+ var _a2;
8842
8907
  if (!Array.isArray(items2) || isEmpty(state == null ? void 0 : state.selected))
8843
8908
  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) {
8909
+ (_a2 = items2 == null ? void 0 : items2.filter((x2) => {
8910
+ var _a3, _b2;
8911
+ return !!((_b2 = (_a3 = state.selected) == null ? void 0 : _a3[`${x2.selCtx}`]) == null ? void 0 : _b2[`${x2.id}`]);
8912
+ })) == null ? void 0 : _a2.map((product2) => {
8913
+ if (product2.state && "selected" in product2.state) {
8849
8914
  const {
8850
8915
  state: { selected: selected2 }
8851
- } = product;
8916
+ } = product2;
8852
8917
  addProductToState(selected2, setValue);
8853
8918
  }
8854
- if (Array.isArray(product.items)) {
8855
- checkSubProducts(product.items);
8919
+ if (Array.isArray(product2.items)) {
8920
+ checkSubProducts(product2.items);
8856
8921
  }
8857
8922
  });
8858
8923
  };
8859
8924
  checkSubProducts(items);
8860
8925
  } else {
8861
- Object.keys(selected).forEach((selCtx) => {
8862
- removeProductFromState(selCtx, getValues, setValue);
8926
+ Object.keys(selected).forEach((selCtx2) => {
8927
+ removeProductFromState(selCtx2, getValues, setValue);
8863
8928
  });
8864
8929
  const checkSubProducts = (items2) => {
8865
8930
  if (!Array.isArray(items2))
8866
8931
  return;
8867
- items2.map((product) => {
8868
- const { selCtx } = product;
8869
- const changedSelCtx = getValues(`selected.${selCtx}`);
8932
+ items2.map((product2) => {
8933
+ const { selCtx: selCtx2 } = product2;
8934
+ const changedSelCtx = getValues(`selected.${selCtx2}`);
8870
8935
  if (changedSelCtx) {
8871
- removeProductFromState(selCtx, getValues, setValue);
8936
+ removeProductFromState(selCtx2, getValues, setValue);
8872
8937
  }
8873
- checkSubProducts(product.items);
8938
+ checkSubProducts(product2.items);
8874
8939
  });
8875
8940
  };
8876
8941
  checkSubProducts(items);
8942
+ (_a = Object.keys(emit)) == null ? void 0 : _a.forEach((x2) => {
8943
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
8944
+ const cnt = getValues(formPath) || 0;
8945
+ if (cnt < 2) {
8946
+ unregister(formPath);
8947
+ } else {
8948
+ setValue(formPath, cnt - 1);
8949
+ }
8950
+ });
8951
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
8952
+ const groupsPath = `filterWithCnt.${fltCtx}`;
8953
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
8954
+ const fltCtxPath = `filter.${fltCtx}`;
8955
+ (_b = Object.keys(product.emit)) == null ? void 0 : _b.forEach((emitKey) => {
8956
+ var _a2, _b2, _c;
8957
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
8958
+ if (groupValue) {
8959
+ if (groupValue > 1) {
8960
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
8961
+ } else {
8962
+ unregister(`${groupCntPath}.${emitKey}`);
8963
+ const groupKeysObj = getValues(groupsPath) || {};
8964
+ const otherGroupsCntKeys = (_a2 = Object.keys(groupKeysObj)) == null ? void 0 : _a2.filter((x2) => x2 !== selCtx);
8965
+ if (!otherGroupsCntKeys) {
8966
+ unregister(`${fltCtxPath}.${emitKey}`);
8967
+ } else {
8968
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
8969
+ var _a3;
8970
+ return !!((_a3 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a3[`${emitKey}`]);
8971
+ });
8972
+ if (!sameKeyOtherGroup) {
8973
+ unregister(`${fltCtxPath}.${emitKey}`);
8974
+ } else {
8975
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
8976
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
8977
+ }
8978
+ }
8979
+ }
8980
+ }
8981
+ });
8982
+ }
8877
8983
  }
8878
8984
  };
8879
- function useMenuProduct(product, parentAttrs) {
8985
+ function useMenuProduct(product, parentAttrs, nestedLvl) {
8880
8986
  var _a;
8881
- const { id, selCtx } = product;
8987
+ const { id, selCtx, emit = {}, fltCtx } = product;
8882
8988
  const {
8883
8989
  allowMultiple: parentAllowMultiple,
8884
8990
  selMin: parentSelMin,
8885
8991
  selMax: parentSelMax
8886
8992
  } = parentAttrs;
8887
- useEmitFilter(product);
8888
8993
  const { isInputEnabled: isInputEnabled2, isInputCounterIncrementEnabled: isInputCounterIncrementEnabled2 } = useValidate();
8889
8994
  const refreshForm = useStore((state) => state.refreshForm);
8890
8995
  const currency = useStore((state) => state.currency);
8891
- const { watch, getValues, setValue, register } = useFormContext();
8996
+ const { watch, getValues, setValue, register, unregister } = useFormContext();
8892
8997
  const value = watch(`selected.${selCtx}.${id}`);
8893
8998
  const { selected = {} } = getValues();
8894
8999
  const valueNumber = Number(value || 0);
@@ -8896,7 +9001,8 @@ function useMenuProduct(product, parentAttrs) {
8896
9001
  const isCheckbox = !isRadio;
8897
9002
  const isCounter = isApiBooleanTrue(parentAllowMultiple);
8898
9003
  const isDecrementEnabled = valueNumber > (isRadio ? 1 : 0);
8899
- const isIncrementEnabled = isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
9004
+ const isIncrementEnabled = useEmitFilter(product, isRadio, nestedLvl);
9005
+ isInputCounterIncrementEnabled2(product, selected) && (isInputEnabled2(product) || valueNumber);
8900
9006
  const isChecked = Number(((_a = selected == null ? void 0 : selected[selCtx]) == null ? void 0 : _a[id]) || 0) > 0;
8901
9007
  const isCheckboxEnabled = isInputEnabled2(product) || valueNumber;
8902
9008
  const registerId = `${isRadio ? "radio" : "selected"}.${selCtx}.${id}`;
@@ -8909,10 +9015,20 @@ function useMenuProduct(product, parentAttrs) {
8909
9015
  setValue(`selected.${selCtx}.${id}`, `${valueNumber + 1}`);
8910
9016
  };
8911
9017
  const handleCounterDecrementQty = () => {
9018
+ var _a2, _b;
8912
9019
  const decrementedValue = valueNumber - 1;
8913
9020
  setValue(`selected.${selCtx}.${id}`, `${decrementedValue}`);
8914
9021
  if (isCheckbox && decrementedValue === 0) {
8915
9022
  setValue(`selected.${selCtx}.${id}`, false);
9023
+ (_a2 = Object.keys(emit)) == null ? void 0 : _a2.forEach((x2) => {
9024
+ const formPath = `filterWithCnt.${fltCtx}.${x2}.${emit[x2]}`;
9025
+ const cnt = getValues(formPath) || 0;
9026
+ if (cnt < 2) {
9027
+ unregister(formPath);
9028
+ } else {
9029
+ setValue(formPath, cnt - 1);
9030
+ }
9031
+ });
8916
9032
  const checkSubProducts = (items) => {
8917
9033
  if (!Array.isArray(items))
8918
9034
  return;
@@ -8926,18 +9042,45 @@ function useMenuProduct(product, parentAttrs) {
8926
9042
  });
8927
9043
  };
8928
9044
  checkSubProducts(product.items);
9045
+ if (nestedLvl && nestedLvl < 2 && !isEmpty(emit)) {
9046
+ const groupsPath = `filterWithCnt.${fltCtx}`;
9047
+ const groupCntPath = `filterWithCnt.${fltCtx}.${selCtx}`;
9048
+ const fltCtxPath = `filter.${fltCtx}`;
9049
+ (_b = Object.keys(emit)) == null ? void 0 : _b.forEach((emitKey) => {
9050
+ var _a3, _b2, _c;
9051
+ const groupValue = getValues(`${groupCntPath}.${emitKey}`) || 0;
9052
+ if (groupValue) {
9053
+ if (groupValue > 1) {
9054
+ setValue(`${groupCntPath}.${emitKey}`, groupValue - 1);
9055
+ } else {
9056
+ unregister(`${groupCntPath}.${emitKey}`);
9057
+ const groupKeysObj = getValues(groupsPath) || {};
9058
+ const otherGroupsCntKeys = (_a3 = Object.keys(groupKeysObj)) == null ? void 0 : _a3.filter((x2) => x2 !== selCtx);
9059
+ if (!otherGroupsCntKeys) {
9060
+ unregister(`${fltCtxPath}.${emitKey}`);
9061
+ } else {
9062
+ const sameKeyOtherGroup = otherGroupsCntKeys == null ? void 0 : otherGroupsCntKeys.find((groupSelCtx) => {
9063
+ var _a4;
9064
+ return !!((_a4 = groupKeysObj == null ? void 0 : groupKeysObj[`${groupSelCtx}`]) == null ? void 0 : _a4[`${emitKey}`]);
9065
+ });
9066
+ if (!sameKeyOtherGroup) {
9067
+ unregister(`${fltCtxPath}.${emitKey}`);
9068
+ } else {
9069
+ const newValue = (_c = (_b2 = groupKeysObj == null ? void 0 : groupKeysObj[`${sameKeyOtherGroup}`]) == null ? void 0 : _b2[`${emitKey}`]) == null ? void 0 : _c.value;
9070
+ setValue(`${fltCtxPath}.${emitKey}`, newValue);
9071
+ }
9072
+ }
9073
+ }
9074
+ }
9075
+ });
9076
+ }
8929
9077
  }
8930
9078
  };
8931
9079
  const handleOnInputChange = (e2) => {
8932
9080
  const {
8933
9081
  target: { checked, name: selCtx2, defaultValue: productId }
8934
9082
  } = 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);
9083
+ selectUnselectProduct(setValue, getValues, product, checked, unregister, nestedLvl);
8941
9084
  if (isRadio) {
8942
9085
  console.log("handleOnInputChange radio", selCtx2, productId);
8943
9086
  const selectedContextName = "selected." + selCtx2.replace("radio.", "").split(".")[0];
@@ -8986,7 +9129,7 @@ const MenuProduct = (props) => {
8986
9129
  handleCounterIncrementQty,
8987
9130
  handleCounterDecrementQty,
8988
9131
  currency
8989
- } = useMenuProduct(product, parentAttrs);
9132
+ } = useMenuProduct(product, parentAttrs, nestedLvl);
8990
9133
  const {
8991
9134
  MenuProductUI
8992
9135
  } = useComponents();
@@ -9222,10 +9365,14 @@ const DisplayState = () => {
9222
9365
  watch
9223
9366
  } = useFormContext();
9224
9367
  const selected = watch("selected");
9368
+ const filter = watch("filter");
9369
+ const filterWithCnt = watch("filterWithCnt");
9225
9370
  const validationMap = useStore((state) => state.validationMap);
9226
9371
  console.log({
9227
9372
  selected,
9228
- validationMap
9373
+ validationMap,
9374
+ filter,
9375
+ filterWithCnt
9229
9376
  });
9230
9377
  return /* @__PURE__ */ jsxs("div", {
9231
9378
  className: "flex flex-col items-center py-4",
@@ -10902,6 +11049,7 @@ function ProductConfigurator(props) {
10902
11049
  setValidationMap(validationMapNew);
10903
11050
  }
10904
11051
  });
11052
+ console.log("ProductConfigurator initSelected", initSelected);
10905
11053
  const methods = useForm({
10906
11054
  defaultValues: {
10907
11055
  selected: initSelected
@@ -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