@orderingstack/front-components-product-configurator 1.4.2-beta.1 → 1.4.2-beta.11

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