ui.shipaid.com 0.3.94 → 0.3.96

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/widget.es.js CHANGED
@@ -2392,6 +2392,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2392
2392
  this.fetchInterceptorCleanup = () => {
2393
2393
  };
2394
2394
  this.intervalId = null;
2395
+ this._sellingPlanId = null;
2395
2396
  this._state = {
2396
2397
  loading: false,
2397
2398
  success: null,
@@ -2758,28 +2759,17 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2758
2759
  this._cart = cart;
2759
2760
  }
2760
2761
  async addCartProtectionVariant() {
2761
- var _a, _b, _c, _d;
2762
+ var _a, _b;
2762
2763
  let cart;
2763
- let sellingPlanId;
2764
- if (this.supportSubscriptions && !this.useShipAidCheckout) {
2765
- const itemWithSubscription = (_b = (_a = this._cart) == null ? void 0 : _a.items) == null ? void 0 : _b.find((item) => {
2766
- var _a2;
2767
- return item.id !== ((_a2 = this._protectionVariant) == null ? void 0 : _a2.id) && !!(item == null ? void 0 : item.selling_plan_allocation);
2768
- });
2769
- if (itemWithSubscription) {
2770
- const protectionSellingPlan = await this._fetchSellingPlanFromVariant(itemWithSubscription.selling_plan_allocation.selling_plan);
2771
- sellingPlanId = protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : null;
2772
- }
2773
- }
2774
2764
  if (this.useCustomStoreFront) {
2775
2765
  const cartItemAdded = await this.runStoreFrontQuery(
2776
2766
  "mutation AddItemToCart($cartId: ID!, $lines: [CartLineInput!]!) { cartLinesAdd(cartId: $cartId, lines: $lines) { cart { id lines(first: 10) { edges { node { id quantity merchandise { ... on ProductVariant { id sku } } cost{ totalAmount{ amount currencyCode } } } } } cost { totalAmount { amount currencyCode } subtotalAmount { amount currencyCode } } } } }",
2777
2767
  {
2778
2768
  cartId: this.cartId,
2779
2769
  lines: [{
2780
- merchandiseId: String((_c = this._protectionVariant) == null ? void 0 : _c.id),
2770
+ merchandiseId: String((_a = this._protectionVariant) == null ? void 0 : _a.id),
2781
2771
  quantity: 1,
2782
- sellingPlanId
2772
+ sellingPlanId: this._sellingPlanId
2783
2773
  }]
2784
2774
  }
2785
2775
  );
@@ -2787,8 +2777,8 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2787
2777
  } else {
2788
2778
  const payload = {
2789
2779
  quantity: 1,
2790
- id: String((_d = this._protectionVariant) == null ? void 0 : _d.id),
2791
- selling_plan: sellingPlanId
2780
+ id: String((_b = this._protectionVariant) == null ? void 0 : _b.id),
2781
+ selling_plan: this._sellingPlanId
2792
2782
  };
2793
2783
  cart = await this._fetch.post(
2794
2784
  "/cart/add.js",
@@ -2797,7 +2787,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2797
2787
  }
2798
2788
  return cart;
2799
2789
  }
2800
- async updateCartProtectionVariant(qty, protectionCartItem = null, sellingPlanId = null) {
2790
+ async updateCartProtectionVariant(qty, protectionCartItem = null) {
2801
2791
  var _a, _b;
2802
2792
  let cart;
2803
2793
  if (this.useCustomStoreFront) {
@@ -2809,7 +2799,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2809
2799
  {
2810
2800
  id: String(protectionCartItem ? protectionCartItem.key : (_a = this._protectionCartItem) == null ? void 0 : _a.key),
2811
2801
  quantity: qty,
2812
- sellingPlanId
2802
+ sellingPlanId: this._sellingPlanId
2813
2803
  }
2814
2804
  ]
2815
2805
  }
@@ -2819,7 +2809,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
2819
2809
  const payload = {
2820
2810
  quantity: qty,
2821
2811
  id: String(protectionCartItem ? protectionCartItem.key : (_b = this._protectionCartItem) == null ? void 0 : _b.key),
2822
- selling_plan: sellingPlanId
2812
+ selling_plan: this._sellingPlanId
2823
2813
  };
2824
2814
  cart = await this._fetch.post(
2825
2815
  "/cart/change.js",
@@ -3089,9 +3079,9 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3089
3079
  });
3090
3080
  if (itemWithSubscription) {
3091
3081
  const protectionSellingPlan = await this._fetchSellingPlanFromVariant(itemWithSubscription.selling_plan_allocation.selling_plan);
3092
- return protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : void 0;
3082
+ return protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : null;
3093
3083
  }
3094
- return void 0;
3084
+ return null;
3095
3085
  }
3096
3086
  getCheckoutURL() {
3097
3087
  var _a, _b;
@@ -3101,7 +3091,7 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3101
3091
  return `/checkout?attributes[_shipaid-internal]=1&updates[${(_b = this._protectionVariant) == null ? void 0 : _b.id}]=1`;
3102
3092
  }
3103
3093
  checkoutButtonTemplate() {
3104
- var _a, _b, _c, _d, _e, _f;
3094
+ var _a, _b, _c, _d, _e, _f, _g;
3105
3095
  if (!document.getElementById("shipaid-styles")) {
3106
3096
  const style = document.createElement("style");
3107
3097
  style.id = "shipaid-styles";
@@ -3264,16 +3254,25 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3264
3254
  </svg>
3265
3255
  </div>
3266
3256
  </div>
3267
-
3268
- <a href="${((_d = this._protectionVariant) == null ? void 0 : _d.id) ? this.getCheckoutURL() : "/checkout"}" id="shipaid-checkout-button" class="${originalClasses}" @click=${() => {
3257
+ ${this._sellingPlanId ? x`
3258
+ <button id="shipaid-checkout-button" class="${originalClasses}" @click=${async () => {
3259
+ var _a2, _b2;
3260
+ sessionStorage.setItem("shipaid_variant", JSON.stringify((_a2 = this._protectionVariant) == null ? void 0 : _a2.id));
3261
+ await this.addCartProtectionVariant();
3262
+ window.location.href = `/checkout?attributes[_shipaid-internal]=1&updates[${(_b2 = this._protectionVariant) == null ? void 0 : _b2.id}]=1`;
3263
+ }}>
3264
+ <slot name="checkout-button-text">CHECKOUT+</slot> ${cartTotal ? this._currencyFormat(cartTotal) : loading2}
3265
+ </button>
3266
+ ` : x`
3267
+ <a href="/checkout${((_d = this._protectionVariant) == null ? void 0 : _d.id) ? `?attributes[_shipaid-internal]=1&updates[${(_e = this._protectionVariant) == null ? void 0 : _e.id}]=1` : ""}" id="shipaid-checkout-button" class="${originalClasses}" @click=${() => {
3269
3268
  var _a2;
3270
3269
  sessionStorage.setItem("shipaid_variant", JSON.stringify((_a2 = this._protectionVariant) == null ? void 0 : _a2.id));
3271
3270
  }}>
3272
- <span class="shipaid-checkout-text">${translate("checkout-plus.checkout")}+</span>
3273
- <span class="shipaid-checkout-amount">${cartTotal ? this._currencyFormat(cartTotal) : loading2}</span>
3274
- </a>
3275
-
3276
- <a href="/checkout${((_e = this._protectionVariant) == null ? void 0 : _e.id) ? `?attributes[_shipaid-internal]=1&updates[${(_f = this._protectionVariant) == null ? void 0 : _f.id}]=0` : ""}" class="continue-link">
3271
+ <span class="shipaid-checkout-text">${translate("checkout-plus.checkout")}+</span>
3272
+ <span class="shipaid-checkout-amount">${cartTotal ? this._currencyFormat(cartTotal) : loading2}</span>
3273
+ </a>
3274
+ `}
3275
+ <a href="/checkout${((_f = this._protectionVariant) == null ? void 0 : _f.id) ? `?attributes[_shipaid-internal]=1&updates[${(_g = this._protectionVariant) == null ? void 0 : _g.id}]=0` : ""}" class="continue-link">
3277
3276
  <slot name="link-continue">${translate("checkout-plus.continue")}</slot>
3278
3277
  </a>
3279
3278
  </div>
@@ -3466,16 +3465,16 @@ const _ShipAidWidget = class _ShipAidWidget extends s$1 {
3466
3465
  var _a2;
3467
3466
  return item.id !== ((_a2 = this._protectionVariant) == null ? void 0 : _a2.id) && !!(item == null ? void 0 : item.selling_plan_allocation);
3468
3467
  });
3469
- let updateSubscriptionPayload = null;
3468
+ let shouldUpdateSubscription = true;
3470
3469
  if (!itemWithSubscription && (protectionCartItem == null ? void 0 : protectionCartItem.selling_plan_allocation)) {
3471
- updateSubscriptionPayload = { id: protectionCartItem.key, quantity: 1, selling_plan: null };
3470
+ this._sellingPlanId = null;
3472
3471
  } else if (itemWithSubscription && !(protectionCartItem == null ? void 0 : protectionCartItem.selling_plan_allocation)) {
3473
- const protectionSellingPlan = await this._fetchSellingPlanFromVariant(itemWithSubscription.selling_plan_allocation.selling_plan);
3474
- const sellingPlanId = protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : null;
3475
- updateSubscriptionPayload = { id: protectionCartItem.key, quantity: 1, selling_plan: sellingPlanId };
3472
+ this._sellingPlanId = await this.getSubscription(this._cart);
3473
+ } else {
3474
+ shouldUpdateSubscription = false;
3476
3475
  }
3477
- if (updateSubscriptionPayload) {
3478
- const cart2 = await this.updateCartProtectionVariant(updateSubscriptionPayload.quantity, protectionCartItem, updateSubscriptionPayload.selling_plan);
3476
+ if (shouldUpdateSubscription) {
3477
+ const cart2 = await this.updateCartProtectionVariant(1, protectionCartItem);
3479
3478
  await this._handleRefresh(cart2);
3480
3479
  }
3481
3480
  }