ui.shipaid.com 0.3.25 → 0.3.27

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
@@ -1,5 +1,5 @@
1
1
  function calculateProtectionTotal(store, protectionProduct, cart) {
2
- var _a, _b, _c;
2
+ var _a, _b;
3
3
  if (!store)
4
4
  throw new Error("Missing store settings.");
5
5
  if (!protectionProduct)
@@ -10,23 +10,34 @@ function calculateProtectionTotal(store, protectionProduct, cart) {
10
10
  if (!settings) {
11
11
  throw new Error("Tried to find protection variant, but protection settings for this store are missing.");
12
12
  }
13
- const excludedProductSkus = Array.isArray(store == null ? void 0 : store.excludedProductSkus) ? store == null ? void 0 : store.excludedProductSkus.map((sku) => sku.trim()) : [];
14
- const itemTotal = ((_a = cart.items) == null ? void 0 : _a.reduce((total, item) => {
15
- if (!item.sku)
16
- return total;
17
- const itemIsExcluded = excludedProductSkus.some((sku) => sku === item.sku.trim());
18
- return itemIsExcluded ? total - item.final_line_price : total;
19
- }, cart.total_price)) ?? cart.total_price;
20
- const protectionVariantsInCart = ((_b = cart.items) == null ? void 0 : _b.filter((item) => {
13
+ const excludedProductSkus = Array.isArray(store == null ? void 0 : store.excludedProductSkus) ? store.excludedProductSkus.map((sku) => sku.trim()) : [];
14
+ const excludedProductIds = Array.isArray(store == null ? void 0 : store.excludedProductsVariantsId) ? store.excludedProductsVariantsId.map((id) => {
15
+ var _a2;
16
+ return parseInt(((_a2 = id.match(/\d+/)) == null ? void 0 : _a2[0]) ?? "", 10);
17
+ }) : [];
18
+ const isItemExcluded = (item) => {
19
+ if (item.sku && excludedProductSkus.includes(item.sku.trim())) {
20
+ return true;
21
+ } else if (item.variant_id && excludedProductIds.includes(item.variant_id)) {
22
+ return true;
23
+ }
24
+ return false;
25
+ };
26
+ const itemTotal = (cart.items ?? []).reduce((total, item) => {
27
+ return isItemExcluded(item) ? total - item.final_line_price : total;
28
+ }, cart.total_price || 0);
29
+ const protectionVariantsInCart = ((_a = cart.items) == null ? void 0 : _a.filter((item) => {
21
30
  var _a2;
22
31
  return (_a2 = protectionProduct == null ? void 0 : protectionProduct.variants) == null ? void 0 : _a2.some((variant) => variant.id === item.variant_id);
23
32
  })) ?? [];
24
33
  const protectionVariantsInCartTotal = protectionVariantsInCart.reduce((total, item) => total + item.final_line_price, 0);
25
34
  const cartTotal = itemTotal - protectionVariantsInCartTotal;
35
+ if (cartTotal === 0)
36
+ return cartTotal;
26
37
  if (settings.protectionType === "FIXED") {
27
38
  if (typeof settings.defaultFee !== "number")
28
39
  throw new Error("Missing default fee amount.");
29
- if (!((_c = settings.rules) == null ? void 0 : _c.length))
40
+ if (!((_b = settings.rules) == null ? void 0 : _b.length))
30
41
  return settings.defaultFee;
31
42
  const formattedCartTotal = cartTotal / 100;
32
43
  const sortedRules = settings.rules.sort((a2, b) => {
@@ -66,6 +77,10 @@ function findProtectionVariant(store, protectionProduct, protectionFee) {
66
77
  }
67
78
  return matchingVariant;
68
79
  }
80
+ function getIdFromShopifyGid(gid) {
81
+ var _a;
82
+ return ((_a = gid == null ? void 0 : gid.match(/\d+/)) == null ? void 0 : _a[0]) ?? null;
83
+ }
69
84
  const common = { calculateProtectionTotal, findProtectionVariant };
70
85
  /**
71
86
  * @license
@@ -1717,7 +1732,7 @@ const styles = i$2`
1717
1732
  .prompt-product-actions
1718
1733
  .prompt-product-actions-price {
1719
1734
  color: var(--shipaid-prompt-actions-price-color, var(--shipaid-text-muted));
1720
- font-size: var(--shipaid-font-base);
1735
+ font-size: var(--shipaid-prompt-actions-price-fontSize, --shipaid-font-base);
1721
1736
  }
1722
1737
  .shipaid-prompt
1723
1738
  .prompt-product
@@ -1821,6 +1836,9 @@ const StoreQuery = `query StoreByDomain ($store: String!) {
1821
1836
  widgetConfigurations
1822
1837
  }
1823
1838
  }`;
1839
+ const SellinPlanGroupFromVariant = `query SellingPlanFromVariant($store: String!, $variantId: String!){
1840
+ sellingPlanFromVariant(input: {store: $store, variantId: $variantId })
1841
+ }`;
1824
1842
  const langFiles = /* @__PURE__ */ Object.assign({
1825
1843
  "./lang/de.json": () => import("./de-7465ac1a.js").then((m2) => m2["default"]),
1826
1844
  "./lang/en.json": () => Promise.resolve().then(() => en).then((m2) => m2["default"]),
@@ -1851,10 +1869,11 @@ let ShipAidWidget = class extends s$1 {
1851
1869
  this.lang = "en";
1852
1870
  this.currency = void 0;
1853
1871
  this.customerId = void 0;
1872
+ this.supportSubscriptions = false;
1854
1873
  this._apiEndpoint = "/apps/shipaid";
1855
1874
  this._storeDomain = ((_a = window.Shopify) == null ? void 0 : _a.shop) ?? ((_c = (_b = window.Shopify) == null ? void 0 : _b.Checkout) == null ? void 0 : _c.apiHost);
1856
1875
  this._hasFinishedSetup = false;
1857
- this._shouldShowWidget = true;
1876
+ this._shouldShowWidget = false;
1858
1877
  this._hasProtectionInCart = false;
1859
1878
  this.hasLoadedStrings = false;
1860
1879
  this.intervalId = 0;
@@ -1905,9 +1924,14 @@ let ShipAidWidget = class extends s$1 {
1905
1924
  }
1906
1925
  // ? Internal Helpers
1907
1926
  _currencyFormat(value) {
1908
- var _a, _b, _c;
1927
+ var _a, _b, _c, _d, _e, _f;
1928
+ const currency = this.currency || ((_b = (_a = window.Shopify) == null ? void 0 : _a.currency) == null ? void 0 : _b.active) || ((_c = this._store) == null ? void 0 : _c.currency) || "USD";
1929
+ if ((_f = (_e = (_d = this._store) == null ? void 0 : _d.widgetConfigurations) == null ? void 0 : _e.widget) == null ? void 0 : _f.currencyFormat) {
1930
+ const format = this._store.widgetConfigurations.widget.currencyFormat;
1931
+ return format.replace("_value_", Number(value)).replace("_currency_", currency);
1932
+ }
1909
1933
  return new Intl.NumberFormat(void 0, {
1910
- currency: this.currency || ((_b = (_a = window.Shopify) == null ? void 0 : _a.currency) == null ? void 0 : _b.active) || ((_c = this._store) == null ? void 0 : _c.currency) || "USD",
1934
+ currency,
1911
1935
  style: "currency"
1912
1936
  }).format(Number(value));
1913
1937
  }
@@ -2010,6 +2034,36 @@ let ShipAidWidget = class extends s$1 {
2010
2034
  throw new Error(`Could not find a store for ${this._storeDomain}`);
2011
2035
  }
2012
2036
  }
2037
+ /** Fetches store info from the ShipAid public API. */
2038
+ async _fetchSellingPlanFromVariant() {
2039
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2040
+ const shop = ((_a = window.Shopify) == null ? void 0 : _a.shop) ?? ((_c = (_b = window.Shopify) == null ? void 0 : _b.Checkout) == null ? void 0 : _c.apiHost);
2041
+ if (!shop)
2042
+ throw new Error("No shop found in Shopify object.");
2043
+ try {
2044
+ const endpoint = new URL(window.location.href);
2045
+ endpoint.pathname = this._apiEndpoint;
2046
+ const payload = {
2047
+ query: SellinPlanGroupFromVariant,
2048
+ variables: { store: shop, variantId: `gid://shopify/ProductVariant/${(_d = this._protectionVariant) == null ? void 0 : _d.id}` }
2049
+ };
2050
+ const response = await this._fetch.post(
2051
+ endpoint.toString(),
2052
+ payload
2053
+ );
2054
+ if (!response)
2055
+ throw new Error("Missing response for selling plan query.");
2056
+ if ((_e = response.errors) == null ? void 0 : _e.length)
2057
+ throw new Error(response.errors[0].message);
2058
+ if (!((_f = response.data) == null ? void 0 : _f.sellingPlanFromVariant)) {
2059
+ throw new Error("Missing variant from selling plan query response.");
2060
+ }
2061
+ const sellingPlan = ((_n = (_m = (_l = (_k = (_j = (_i = (_h = (_g = response.data.sellingPlanFromVariant) == null ? void 0 : _g.sellingPlanGroups) == null ? void 0 : _h.edges) == null ? void 0 : _i[0]) == null ? void 0 : _j.node) == null ? void 0 : _k.sellingPlans) == null ? void 0 : _l.edges) == null ? void 0 : _m[0]) == null ? void 0 : _n.node) || null;
2062
+ return sellingPlan;
2063
+ } catch (error) {
2064
+ console.error("Error during the query ====>", error);
2065
+ }
2066
+ }
2013
2067
  /** Fetch current cart from the Shopify ajax API */
2014
2068
  async _fetchCart() {
2015
2069
  try {
@@ -2061,6 +2115,17 @@ let ShipAidWidget = class extends s$1 {
2061
2115
  quantity: 1,
2062
2116
  id: this._protectionVariant.id
2063
2117
  };
2118
+ if (this.supportSubscriptions) {
2119
+ const hasSubscriptions = this._cart.items.some((item) => {
2120
+ var _a2;
2121
+ return item.id !== ((_a2 = this._protectionVariant) == null ? void 0 : _a2.id) && !!(item == null ? void 0 : item.selling_plan_allocation);
2122
+ });
2123
+ if (hasSubscriptions) {
2124
+ const protectionSellingPlan = await this._fetchSellingPlanFromVariant();
2125
+ const sellingPlanId = protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : null;
2126
+ payload.selling_plan = sellingPlanId;
2127
+ }
2128
+ }
2064
2129
  const cartItem = await this._fetch.post(
2065
2130
  "/cart/add.js",
2066
2131
  payload
@@ -2314,7 +2379,6 @@ let ShipAidWidget = class extends s$1 {
2314
2379
  return;
2315
2380
  }
2316
2381
  this._hasFinishedSetup = true;
2317
- this._shouldShowWidget = true;
2318
2382
  this._dispatchEvent(Events.LOADED, this._store);
2319
2383
  setTimeout(async () => {
2320
2384
  var _a2, _b2, _c2, _d2;
@@ -2368,7 +2432,7 @@ let ShipAidWidget = class extends s$1 {
2368
2432
  useEffect(
2369
2433
  this,
2370
2434
  async () => {
2371
- var _a, _b, _c;
2435
+ var _a, _b, _c, _d;
2372
2436
  this._cartLastUpdated = /* @__PURE__ */ new Date();
2373
2437
  if (!((_a = this._cart) == null ? void 0 : _a.items))
2374
2438
  return;
@@ -2380,7 +2444,10 @@ let ShipAidWidget = class extends s$1 {
2380
2444
  });
2381
2445
  const protectionCartItem = (_c = this._cart) == null ? void 0 : _c.items[protectionCartItemIndex];
2382
2446
  this._hasProtectionInCart = !!protectionCartItem;
2383
- if (this._cart.item_count === 1 && !!protectionCartItem) {
2447
+ if (!this._store)
2448
+ return;
2449
+ const protectionFee = await this.calculateProtectionTotal(this._cart);
2450
+ if (this._cart.item_count > 0 && !!protectionCartItem && (this._cart.total_price === protectionCartItem.final_line_price || !protectionFee)) {
2384
2451
  const removePayload = {
2385
2452
  id: protectionCartItem.key,
2386
2453
  quantity: 0
@@ -2392,18 +2459,45 @@ let ShipAidWidget = class extends s$1 {
2392
2459
  sessionStorage.removeItem(LOCAL_STORAGE_KEY);
2393
2460
  return await this._handleRefresh(cart2);
2394
2461
  }
2395
- if (!this._store)
2396
- return;
2397
- const protectionFee = await this.calculateProtectionTotal(this._cart);
2398
2462
  const protectionVariant = this._findProtectionVariant(protectionFee);
2399
- this._protectionVariant = protectionVariant;
2463
+ if (!protectionFee) {
2464
+ this._protectionVariant = {
2465
+ id: 0,
2466
+ price: "0"
2467
+ };
2468
+ } else {
2469
+ this._protectionVariant = protectionVariant;
2470
+ this._shouldShowWidget = true;
2471
+ }
2400
2472
  if (!(protectionVariant == null ? void 0 : protectionVariant.id)) {
2401
2473
  this._shouldShowWidget = false;
2402
2474
  logger.error("No matching protection variant found.");
2403
2475
  return;
2404
2476
  }
2477
+ if (!((_d = this._protectionVariant) == null ? void 0 : _d.id)) {
2478
+ this._shouldShowWidget = false;
2479
+ return;
2480
+ }
2405
2481
  if (!protectionCartItem)
2406
2482
  return;
2483
+ if (this.supportSubscriptions) {
2484
+ const hasSubscriptions = this._cart.items.some((item) => {
2485
+ var _a2;
2486
+ return item.id !== ((_a2 = this._protectionVariant) == null ? void 0 : _a2.id) && !!(item == null ? void 0 : item.selling_plan_allocation);
2487
+ });
2488
+ let updateSubscriptionPayload = null;
2489
+ if (!hasSubscriptions && (protectionCartItem == null ? void 0 : protectionCartItem.selling_plan_allocation)) {
2490
+ updateSubscriptionPayload = { id: protectionCartItem.key, quantity: 1, selling_plan: null };
2491
+ } else if (hasSubscriptions && !(protectionCartItem == null ? void 0 : protectionCartItem.selling_plan_allocation)) {
2492
+ const protectionSellingPlan = await this._fetchSellingPlanFromVariant();
2493
+ const sellingPlanId = protectionSellingPlan ? getIdFromShopifyGid(protectionSellingPlan.id) : null;
2494
+ updateSubscriptionPayload = { id: protectionCartItem.key, quantity: 1, selling_plan: sellingPlanId };
2495
+ }
2496
+ if (updateSubscriptionPayload) {
2497
+ const cart2 = await this._fetch.post("/cart/change.js", updateSubscriptionPayload);
2498
+ await this._handleRefresh(cart2);
2499
+ }
2500
+ }
2407
2501
  if (protectionVariant.id === protectionCartItem.variant_id) {
2408
2502
  this._protectionCartItem = {
2409
2503
  ...protectionCartItem,
@@ -2544,7 +2638,7 @@ let ShipAidWidget = class extends s$1 {
2544
2638
  .prompt-product-actions
2545
2639
  .prompt-product-actions-price {
2546
2640
  color: var(--shipaid-prompt-actions-price-color, var(--shipaid-text-muted));
2547
- font-size: var(--shipaid-font-base);
2641
+ font-size: var(--shipaid-prompt-actions-price-fontSize, --shipaid-font-base);
2548
2642
  }
2549
2643
  .shipaid-prompt
2550
2644
  .prompt-product
@@ -2688,6 +2782,9 @@ __decorateClass([
2688
2782
  __decorateClass([
2689
2783
  n$7({ type: String, attribute: true })
2690
2784
  ], ShipAidWidget.prototype, "customerId", 2);
2785
+ __decorateClass([
2786
+ n$7({ type: Boolean, attribute: true })
2787
+ ], ShipAidWidget.prototype, "supportSubscriptions", 2);
2691
2788
  __decorateClass([
2692
2789
  t$3()
2693
2790
  ], ShipAidWidget.prototype, "_storeDomain", 2);