thm-p3-configurator 0.0.341 → 0.0.342

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.
@@ -48,6 +48,8 @@ const TextInput = _ref => {
48
48
  value,
49
49
  name,
50
50
  onClick = () => {},
51
+ onBlur = () => {},
52
+ onKeyDown = () => {},
51
53
  isDisabled = false,
52
54
  onChange = () => {},
53
55
  isRequired = true,
@@ -82,6 +84,8 @@ const TextInput = _ref => {
82
84
  value: displayValue,
83
85
  disabled: isDisabled,
84
86
  onChange: e => handleChange(e.target.value),
87
+ onBlur: onBlur,
88
+ onKeyDown: onKeyDown,
85
89
  required: isRequired,
86
90
  className: "".concat((0, _helpers__.withStyle)('form-control'), " ").concat(errorMessage ? (0, _helpers__.withStyle)('is-invalid') : '')
87
91
  }), errorMessage && /*#__PURE__*/_react.default.createElement(_ErrorMessage.default, {
@@ -142,6 +142,66 @@ function _toPrimitive(t, r) {
142
142
  }
143
143
  return ("string" === r ? String : Number)(t);
144
144
  }
145
+ // ============================================================================
146
+ // INPUT HELPERS
147
+ // ============================================================================
148
+
149
+ const sanitizeDiscountDraft = value => {
150
+ var _value$toString$repla;
151
+ return (_value$toString$repla = value === null || value === void 0 ? void 0 : value.toString().replace(/[^0-9]/g, '')) !== null && _value$toString$repla !== void 0 ? _value$toString$repla : '';
152
+ };
153
+ const parseDiscountValue = value => {
154
+ const sanitized = sanitizeDiscountDraft(value);
155
+ if (sanitized === '') {
156
+ return null;
157
+ }
158
+ return Math.min(100, Math.max(0, parseInt(sanitized, 10)));
159
+ };
160
+ const formatDiscountDraft = value => value === null || value === undefined ? '' : value.toString();
161
+ const DeferredDiscountInput = _ref => {
162
+ let {
163
+ articleNumber,
164
+ value,
165
+ onCommit
166
+ } = _ref;
167
+ const [draftValue, setDraftValue] = (0, _react.useState)(formatDiscountDraft(value));
168
+ const isEditingRef = (0, _react.useRef)(false);
169
+ (0, _react.useEffect)(() => {
170
+ if (!isEditingRef.current) {
171
+ setDraftValue(formatDiscountDraft(value));
172
+ }
173
+ }, [value]);
174
+ const handleBlur = () => {
175
+ isEditingRef.current = false;
176
+ const nextValue = parseDiscountValue(draftValue);
177
+ const currentValue = value !== null && value !== void 0 ? value : null;
178
+ setDraftValue(formatDiscountDraft(nextValue));
179
+ if (nextValue === currentValue) {
180
+ return;
181
+ }
182
+ onCommit(nextValue);
183
+ };
184
+ return /*#__PURE__*/_react.default.createElement("div", {
185
+ className: (0, _helpers__.withStyle)('d-flex align-items-center justify-content-end')
186
+ }, /*#__PURE__*/_react.default.createElement(_TextInput.default, {
187
+ className: "mb-0",
188
+ name: "consumer-discount-".concat(articleNumber),
189
+ placeholder: "0-100",
190
+ value: draftValue,
191
+ isRequired: false,
192
+ onChange: nextValue => {
193
+ isEditingRef.current = true;
194
+ setDraftValue(sanitizeDiscountDraft(nextValue));
195
+ },
196
+ onBlur: handleBlur,
197
+ onKeyDown: event => {
198
+ if (event.key === 'Enter') {
199
+ event.currentTarget.blur();
200
+ }
201
+ }
202
+ }));
203
+ };
204
+
145
205
  // ============================================================================
146
206
  // MAIN COMPONENT
147
207
  // ============================================================================
@@ -153,8 +213,6 @@ const ProductCartTable = () => {
153
213
  const isTmg = (0, _useIsTmg.useIsTmg)();
154
214
  const isTowmotive = (0, _useIsTowmotive.useIsTowmotive)();
155
215
  const [isToggled, setIsToggled] = (0, _react.useState)(false);
156
- const [localDiscounts, setLocalDiscounts] = (0, _react.useState)({});
157
- const discountTimersRef = (0, _react.useRef)({});
158
216
  const [{
159
217
  licensePlate,
160
218
  model,
@@ -184,72 +242,26 @@ const ProductCartTable = () => {
184
242
  const canEditDiscounts = isTowmotive || isTowFormula;
185
243
  const useTmgDisplayMode = isTmg && !isTowFormula || isThc && !canEditDiscounts;
186
244
  const displayMode = (0, _cartTable2.getCartTableDisplayMode)(isToggled, useTmgDisplayMode);
187
- const DISCOUNT_DEBOUNCE_MS = 600;
188
- const handleVehicleCodingDiscountChange = function handleVehicleCodingDiscountChange(value) {
189
- let articleNumber = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
190
- const sanitized = value === null || value === void 0 ? void 0 : value.toString().replace(/[^0-9]/g, '');
191
- const parsed = sanitized === '' ? null : Math.min(100, Math.max(0, parseInt(sanitized, 10)));
192
- const key = articleNumber || '__vehicleCoding';
193
- setLocalDiscounts(prev => _objectSpread(_objectSpread({}, prev), {}, {
194
- [key]: parsed
195
- }));
196
- clearTimeout(discountTimersRef.current[key]);
197
- discountTimersRef.current[key] = setTimeout(() => {
198
- dispatch({
199
- type: _OrderSessionContext.orderSessionActions.SET_VEHICLE_CODING_DISCOUNT_PERCENTAGE,
200
- payload: {
201
- percentage: parsed
202
- }
203
- });
204
- if (articleNumber) {
205
- dispatch({
206
- type: _OrderSessionContext.orderSessionActions.SET_CONSUMER_DISCOUNT_PERCENTAGE,
207
- payload: {
208
- articleNumber,
209
- percentage: parsed
210
- }
211
- });
212
- }
213
- setLocalDiscounts(prev => {
214
- const next = _objectSpread({}, prev);
215
- delete next[key];
216
- return next;
217
- });
218
- }, DISCOUNT_DEBOUNCE_MS);
219
- };
220
- const handleLineDiscountChange = function handleLineDiscountChange(articleNumber, value) {
245
+ const handleLineDiscountCommit = function handleLineDiscountCommit(articleNumber, percentage) {
221
246
  let {
222
247
  isVehicleCoding = false
223
248
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
224
- const sanitized = value === null || value === void 0 ? void 0 : value.toString().replace(/[^0-9]/g, '');
225
- const parsed = sanitized === '' ? null : Math.min(100, Math.max(0, parseInt(sanitized, 10)));
226
- setLocalDiscounts(prev => _objectSpread(_objectSpread({}, prev), {}, {
227
- [articleNumber]: parsed
228
- }));
229
- clearTimeout(discountTimersRef.current[articleNumber]);
230
- discountTimersRef.current[articleNumber] = setTimeout(() => {
249
+ dispatch({
250
+ type: _OrderSessionContext.orderSessionActions.SET_CONSUMER_DISCOUNT_PERCENTAGE,
251
+ payload: {
252
+ articleNumber,
253
+ percentage,
254
+ userModified: true
255
+ }
256
+ });
257
+ if (isVehicleCoding) {
231
258
  dispatch({
232
- type: _OrderSessionContext.orderSessionActions.SET_CONSUMER_DISCOUNT_PERCENTAGE,
259
+ type: _OrderSessionContext.orderSessionActions.SET_VEHICLE_CODING_DISCOUNT_PERCENTAGE,
233
260
  payload: {
234
- articleNumber,
235
- percentage: parsed,
236
- userModified: true
261
+ percentage
237
262
  }
238
263
  });
239
- if (isVehicleCoding) {
240
- dispatch({
241
- type: _OrderSessionContext.orderSessionActions.SET_VEHICLE_CODING_DISCOUNT_PERCENTAGE,
242
- payload: {
243
- percentage: parsed
244
- }
245
- });
246
- }
247
- setLocalDiscounts(prev => {
248
- const next = _objectSpread({}, prev);
249
- delete next[articleNumber];
250
- return next;
251
- });
252
- }, DISCOUNT_DEBOUNCE_MS);
264
+ }
253
265
  };
254
266
  const handleMontageRateChange = value => {
255
267
  if (value === null || value === undefined) {
@@ -593,16 +605,11 @@ const ProductCartTable = () => {
593
605
  // RENDER HELPERS
594
606
  // ============================================================================
595
607
 
596
- const renderDiscountInput = (articleNumber, value, onChange) => /*#__PURE__*/_react.default.createElement("div", {
597
- className: (0, _helpers__.withStyle)('d-flex align-items-center justify-content-end')
598
- }, /*#__PURE__*/_react.default.createElement(_TextInput.default, {
599
- className: "mb-0",
600
- name: "consumer-discount-".concat(articleNumber),
601
- placeholder: "0-100",
602
- value: value === null || value === undefined ? '' : value.toString(),
603
- isRequired: false,
604
- onChange: onChange
605
- }));
608
+ const renderDiscountInput = (articleNumber, value, onCommit) => /*#__PURE__*/_react.default.createElement(DeferredDiscountInput, {
609
+ articleNumber: articleNumber,
610
+ value: value,
611
+ onCommit: onCommit
612
+ });
606
613
  const renderVehicleCodingInput = articleNumber => /*#__PURE__*/_react.default.createElement("div", {
607
614
  className: (0, _helpers__.withStyle)('text-end')
608
615
  }, /*#__PURE__*/_react.default.createElement("div", {
@@ -663,10 +670,10 @@ const ProductCartTable = () => {
663
670
  const isVehicleCodingProduct = (product === null || product === void 0 ? void 0 : product.subgroupName) === _constants__.VEHICLE_CODING_SUBGROUP_NAME || (product === null || product === void 0 ? void 0 : product.groupName) === _constants__.VEHICLE_CODING_SUBGROUP_NAME;
664
671
  const isBoardComputerProduct = selectedBoardComputer && String(product.articleNumber) === String(selectedBoardComputer);
665
672
  const stateDiscountValue = (_product$consumerDisc = product === null || product === void 0 ? void 0 : product.consumerDiscountPercentage) !== null && _product$consumerDisc !== void 0 ? _product$consumerDisc : isVehicleCodingProduct ? vehicleCodingDiscountPercentage || null : null;
666
- const lineDiscountValue = localDiscounts[product.articleNumber] !== undefined ? localDiscounts[product.articleNumber] : stateDiscountValue;
673
+ const lineDiscountValue = stateDiscountValue;
667
674
  const allowLineDiscountMain = displayMode === _cartTable.CART_TABLE_DISPLAY_MODES.TOGGLED_STANDARD && (isTowmotive || isTowFormula);
668
675
  const discountInputCellMain = allowLineDiscountMain ? {
669
- text: renderDiscountInput(product.articleNumber, lineDiscountValue, v => handleLineDiscountChange(product.articleNumber, v, {
676
+ text: renderDiscountInput(product.articleNumber, lineDiscountValue, percentage => handleLineDiscountCommit(product.articleNumber, percentage, {
670
677
  isVehicleCoding: isVehicleCodingProduct
671
678
  })),
672
679
  className: _cartTable.CART_TABLE_PRICE_CELL_CLASS
@@ -717,10 +724,10 @@ const ProductCartTable = () => {
717
724
  heading: "Toebehoren"
718
725
  }, accessoireProducts.map(product => {
719
726
  var _product$consumerDisc2, _product$images3, _product$images4;
720
- const lineDiscountValue = localDiscounts[product.articleNumber] !== undefined ? localDiscounts[product.articleNumber] : (_product$consumerDisc2 = product === null || product === void 0 ? void 0 : product.consumerDiscountPercentage) !== null && _product$consumerDisc2 !== void 0 ? _product$consumerDisc2 : null;
727
+ const lineDiscountValue = (_product$consumerDisc2 = product === null || product === void 0 ? void 0 : product.consumerDiscountPercentage) !== null && _product$consumerDisc2 !== void 0 ? _product$consumerDisc2 : null;
721
728
  const allowLineDiscountAccessory = displayMode === _cartTable.CART_TABLE_DISPLAY_MODES.TOGGLED_STANDARD && (isTowmotive || isTowFormula);
722
729
  const discountInputCellAccessory = allowLineDiscountAccessory ? {
723
- text: renderDiscountInput(product.articleNumber, lineDiscountValue, v => handleLineDiscountChange(product.articleNumber, v)),
730
+ text: renderDiscountInput(product.articleNumber, lineDiscountValue, percentage => handleLineDiscountCommit(product.articleNumber, percentage)),
724
731
  className: _cartTable.CART_TABLE_PRICE_CELL_CLASS
725
732
  } : {};
726
733
  let canProductBeAltered = canAlterQuantity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thm-p3-configurator",
3
- "version": "0.0.341",
3
+ "version": "0.0.342",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": "EnoRm.",