@alma/widgets 2.2.10 → 2.3.0

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.
@@ -6134,7 +6134,7 @@
6134
6134
  "payment-plan-strings.deferred": "{totalAmount} zu zahlen am {dueDate}",
6135
6135
  "payment-plan-strings.ineligible-greater-than-max": "Bis zu {maxAmount}",
6136
6136
  "payment-plan-strings.ineligible-lower-than-min": "Ab {minAmount}",
6137
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}",
6137
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
6138
6138
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
6139
6139
  "payment-plan-strings.no-fee": "(gebührenfrei)"
6140
6140
  };
@@ -6156,7 +6156,7 @@
6156
6156
  "payment-plan-strings.deferred": "{totalAmount} to be paid on {dueDate}",
6157
6157
  "payment-plan-strings.ineligible-greater-than-max": "Until {maxAmount}",
6158
6158
  "payment-plan-strings.ineligible-lower-than-min": "From {minAmount}",
6159
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}",
6159
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
6160
6160
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
6161
6161
  "payment-plan-strings.no-fee": "(free of charge)"
6162
6162
  };
@@ -6178,7 +6178,7 @@
6178
6178
  "payment-plan-strings.deferred": "{totalAmount} a pagar el {dueDate}",
6179
6179
  "payment-plan-strings.ineligible-greater-than-max": "Hasta {maxAmount}",
6180
6180
  "payment-plan-strings.ineligible-lower-than-min": "Desde {minAmount}",
6181
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} hoy, después {numberOfRemainingInstallments, plural, one {# mensualidad} other {# mensualidades}} de {othersInstallmentAmount}",
6181
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} hoy, después {numberOfRemainingInstallments} mensualidad de {othersInstallmentAmount}} other {{firstInstallmentAmount} hoy, después {numberOfRemainingInstallments} de {othersInstallmentAmount} mensualidades }}",
6182
6182
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} plazos de {totalAmount}",
6183
6183
  "payment-plan-strings.no-fee": "(sin intereses)"
6184
6184
  };
@@ -6200,7 +6200,7 @@
6200
6200
  "payment-plan-strings.deferred": "{totalAmount} à payer le {dueDate}",
6201
6201
  "payment-plan-strings.ineligible-greater-than-max": "Jusqu'à {maxAmount}",
6202
6202
  "payment-plan-strings.ineligible-lower-than-min": "À partir de {minAmount}",
6203
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}",
6203
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
6204
6204
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
6205
6205
  "payment-plan-strings.no-fee": "(sans frais)"
6206
6206
  };
@@ -6222,7 +6222,7 @@
6222
6222
  "payment-plan-strings.deferred": "{totalAmount} da pagare il {dueDate}",
6223
6223
  "payment-plan-strings.ineligible-greater-than-max": "Disponibile fino a {maxAmount}",
6224
6224
  "payment-plan-strings.ineligible-lower-than-min": "Pagamento rateale disponibile a partire da {minAmount}",
6225
- "payment-plan-strings.multiple-installments": "Oggi paghi {firstInstallmentAmount} poi {numberOfRemainingInstallments} rate mensili di {othersInstallmentAmount}",
6225
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {Oggi paghi {firstInstallmentAmount} poi {numberOfRemainingInstallments} rata mensile di {othersInstallmentAmount}} other {Oggi paghi {firstInstallmentAmount} poi {numberOfRemainingInstallments} rate mensili di {othersInstallmentAmount}}}",
6226
6226
  "payment-plan-strings.multiple-installments-same-amount": "In {installmentsCount} rate mensili di {totalAmount}",
6227
6227
  "payment-plan-strings.no-fee": "(senza interessi)"
6228
6228
  };
@@ -6244,7 +6244,7 @@
6244
6244
  "payment-plan-strings.deferred": "{totalAmount} te betalen op {dueDate}",
6245
6245
  "payment-plan-strings.ineligible-greater-than-max": "Tot {maxAmount}",
6246
6246
  "payment-plan-strings.ineligible-lower-than-min": "Van {minAmount}",
6247
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}",
6247
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
6248
6248
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
6249
6249
  "payment-plan-strings.no-fee": "(gratis)"
6250
6250
  };
@@ -14359,8 +14359,8 @@
14359
14359
  }, {
14360
14360
  Authorization: "Alma-Merchant-Auth " + merchantId
14361
14361
  }).then(function (res) {
14362
- setStatus(apiStatus.SUCCESS);
14363
14362
  setEligibility(res);
14363
+ setStatus(apiStatus.SUCCESS);
14364
14364
  })["catch"](function () {
14365
14365
  setStatus(apiStatus.FAILED);
14366
14366
  });
@@ -17846,7 +17846,7 @@
17846
17846
 
17847
17847
  return /*#__PURE__*/react.createElement(react.Fragment, null, /*#__PURE__*/react.createElement(MemoizedFormattedMessage, {
17848
17848
  id: "payment-plan-strings.multiple-installments",
17849
- defaultMessage: "{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}",
17849
+ defaultMessage: "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
17850
17850
  values: {
17851
17851
  firstInstallmentAmount: /*#__PURE__*/react.createElement(FormattedNumber, {
17852
17852
  value: priceFromCents(payment.payment_plan[0].total_amount),
@@ -18212,16 +18212,54 @@
18212
18212
  };
18213
18213
  };
18214
18214
 
18215
- var s$b = {"widgetButton":"_TSkFv","logo":"_LJ4nZ","primaryContainer":"_bMClc","paymentPlans":"_17c_S","plan":"_2Kqjn","active":"_3dG_J","notEligible":"_3O1bg","info":"_25GrF","loader":"_30j1O","error":"_R0YlN","errorText":"_2kGhu","errorButton":"_73d_Y","pending":"_1ZDMS"};
18215
+ /**
18216
+ * It returns the **index** of the **first eligible plan** that matches the default installments count
18217
+ *
18218
+ * @param {number | number[]} suggestedPaymentPlan
18219
+ * @param {EligibilityPlan[]} eligibilityPlans
18220
+ * @returns number (index of the first eligible plan that matches the default installments count)
18221
+ */
18222
+ var getIndexOfActivePlan = function getIndexOfActivePlan(_ref) {
18223
+ var suggestedPaymentPlan = _ref.suggestedPaymentPlan,
18224
+ eligibilityPlans = _ref.eligibilityPlans;
18225
+ var suggestedPaymentPlanArray = Array.isArray(suggestedPaymentPlan) ? suggestedPaymentPlan : [suggestedPaymentPlan];
18226
+
18227
+ var _loop = function _loop(index) {
18228
+ var installmentsCount = suggestedPaymentPlanArray[index];
18229
+ var planFound = eligibilityPlans.findIndex(function (plan) {
18230
+ return plan.installments_count === installmentsCount && plan.eligible;
18231
+ });
18232
+
18233
+ if (planFound !== -1) {
18234
+ return {
18235
+ v: planFound
18236
+ };
18237
+ }
18238
+ };
18239
+
18240
+ for (var index in suggestedPaymentPlanArray) {
18241
+ var _ret = _loop(index);
18242
+
18243
+ if (typeof _ret === "object") return _ret.v;
18244
+ }
18245
+
18246
+ return 0;
18247
+ };
18248
+
18249
+ var s$b = {"widgetButton":"_TSkFv","logo":"_LJ4nZ","primaryContainer":"_bMClc","paymentPlans":"_17c_S","plan":"_2Kqjn","active":"_3dG_J","notEligible":"_3O1bg","info":"_25GrF","loader":"_30j1O","error":"_R0YlN","errorText":"_2kGhu","errorButton":"_73d_Y","pending":"_1ZDMS","clickable":"_UksZa","unClickable":"_1lr-q"};
18250
+
18251
+ var VERY_LONG_TIME_IN_MS = 1000 * 3600 * 24 * 365;
18252
+ var DEFAULT_TRANSITION_TIME = 5500;
18216
18253
 
18217
18254
  var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
18218
- var _cx2;
18255
+ var _cx, _cx3;
18219
18256
 
18220
18257
  var purchaseAmount = _ref.purchaseAmount,
18221
18258
  apiData = _ref.apiData,
18222
18259
  configPlans = _ref.configPlans,
18223
18260
  transitionDelay = _ref.transitionDelay,
18224
- hideIfNotEligible = _ref.hideIfNotEligible;
18261
+ hideIfNotEligible = _ref.hideIfNotEligible,
18262
+ suggestedPaymentPlan = _ref.suggestedPaymentPlan;
18225
18263
 
18226
18264
  var _useFetchEligibility = useFetchEligibility(purchaseAmount, apiData, configPlans),
18227
18265
  eligibilityPlans = _useFetchEligibility[0],
@@ -18230,15 +18268,18 @@
18230
18268
  var eligiblePlans = eligibilityPlans.filter(function (plan) {
18231
18269
  return plan.eligible;
18232
18270
  });
18271
+ var activePlanIndex = getIndexOfActivePlan({
18272
+ eligibilityPlans: eligibilityPlans,
18273
+ suggestedPaymentPlan: suggestedPaymentPlan != null ? suggestedPaymentPlan : 0
18274
+ });
18275
+ var isSuggestedPaymentPlanSpecified = suggestedPaymentPlan !== undefined; // 👈 The merchant decided to focus a tab and remove animated transition.
18276
+
18277
+ var isTransitionSpecified = transitionDelay !== undefined; // 👈 The merchant has specified a transition time
18233
18278
 
18234
18279
  var _useState = react.useState(false),
18235
18280
  isOpen = _useState[0],
18236
18281
  setIsOpen = _useState[1];
18237
18282
 
18238
- var _useState2 = react.useState(0),
18239
- initialPlanIndex = _useState2[0],
18240
- setInitialPlanIndex = _useState2[1];
18241
-
18242
18283
  var openModal = function openModal() {
18243
18284
  return setIsOpen(true);
18244
18285
  };
@@ -18250,19 +18291,51 @@
18250
18291
  var eligiblePlanKeys = eligibilityPlans.reduce(function (acc, plan, index) {
18251
18292
  return plan.eligible ? [].concat(acc, [index]) : acc;
18252
18293
  }, []);
18294
+ /**
18295
+ * If merchand specify a suggestedPaymentPlan and no transition, we set a very long transition delay.
18296
+ * Otherwise, we set the transition delay specified by the merchant.
18297
+ * If none of those properties are specified, we set a default transition delay.
18298
+ * @returns
18299
+ */
18300
+
18301
+ var realTransitionTime = function realTransitionTime() {
18302
+ if (isTransitionSpecified) {
18303
+ return transitionDelay != null ? transitionDelay : DEFAULT_TRANSITION_TIME;
18304
+ }
18253
18305
 
18254
- var _useButtonAnimation = useButtonAnimation(eligiblePlanKeys, transitionDelay ? transitionDelay : 5500),
18306
+ if (isSuggestedPaymentPlanSpecified) {
18307
+ return VERY_LONG_TIME_IN_MS;
18308
+ }
18309
+
18310
+ return DEFAULT_TRANSITION_TIME;
18311
+ };
18312
+
18313
+ var _useButtonAnimation = useButtonAnimation(eligiblePlanKeys, realTransitionTime()),
18255
18314
  current = _useButtonAnimation.current,
18256
18315
  onHover = _useButtonAnimation.onHover,
18257
- onLeave = _useButtonAnimation.onLeave; // This hook is needed to update the initial plan index when useButtonAnimation
18258
- // updates the `current` value (i.e. without any hover / click callback).
18259
-
18316
+ onLeave = _useButtonAnimation.onLeave;
18260
18317
 
18261
18318
  react.useEffect(function () {
18262
- if (current !== initialPlanIndex) {
18263
- setInitialPlanIndex(current);
18319
+ // When API has given a response AND the marchand set an active plan by default.
18320
+ if (status === apiStatus.SUCCESS && isSuggestedPaymentPlanSpecified) {
18321
+ onHover(activePlanIndex); // We select the first active plan possible
18322
+
18323
+ onLeave(); // We need to call onLeave to reset the animation
18264
18324
  }
18265
- }, [current, initialPlanIndex]);
18325
+ }, [status]);
18326
+ /**
18327
+ * It takes a plan index and returns the index of that plan within the eligible plans
18328
+ *
18329
+ * @param {number} planIndex - The index of the plan that the user has selected.
18330
+ * @returns The index of the planKey in the eligiblePlanKeys array.
18331
+ */
18332
+
18333
+ var getIndexWithinEligiblePlans = function getIndexWithinEligiblePlans(planIndex) {
18334
+ var index = eligiblePlanKeys.findIndex(function (planKey) {
18335
+ return planKey === planIndex;
18336
+ });
18337
+ return index === -1 ? 0 : index;
18338
+ };
18266
18339
 
18267
18340
  if (status === apiStatus.PENDING) {
18268
18341
  return /*#__PURE__*/react.createElement("div", {
@@ -18270,11 +18343,7 @@
18270
18343
  }, /*#__PURE__*/react.createElement(Loader, null));
18271
18344
  }
18272
18345
 
18273
- if (hideIfNotEligible && eligiblePlans.length == 0 || eligibilityPlans.length === 0) {
18274
- return null;
18275
- }
18276
-
18277
- if (status === apiStatus.FAILED) {
18346
+ if (hideIfNotEligible && eligiblePlans.length === 0 || eligibilityPlans.length === 0 || status === apiStatus.FAILED) {
18278
18347
  return null;
18279
18348
  }
18280
18349
 
@@ -18286,19 +18355,9 @@
18286
18355
  }
18287
18356
  };
18288
18357
 
18289
- var handleHoverAndClick = function handleHoverAndClick(key, newInitialPlanIndex) {
18290
- // This is needed to update the initial plan index and button style when hovering (desktop) or clicking (mobile).
18291
- // The actual modal opening is triggered by a click on the highest parent div in `handleOpenModal`.
18292
- onHover(key);
18293
- setInitialPlanIndex(newInitialPlanIndex);
18294
- };
18295
-
18296
18358
  return /*#__PURE__*/react.createElement(react.Fragment, null, /*#__PURE__*/react.createElement("div", {
18297
18359
  onClick: handleOpenModal,
18298
- className: s$b.widgetButton,
18299
- style: {
18300
- cursor: eligiblePlans.length > 0 ? 'pointer' : 'initial'
18301
- },
18360
+ className: classnames(s$b.widgetButton, (_cx = {}, _cx[s$b.clickable] = eligiblePlans.length > 0, _cx[s$b.unClickable] = eligiblePlans.length === 0, _cx)),
18302
18361
  "data-testid": "widget-button"
18303
18362
  }, /*#__PURE__*/react.createElement("div", {
18304
18363
  className: s$b.primaryContainer
@@ -18307,28 +18366,20 @@
18307
18366
  }), /*#__PURE__*/react.createElement("div", {
18308
18367
  className: s$b.paymentPlans
18309
18368
  }, eligibilityPlans.map(function (eligibilityPlan, key) {
18310
- var _cx;
18369
+ var _cx2;
18311
18370
 
18312
- var newInitialPlanIndex = eligiblePlanKeys.includes(key) ? eligiblePlanKeys.findIndex(function (planKey) {
18313
- return planKey === key;
18314
- }) : 0;
18315
18371
  return /*#__PURE__*/react.createElement("div", {
18372
+ key: key,
18316
18373
  onMouseEnter: function onMouseEnter() {
18317
- handleHoverAndClick(key, newInitialPlanIndex);
18318
- },
18319
- onMouseOut: function onMouseOut() {
18320
- onLeave();
18374
+ return onHover(key);
18321
18375
  },
18322
- onClick: function onClick() {
18323
- handleHoverAndClick(key, newInitialPlanIndex);
18324
- },
18325
- key: key,
18326
- className: classnames(s$b.plan, (_cx = {}, _cx[s$b.active] = current === key, _cx[s$b.notEligible] = !eligibilityPlan.eligible, _cx))
18376
+ onMouseOut: onLeave,
18377
+ className: classnames(s$b.plan, (_cx2 = {}, _cx2[s$b.active] = current === key, _cx2[s$b.notEligible] = !eligibilityPlan.eligible, _cx2))
18327
18378
  }, paymentPlanShorthandName(eligibilityPlan));
18328
18379
  }))), /*#__PURE__*/react.createElement("div", {
18329
- className: classnames(s$b.info, (_cx2 = {}, _cx2[s$b.notEligible] = eligibilityPlans[current] && !eligibilityPlans[current].eligible, _cx2))
18380
+ className: classnames(s$b.info, (_cx3 = {}, _cx3[s$b.notEligible] = eligibilityPlans[current] && !eligibilityPlans[current].eligible, _cx3))
18330
18381
  }, eligibilityPlans.length !== 0 && paymentPlanInfoText(eligibilityPlans[current]))), isOpen && /*#__PURE__*/react.createElement(EligibilityModal, {
18331
- initialPlanIndex: initialPlanIndex,
18382
+ initialPlanIndex: getIndexWithinEligiblePlans(current),
18332
18383
  onClose: closeModal,
18333
18384
  eligibilityPlans: eligiblePlans,
18334
18385
  status: status
@@ -18357,6 +18408,7 @@
18357
18408
  plans = options.plans,
18358
18409
  transitionDelay = options.transitionDelay,
18359
18410
  hideIfNotEligible = options.hideIfNotEligible,
18411
+ suggestedPaymentPlan = options.suggestedPaymentPlan,
18360
18412
  _options$locale = options.locale,
18361
18413
  locale = _options$locale === void 0 ? Locale.en : _options$locale;
18362
18414
 
@@ -18368,7 +18420,8 @@
18368
18420
  apiData: this.apiData,
18369
18421
  configPlans: plans,
18370
18422
  transitionDelay: transitionDelay,
18371
- hideIfNotEligible: hideIfNotEligible
18423
+ hideIfNotEligible: hideIfNotEligible,
18424
+ suggestedPaymentPlan: suggestedPaymentPlan
18372
18425
  })), document.querySelector(container));
18373
18426
  }
18374
18427
  }