@redotech/redo-hydrogen 1.0.2 → 1.1.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.
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CartReturn, Storefront } from '@shopify/hydrogen';
1
+ import { CartReturn } from '@shopify/hydrogen';
2
2
  import { ReactNode, DependencyList } from 'react';
3
3
  import { ProductVariant } from '@shopify/hydrogen-react/storefront-api-types';
4
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
@@ -11,6 +11,8 @@ interface RedoCoverageClient {
11
11
  get loading(): boolean;
12
12
  get enabled(): boolean;
13
13
  get price(): number;
14
+ get storeId(): string | undefined;
15
+ get cart(): CartReturn | undefined;
14
16
  get cartProduct(): CartProductVariantFragment | undefined;
15
17
  get cartAttribute(): CartAttributeKey | undefined;
16
18
  }
@@ -37,8 +39,6 @@ declare const useRedoCoverageClient: () => RedoCoverageClient;
37
39
 
38
40
  declare const RedoCheckoutButtons: (props: {
39
41
  cart: CartReturn;
40
- storefront: Storefront;
41
- storeId: string;
42
42
  children?: ReactNode;
43
43
  onClick?: (enabled: boolean) => void;
44
44
  }) => react_jsx_runtime.JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redotech/redo-hydrogen",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Utilities to enable and disable Redo coverage on Hydrogen stores",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -3,7 +3,6 @@ import {
3
3
  CartForm,
4
4
  CartActionInput,
5
5
  CartReturn,
6
- Storefront,
7
6
  } from "@shopify/hydrogen";
8
7
  import { useRedoCoverageClient } from "../providers/redo-coverage-client";
9
8
  import { CartInfoToEnable, RedoCoverageClient } from "../types";
@@ -89,14 +88,12 @@ const findAncestor = (
89
88
 
90
89
  const RedoCheckoutButtons = (props: {
91
90
  cart: CartReturn;
92
- storefront: Storefront;
93
- storeId: string;
94
91
  children?: ReactNode;
95
92
  onClick?: (enabled: boolean) => void;
96
93
  }) => {
97
94
  const redoCoverageClient = useRedoCoverageClient();
98
- let cart = props.cart;
99
- let checkoutUrl = cart.checkoutUrl;
95
+ let cart = redoCoverageClient.cart;
96
+ let checkoutUrl = redoCoverageClient.cart?.checkoutUrl || '/checkout';
100
97
  let [redoProductToAdd, setRedoProductToAdd] =
101
98
  useState<CartInfoToEnable | null>(null);
102
99
  let [checkoutButtonsUI, setCheckoutButtonsUI] = useState<CheckoutButtonUIResponse | null>(
@@ -105,12 +102,16 @@ const RedoCheckoutButtons = (props: {
105
102
 
106
103
  useEffect(() => {
107
104
  (async () => {
108
- const buttons = await getButtonsToShow({ redoCoverageClient, cart, storeId: props.storeId });
105
+ if(!redoCoverageClient.storeId || !cart) {
106
+ return;
107
+ }
108
+
109
+ const buttons = await getButtonsToShow({ redoCoverageClient, cart, storeId: redoCoverageClient.storeId });
109
110
  if(buttons) {
110
111
  setCheckoutButtonsUI(buttons);
111
112
  }
112
113
  })();
113
- }, [cart, redoCoverageClient.price]);
114
+ }, [cart, redoCoverageClient.price, redoCoverageClient.storeId]);
114
115
 
115
116
  const wrapperClickHandler = async (e: MouseEvent) => {
116
117
  let clickedElement = e.target as HTMLElement;
@@ -10,7 +10,7 @@ const DEFAULT_REDO_CONTEXT_VALUE: RedoContextValue = {
10
10
  loading: true,
11
11
  }
12
12
 
13
- const RedoContext = createContext(DEFAULT_REDO_CONTEXT_VALUE);
13
+ const RedoContext = createContext<RedoContextValue>(DEFAULT_REDO_CONTEXT_VALUE);
14
14
 
15
15
  const RedoProvider = ({
16
16
  cart,
@@ -27,6 +27,10 @@ const RedoProvider = ({
27
27
  const [loading, setLoading] = useState<boolean>(true);
28
28
 
29
29
  useEffect(() => {
30
+ if(!cart) {
31
+ return;
32
+ }
33
+
30
34
  fetch(`https://${REDO_PUBLIC_API_HOSTNAME}/v2.2/stores/${storeId}/coverage-products`, {
31
35
  method: 'POST',
32
36
  headers: {
@@ -68,6 +72,10 @@ const RedoProvider = ({
68
72
 
69
73
  setLoading(false);
70
74
 
75
+ if(!json?.coverageProducts?.[0]?.cartInfoToEnable) {
76
+ return;
77
+ }
78
+
71
79
  setCartInfoToEnable(json.coverageProducts[0].cartInfoToEnable);
72
80
  })
73
81
  }, [cart]);
@@ -144,11 +152,17 @@ const useRedoCoverageClient = (): RedoCoverageClient => {
144
152
  get price() {
145
153
  return Number(redoContext.cartInfoToEnable?.selectedVariant.price.amount);
146
154
  },
155
+ get cart() {
156
+ return redoContext.cart;
157
+ },
147
158
  get cartProduct() {
148
159
  return redoContext.cartInfoToEnable?.selectedVariant;
149
160
  },
150
161
  get cartAttribute() {
151
162
  return redoContext.cartInfoToEnable?.cartAttribute
163
+ },
164
+ get storeId() {
165
+ return redoContext.storeId;
152
166
  }
153
167
  }
154
168
  };
package/src/types.ts CHANGED
@@ -13,6 +13,8 @@ interface RedoCoverageClient {
13
13
  get loading(): boolean;
14
14
  get enabled(): boolean;
15
15
  get price(): number;
16
+ get storeId(): string | undefined;
17
+ get cart(): CartReturn | undefined;
16
18
  get cartProduct(): CartProductVariantFragment | undefined
17
19
  get cartAttribute(): CartAttributeKey | undefined
18
20
  }
package/src/utils/cart.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { FetcherWithComponents, useFetcher } from "@remix-run/react";
2
2
  import { CartInfoToEnable } from "../types";
3
3
  import { CartForm, CartReturn } from "@shopify/hydrogen";
4
- import { CartLine } from "@shopify/hydrogen-react/storefront-api-types";
5
4
  import type { AppData } from '@remix-run/react/dist/data';
6
5
  import React from 'react'
7
6