@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.
@@ -1526,7 +1526,7 @@ var messagesDE = {
1526
1526
  "payment-plan-strings.deferred": "{totalAmount} zu zahlen am {dueDate}",
1527
1527
  "payment-plan-strings.ineligible-greater-than-max": "Bis zu {maxAmount}",
1528
1528
  "payment-plan-strings.ineligible-lower-than-min": "Ab {minAmount}",
1529
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}",
1529
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} dann {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
1530
1530
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
1531
1531
  "payment-plan-strings.no-fee": "(gebührenfrei)"
1532
1532
  };
@@ -1548,7 +1548,7 @@ var messagesEN = {
1548
1548
  "payment-plan-strings.deferred": "{totalAmount} to be paid on {dueDate}",
1549
1549
  "payment-plan-strings.ineligible-greater-than-max": "Until {maxAmount}",
1550
1550
  "payment-plan-strings.ineligible-lower-than-min": "From {minAmount}",
1551
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}",
1551
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} then {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
1552
1552
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
1553
1553
  "payment-plan-strings.no-fee": "(free of charge)"
1554
1554
  };
@@ -1570,7 +1570,7 @@ var messagesES = {
1570
1570
  "payment-plan-strings.deferred": "{totalAmount} a pagar el {dueDate}",
1571
1571
  "payment-plan-strings.ineligible-greater-than-max": "Hasta {maxAmount}",
1572
1572
  "payment-plan-strings.ineligible-lower-than-min": "Desde {minAmount}",
1573
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} hoy, después {numberOfRemainingInstallments, plural, one {# mensualidad} other {# mensualidades}} de {othersInstallmentAmount}",
1573
+ "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 }}",
1574
1574
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} plazos de {totalAmount}",
1575
1575
  "payment-plan-strings.no-fee": "(sin intereses)"
1576
1576
  };
@@ -1592,7 +1592,7 @@ var messagesFR = {
1592
1592
  "payment-plan-strings.deferred": "{totalAmount} à payer le {dueDate}",
1593
1593
  "payment-plan-strings.ineligible-greater-than-max": "Jusqu'à {maxAmount}",
1594
1594
  "payment-plan-strings.ineligible-lower-than-min": "À partir de {minAmount}",
1595
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}",
1595
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
1596
1596
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
1597
1597
  "payment-plan-strings.no-fee": "(sans frais)"
1598
1598
  };
@@ -1614,7 +1614,7 @@ var messagesIT = {
1614
1614
  "payment-plan-strings.deferred": "{totalAmount} da pagare il {dueDate}",
1615
1615
  "payment-plan-strings.ineligible-greater-than-max": "Disponibile fino a {maxAmount}",
1616
1616
  "payment-plan-strings.ineligible-lower-than-min": "Pagamento rateale disponibile a partire da {minAmount}",
1617
- "payment-plan-strings.multiple-installments": "Oggi paghi {firstInstallmentAmount} poi {numberOfRemainingInstallments} rate mensili di {othersInstallmentAmount}",
1617
+ "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}}}",
1618
1618
  "payment-plan-strings.multiple-installments-same-amount": "In {installmentsCount} rate mensili di {totalAmount}",
1619
1619
  "payment-plan-strings.no-fee": "(senza interessi)"
1620
1620
  };
@@ -1636,7 +1636,7 @@ var messagesNL = {
1636
1636
  "payment-plan-strings.deferred": "{totalAmount} te betalen op {dueDate}",
1637
1637
  "payment-plan-strings.ineligible-greater-than-max": "Tot {maxAmount}",
1638
1638
  "payment-plan-strings.ineligible-lower-than-min": "Van {minAmount}",
1639
- "payment-plan-strings.multiple-installments": "{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}",
1639
+ "payment-plan-strings.multiple-installments": "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} dan {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
1640
1640
  "payment-plan-strings.multiple-installments-same-amount": "{installmentsCount} x {totalAmount}",
1641
1641
  "payment-plan-strings.no-fee": "(gratis)"
1642
1642
  };
@@ -1784,8 +1784,8 @@ var useFetchEligibility = function useFetchEligibility(purchaseAmount, _ref, pla
1784
1784
  }, {
1785
1785
  Authorization: "Alma-Merchant-Auth " + merchantId
1786
1786
  }).then(function (res) {
1787
- setStatus(apiStatus.SUCCESS);
1788
1787
  setEligibility(res);
1788
+ setStatus(apiStatus.SUCCESS);
1789
1789
  })["catch"](function () {
1790
1790
  setStatus(apiStatus.FAILED);
1791
1791
  });
@@ -1967,7 +1967,7 @@ var paymentPlanInfoText = function paymentPlanInfoText(payment) {
1967
1967
 
1968
1968
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FormattedMessage, {
1969
1969
  id: "payment-plan-strings.multiple-installments",
1970
- defaultMessage: "{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}",
1970
+ defaultMessage: "{numberOfRemainingInstallments, plural, one {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}} other {{firstInstallmentAmount} puis {numberOfRemainingInstallments} x {othersInstallmentAmount}}}",
1971
1971
  values: {
1972
1972
  firstInstallmentAmount: /*#__PURE__*/React.createElement(FormattedNumber, {
1973
1973
  value: priceFromCents(payment.payment_plan[0].total_amount),
@@ -2333,16 +2333,54 @@ var useButtonAnimation = function useButtonAnimation(iterateValues, transitionDe
2333
2333
  };
2334
2334
  };
2335
2335
 
2336
- 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"};
2336
+ /**
2337
+ * It returns the **index** of the **first eligible plan** that matches the default installments count
2338
+ *
2339
+ * @param {number | number[]} suggestedPaymentPlan
2340
+ * @param {EligibilityPlan[]} eligibilityPlans
2341
+ * @returns number (index of the first eligible plan that matches the default installments count)
2342
+ */
2343
+ var getIndexOfActivePlan = function getIndexOfActivePlan(_ref) {
2344
+ var suggestedPaymentPlan = _ref.suggestedPaymentPlan,
2345
+ eligibilityPlans = _ref.eligibilityPlans;
2346
+ var suggestedPaymentPlanArray = Array.isArray(suggestedPaymentPlan) ? suggestedPaymentPlan : [suggestedPaymentPlan];
2347
+
2348
+ var _loop = function _loop(index) {
2349
+ var installmentsCount = suggestedPaymentPlanArray[index];
2350
+ var planFound = eligibilityPlans.findIndex(function (plan) {
2351
+ return plan.installments_count === installmentsCount && plan.eligible;
2352
+ });
2353
+
2354
+ if (planFound !== -1) {
2355
+ return {
2356
+ v: planFound
2357
+ };
2358
+ }
2359
+ };
2360
+
2361
+ for (var index in suggestedPaymentPlanArray) {
2362
+ var _ret = _loop(index);
2363
+
2364
+ if (typeof _ret === "object") return _ret.v;
2365
+ }
2366
+
2367
+ return 0;
2368
+ };
2369
+
2370
+ 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"};
2371
+
2372
+ var VERY_LONG_TIME_IN_MS = 1000 * 3600 * 24 * 365;
2373
+ var DEFAULT_TRANSITION_TIME = 5500;
2337
2374
 
2338
2375
  var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2339
- var _cx2;
2376
+ var _cx, _cx3;
2340
2377
 
2341
2378
  var purchaseAmount = _ref.purchaseAmount,
2342
2379
  apiData = _ref.apiData,
2343
2380
  configPlans = _ref.configPlans,
2344
2381
  transitionDelay = _ref.transitionDelay,
2345
- hideIfNotEligible = _ref.hideIfNotEligible;
2382
+ hideIfNotEligible = _ref.hideIfNotEligible,
2383
+ suggestedPaymentPlan = _ref.suggestedPaymentPlan;
2346
2384
 
2347
2385
  var _useFetchEligibility = useFetchEligibility(purchaseAmount, apiData, configPlans),
2348
2386
  eligibilityPlans = _useFetchEligibility[0],
@@ -2351,15 +2389,18 @@ var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2351
2389
  var eligiblePlans = eligibilityPlans.filter(function (plan) {
2352
2390
  return plan.eligible;
2353
2391
  });
2392
+ var activePlanIndex = getIndexOfActivePlan({
2393
+ eligibilityPlans: eligibilityPlans,
2394
+ suggestedPaymentPlan: suggestedPaymentPlan != null ? suggestedPaymentPlan : 0
2395
+ });
2396
+ var isSuggestedPaymentPlanSpecified = suggestedPaymentPlan !== undefined; // 👈 The merchant decided to focus a tab and remove animated transition.
2397
+
2398
+ var isTransitionSpecified = transitionDelay !== undefined; // 👈 The merchant has specified a transition time
2354
2399
 
2355
2400
  var _useState = useState(false),
2356
2401
  isOpen = _useState[0],
2357
2402
  setIsOpen = _useState[1];
2358
2403
 
2359
- var _useState2 = useState(0),
2360
- initialPlanIndex = _useState2[0],
2361
- setInitialPlanIndex = _useState2[1];
2362
-
2363
2404
  var openModal = function openModal() {
2364
2405
  return setIsOpen(true);
2365
2406
  };
@@ -2371,19 +2412,51 @@ var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2371
2412
  var eligiblePlanKeys = eligibilityPlans.reduce(function (acc, plan, index) {
2372
2413
  return plan.eligible ? [].concat(acc, [index]) : acc;
2373
2414
  }, []);
2415
+ /**
2416
+ * If merchand specify a suggestedPaymentPlan and no transition, we set a very long transition delay.
2417
+ * Otherwise, we set the transition delay specified by the merchant.
2418
+ * If none of those properties are specified, we set a default transition delay.
2419
+ * @returns
2420
+ */
2421
+
2422
+ var realTransitionTime = function realTransitionTime() {
2423
+ if (isTransitionSpecified) {
2424
+ return transitionDelay != null ? transitionDelay : DEFAULT_TRANSITION_TIME;
2425
+ }
2374
2426
 
2375
- var _useButtonAnimation = useButtonAnimation(eligiblePlanKeys, transitionDelay ? transitionDelay : 5500),
2427
+ if (isSuggestedPaymentPlanSpecified) {
2428
+ return VERY_LONG_TIME_IN_MS;
2429
+ }
2430
+
2431
+ return DEFAULT_TRANSITION_TIME;
2432
+ };
2433
+
2434
+ var _useButtonAnimation = useButtonAnimation(eligiblePlanKeys, realTransitionTime()),
2376
2435
  current = _useButtonAnimation.current,
2377
2436
  onHover = _useButtonAnimation.onHover,
2378
- onLeave = _useButtonAnimation.onLeave; // This hook is needed to update the initial plan index when useButtonAnimation
2379
- // updates the `current` value (i.e. without any hover / click callback).
2380
-
2437
+ onLeave = _useButtonAnimation.onLeave;
2381
2438
 
2382
2439
  useEffect(function () {
2383
- if (current !== initialPlanIndex) {
2384
- setInitialPlanIndex(current);
2440
+ // When API has given a response AND the marchand set an active plan by default.
2441
+ if (status === apiStatus.SUCCESS && isSuggestedPaymentPlanSpecified) {
2442
+ onHover(activePlanIndex); // We select the first active plan possible
2443
+
2444
+ onLeave(); // We need to call onLeave to reset the animation
2385
2445
  }
2386
- }, [current, initialPlanIndex]);
2446
+ }, [status]);
2447
+ /**
2448
+ * It takes a plan index and returns the index of that plan within the eligible plans
2449
+ *
2450
+ * @param {number} planIndex - The index of the plan that the user has selected.
2451
+ * @returns The index of the planKey in the eligiblePlanKeys array.
2452
+ */
2453
+
2454
+ var getIndexWithinEligiblePlans = function getIndexWithinEligiblePlans(planIndex) {
2455
+ var index = eligiblePlanKeys.findIndex(function (planKey) {
2456
+ return planKey === planIndex;
2457
+ });
2458
+ return index === -1 ? 0 : index;
2459
+ };
2387
2460
 
2388
2461
  if (status === apiStatus.PENDING) {
2389
2462
  return /*#__PURE__*/React.createElement("div", {
@@ -2391,11 +2464,7 @@ var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2391
2464
  }, /*#__PURE__*/React.createElement(Loader, null));
2392
2465
  }
2393
2466
 
2394
- if (hideIfNotEligible && eligiblePlans.length == 0 || eligibilityPlans.length === 0) {
2395
- return null;
2396
- }
2397
-
2398
- if (status === apiStatus.FAILED) {
2467
+ if (hideIfNotEligible && eligiblePlans.length === 0 || eligibilityPlans.length === 0 || status === apiStatus.FAILED) {
2399
2468
  return null;
2400
2469
  }
2401
2470
 
@@ -2407,19 +2476,9 @@ var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2407
2476
  }
2408
2477
  };
2409
2478
 
2410
- var handleHoverAndClick = function handleHoverAndClick(key, newInitialPlanIndex) {
2411
- // This is needed to update the initial plan index and button style when hovering (desktop) or clicking (mobile).
2412
- // The actual modal opening is triggered by a click on the highest parent div in `handleOpenModal`.
2413
- onHover(key);
2414
- setInitialPlanIndex(newInitialPlanIndex);
2415
- };
2416
-
2417
2479
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
2418
2480
  onClick: handleOpenModal,
2419
- className: s$b.widgetButton,
2420
- style: {
2421
- cursor: eligiblePlans.length > 0 ? 'pointer' : 'initial'
2422
- },
2481
+ className: cx(s$b.widgetButton, (_cx = {}, _cx[s$b.clickable] = eligiblePlans.length > 0, _cx[s$b.unClickable] = eligiblePlans.length === 0, _cx)),
2423
2482
  "data-testid": "widget-button"
2424
2483
  }, /*#__PURE__*/React.createElement("div", {
2425
2484
  className: s$b.primaryContainer
@@ -2428,28 +2487,20 @@ var PaymentPlanWidget = function PaymentPlanWidget(_ref) {
2428
2487
  }), /*#__PURE__*/React.createElement("div", {
2429
2488
  className: s$b.paymentPlans
2430
2489
  }, eligibilityPlans.map(function (eligibilityPlan, key) {
2431
- var _cx;
2490
+ var _cx2;
2432
2491
 
2433
- var newInitialPlanIndex = eligiblePlanKeys.includes(key) ? eligiblePlanKeys.findIndex(function (planKey) {
2434
- return planKey === key;
2435
- }) : 0;
2436
2492
  return /*#__PURE__*/React.createElement("div", {
2493
+ key: key,
2437
2494
  onMouseEnter: function onMouseEnter() {
2438
- handleHoverAndClick(key, newInitialPlanIndex);
2439
- },
2440
- onMouseOut: function onMouseOut() {
2441
- onLeave();
2495
+ return onHover(key);
2442
2496
  },
2443
- onClick: function onClick() {
2444
- handleHoverAndClick(key, newInitialPlanIndex);
2445
- },
2446
- key: key,
2447
- className: cx(s$b.plan, (_cx = {}, _cx[s$b.active] = current === key, _cx[s$b.notEligible] = !eligibilityPlan.eligible, _cx))
2497
+ onMouseOut: onLeave,
2498
+ className: cx(s$b.plan, (_cx2 = {}, _cx2[s$b.active] = current === key, _cx2[s$b.notEligible] = !eligibilityPlan.eligible, _cx2))
2448
2499
  }, paymentPlanShorthandName(eligibilityPlan));
2449
2500
  }))), /*#__PURE__*/React.createElement("div", {
2450
- className: cx(s$b.info, (_cx2 = {}, _cx2[s$b.notEligible] = eligibilityPlans[current] && !eligibilityPlans[current].eligible, _cx2))
2501
+ className: cx(s$b.info, (_cx3 = {}, _cx3[s$b.notEligible] = eligibilityPlans[current] && !eligibilityPlans[current].eligible, _cx3))
2451
2502
  }, eligibilityPlans.length !== 0 && paymentPlanInfoText(eligibilityPlans[current]))), isOpen && /*#__PURE__*/React.createElement(EligibilityModal, {
2452
- initialPlanIndex: initialPlanIndex,
2503
+ initialPlanIndex: getIndexWithinEligiblePlans(current),
2453
2504
  onClose: closeModal,
2454
2505
  eligibilityPlans: eligiblePlans,
2455
2506
  status: status
@@ -2478,6 +2529,7 @@ var WidgetsController = /*#__PURE__*/function () {
2478
2529
  plans = options.plans,
2479
2530
  transitionDelay = options.transitionDelay,
2480
2531
  hideIfNotEligible = options.hideIfNotEligible,
2532
+ suggestedPaymentPlan = options.suggestedPaymentPlan,
2481
2533
  _options$locale = options.locale,
2482
2534
  locale = _options$locale === void 0 ? Locale.en : _options$locale;
2483
2535
 
@@ -2489,7 +2541,8 @@ var WidgetsController = /*#__PURE__*/function () {
2489
2541
  apiData: this.apiData,
2490
2542
  configPlans: plans,
2491
2543
  transitionDelay: transitionDelay,
2492
- hideIfNotEligible: hideIfNotEligible
2544
+ hideIfNotEligible: hideIfNotEligible,
2545
+ suggestedPaymentPlan: suggestedPaymentPlan
2493
2546
  })), document.querySelector(container));
2494
2547
  }
2495
2548
  }