@rebuy/rebuy-hydrogen 2.3.1 → 3.0.0-alpha.1

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.
Files changed (162) hide show
  1. package/README.md +195 -17
  2. package/dist/assets/Close.d.ts +2 -0
  3. package/dist/assets/Close.d.ts.map +1 -0
  4. package/dist/components/AddToCartBtn/AddToCartBtn.d.ts +3 -0
  5. package/dist/components/AddToCartBtn/AddToCartBtn.d.ts.map +1 -0
  6. package/dist/components/AddToCartBtn/HydrogenAddToCartBtn.d.ts +3 -0
  7. package/dist/components/AddToCartBtn/HydrogenAddToCartBtn.d.ts.map +1 -0
  8. package/dist/components/AddToCartBtn/HydrogenReactAddToCartBtn.d.ts +3 -0
  9. package/dist/components/AddToCartBtn/HydrogenReactAddToCartBtn.d.ts.map +1 -0
  10. package/dist/components/AddToCartBtn/index.d.ts +2 -0
  11. package/dist/components/AddToCartBtn/index.d.ts.map +1 -0
  12. package/dist/components/AddToCartBtn/types.d.ts +27 -0
  13. package/dist/components/AddToCartBtn/types.d.ts.map +1 -0
  14. package/dist/components/ProductCard/ProductCard.d.ts +3 -0
  15. package/dist/components/ProductCard/ProductCard.d.ts.map +1 -0
  16. package/dist/components/ProductCard/defaultSettings.d.ts +3 -0
  17. package/dist/components/ProductCard/defaultSettings.d.ts.map +1 -0
  18. package/dist/components/ProductCard/index.d.ts +3 -0
  19. package/dist/components/ProductCard/index.d.ts.map +1 -0
  20. package/dist/components/ProductCard/types.d.ts +52 -0
  21. package/dist/components/ProductCard/types.d.ts.map +1 -0
  22. package/dist/components/ProductCarousel/ProductCarousel.d.ts +16 -0
  23. package/dist/components/ProductCarousel/ProductCarousel.d.ts.map +1 -0
  24. package/dist/components/ProductCarousel/index.d.ts +2 -0
  25. package/dist/components/ProductCarousel/index.d.ts.map +1 -0
  26. package/dist/components/ProductPrice/ProductPrice.d.ts +3 -0
  27. package/dist/components/ProductPrice/ProductPrice.d.ts.map +1 -0
  28. package/dist/components/ProductPrice/index.d.ts +2 -0
  29. package/dist/components/ProductPrice/index.d.ts.map +1 -0
  30. package/dist/components/ProductPrice/types.d.ts +23 -0
  31. package/dist/components/ProductPrice/types.d.ts.map +1 -0
  32. package/dist/components/QuantityInput/QuantityInput.d.ts +3 -0
  33. package/dist/components/QuantityInput/QuantityInput.d.ts.map +1 -0
  34. package/dist/components/QuantityInput/index.d.ts +3 -0
  35. package/dist/components/QuantityInput/index.d.ts.map +1 -0
  36. package/dist/components/QuantityInput/types.d.ts +7 -0
  37. package/dist/components/QuantityInput/types.d.ts.map +1 -0
  38. package/dist/components/Timer/Timer.d.ts +3 -0
  39. package/dist/components/Timer/Timer.d.ts.map +1 -0
  40. package/dist/components/Timer/index.d.ts +3 -0
  41. package/dist/components/Timer/index.d.ts.map +1 -0
  42. package/dist/components/Timer/types.d.ts +16 -0
  43. package/dist/components/Timer/types.d.ts.map +1 -0
  44. package/dist/components/Title/Title.d.ts +3 -0
  45. package/dist/components/Title/Title.d.ts.map +1 -0
  46. package/dist/components/Title/index.d.ts +2 -0
  47. package/dist/components/Title/index.d.ts.map +1 -0
  48. package/dist/components/Title/types.d.ts +8 -0
  49. package/dist/components/Title/types.d.ts.map +1 -0
  50. package/dist/components/VariantSelect/VariantSelect.d.ts +3 -0
  51. package/dist/components/VariantSelect/VariantSelect.d.ts.map +1 -0
  52. package/dist/components/VariantSelect/index.d.ts +2 -0
  53. package/dist/components/VariantSelect/index.d.ts.map +1 -0
  54. package/dist/components/VariantSelect/types.d.ts +8 -0
  55. package/dist/components/VariantSelect/types.d.ts.map +1 -0
  56. package/dist/constants/api.d.ts +2 -0
  57. package/dist/constants/api.d.ts.map +1 -0
  58. package/dist/context/RebuyConfigContext.d.ts +21 -0
  59. package/dist/context/RebuyConfigContext.d.ts.map +1 -0
  60. package/dist/context/RebuyContext.d.ts +6 -0
  61. package/dist/context/RebuyContext.d.ts.map +1 -0
  62. package/dist/hooks/titleLevel.d.ts +3 -0
  63. package/dist/hooks/titleLevel.d.ts.map +1 -0
  64. package/dist/hooks/useBreakpoint.d.ts +5 -0
  65. package/dist/hooks/useBreakpoint.d.ts.map +1 -0
  66. package/dist/hooks/usePopupTrigger.d.ts +3 -0
  67. package/dist/hooks/usePopupTrigger.d.ts.map +1 -0
  68. package/dist/hooks/useRebuyTheme.d.ts +21 -0
  69. package/dist/hooks/useRebuyTheme.d.ts.map +1 -0
  70. package/dist/index.css +853 -0
  71. package/dist/index.css.map +7 -0
  72. package/dist/index.d.ts +14 -0
  73. package/dist/index.d.ts.map +1 -0
  74. package/dist/index.js +3211 -0
  75. package/dist/index.js.map +7 -0
  76. package/dist/index.mjs +3178 -0
  77. package/dist/index.mjs.map +7 -0
  78. package/dist/providers/RebuyHydrogenContextProvider.d.ts +3 -0
  79. package/dist/providers/RebuyHydrogenContextProvider.d.ts.map +1 -0
  80. package/dist/providers/RebuyHydrogenReactContextProvider.d.ts +8 -0
  81. package/dist/providers/RebuyHydrogenReactContextProvider.d.ts.map +1 -0
  82. package/dist/providers/types.d.ts +71 -0
  83. package/dist/providers/types.d.ts.map +1 -0
  84. package/dist/queries/cart.queries.d.ts +5 -0
  85. package/dist/queries/cart.queries.d.ts.map +1 -0
  86. package/dist/types/common.d.ts +9 -0
  87. package/dist/types/common.d.ts.map +1 -0
  88. package/dist/types/rebuyCustom.d.ts +275 -0
  89. package/dist/types/rebuyCustom.d.ts.map +1 -0
  90. package/dist/types/rebuySmartCart.d.ts +184 -0
  91. package/dist/types/rebuySmartCart.d.ts.map +1 -0
  92. package/dist/types/shopify.d.ts +85 -0
  93. package/dist/types/shopify.d.ts.map +1 -0
  94. package/dist/types/widgets.d.ts +23 -0
  95. package/dist/types/widgets.d.ts.map +1 -0
  96. package/dist/utils/convertToRebuyProduct.d.ts +156 -0
  97. package/dist/utils/convertToRebuyProduct.d.ts.map +1 -0
  98. package/dist/utils/createContextParameters.d.ts +3 -0
  99. package/dist/utils/createContextParameters.d.ts.map +1 -0
  100. package/dist/utils/csp.d.ts +16 -0
  101. package/dist/utils/csp.d.ts.map +1 -0
  102. package/dist/utils/getEncodedAttributes.d.ts +3 -0
  103. package/dist/utils/getEncodedAttributes.d.ts.map +1 -0
  104. package/dist/utils/getRebuyConfig.d.ts +3 -0
  105. package/dist/utils/getRebuyConfig.d.ts.map +1 -0
  106. package/dist/utils/theme.d.ts +20 -0
  107. package/dist/utils/theme.d.ts.map +1 -0
  108. package/dist/widgetContainer/RebuyWidgetContainer.d.ts +8 -0
  109. package/dist/widgetContainer/RebuyWidgetContainer.d.ts.map +1 -0
  110. package/dist/widgets/RebuyCompleteTheLook/RebuyCompleteTheLook.d.ts +3 -0
  111. package/dist/widgets/RebuyCompleteTheLook/RebuyCompleteTheLook.d.ts.map +1 -0
  112. package/dist/widgets/RebuyCompleteTheLook/index.d.ts +2 -0
  113. package/dist/widgets/RebuyCompleteTheLook/index.d.ts.map +1 -0
  114. package/dist/widgets/RebuyCompleteTheLook/types.d.ts +5 -0
  115. package/dist/widgets/RebuyCompleteTheLook/types.d.ts.map +1 -0
  116. package/dist/widgets/RebuyDynamicBundleProducts/BundleImages.d.ts +5 -0
  117. package/dist/widgets/RebuyDynamicBundleProducts/BundleImages.d.ts.map +1 -0
  118. package/dist/widgets/RebuyDynamicBundleProducts/BundlePrice.d.ts +5 -0
  119. package/dist/widgets/RebuyDynamicBundleProducts/BundlePrice.d.ts.map +1 -0
  120. package/dist/widgets/RebuyDynamicBundleProducts/BundleSelection.d.ts +3 -0
  121. package/dist/widgets/RebuyDynamicBundleProducts/BundleSelection.d.ts.map +1 -0
  122. package/dist/widgets/RebuyDynamicBundleProducts/RebuyDynamicBundleProducts.d.ts +3 -0
  123. package/dist/widgets/RebuyDynamicBundleProducts/RebuyDynamicBundleProducts.d.ts.map +1 -0
  124. package/dist/widgets/RebuyDynamicBundleProducts/Select.d.ts +3 -0
  125. package/dist/widgets/RebuyDynamicBundleProducts/Select.d.ts.map +1 -0
  126. package/dist/widgets/RebuyDynamicBundleProducts/index.d.ts +2 -0
  127. package/dist/widgets/RebuyDynamicBundleProducts/index.d.ts.map +1 -0
  128. package/dist/widgets/RebuyDynamicBundleProducts/types.d.ts +19 -0
  129. package/dist/widgets/RebuyDynamicBundleProducts/types.d.ts.map +1 -0
  130. package/dist/widgets/RebuyProductAddOns/RebuyProductAddOnCard.d.ts +3 -0
  131. package/dist/widgets/RebuyProductAddOns/RebuyProductAddOnCard.d.ts.map +1 -0
  132. package/dist/widgets/RebuyProductAddOns/RebuyProductAddOns.d.ts +3 -0
  133. package/dist/widgets/RebuyProductAddOns/RebuyProductAddOns.d.ts.map +1 -0
  134. package/dist/widgets/RebuyProductAddOns/index.d.ts +2 -0
  135. package/dist/widgets/RebuyProductAddOns/index.d.ts.map +1 -0
  136. package/dist/widgets/RebuyProductAddOns/types.d.ts +19 -0
  137. package/dist/widgets/RebuyProductAddOns/types.d.ts.map +1 -0
  138. package/dist/widgets/RebuyProductRecommendations/RebuyProductRecommendations.d.ts +3 -0
  139. package/dist/widgets/RebuyProductRecommendations/RebuyProductRecommendations.d.ts.map +1 -0
  140. package/dist/widgets/RebuyProductRecommendations/index.d.ts +2 -0
  141. package/dist/widgets/RebuyProductRecommendations/index.d.ts.map +1 -0
  142. package/dist/widgets/RebuyProductRecommendations/types.d.ts +5 -0
  143. package/dist/widgets/RebuyProductRecommendations/types.d.ts.map +1 -0
  144. package/dist/widgets/RebuyWidget/RebuyWidget.d.ts +3 -0
  145. package/dist/widgets/RebuyWidget/RebuyWidget.d.ts.map +1 -0
  146. package/dist/widgets/RebuyWidget/WidgetContent.d.ts +3 -0
  147. package/dist/widgets/RebuyWidget/WidgetContent.d.ts.map +1 -0
  148. package/dist/widgets/RebuyWidget/index.d.ts +2 -0
  149. package/dist/widgets/RebuyWidget/index.d.ts.map +1 -0
  150. package/dist/widgets/RebuyWidget/types.d.ts +335 -0
  151. package/dist/widgets/RebuyWidget/types.d.ts.map +1 -0
  152. package/package.json +86 -47
  153. package/RebuyCompleteTheLook.client.jsx +0 -188
  154. package/RebuyContextProvider.client.jsx +0 -222
  155. package/RebuyContexts.client.jsx +0 -3
  156. package/RebuyDynamicBundleProducts.client.jsx +0 -415
  157. package/RebuyProductAddOnCard.client.jsx +0 -89
  158. package/RebuyProductAddOns.client.jsx +0 -227
  159. package/RebuyProductRecommendations.client.jsx +0 -68
  160. package/RebuyProductViewed.client.jsx +0 -62
  161. package/RebuyRecentlyViewedProducts.client.jsx +0 -68
  162. package/RebuyWidgetContainer.client.jsx +0 -136
@@ -1,227 +0,0 @@
1
- import {
2
- Money,
3
- ProductOptionsProvider,
4
- useCart,
5
- useProductOptions,
6
- } from '@shopify/hydrogen';
7
- import clsx from 'clsx';
8
- import { useCallback, useEffect, useMemo, useState } from 'react';
9
- import { Heading, Text } from '~/components';
10
- import { Button } from '~/components/elements';
11
- import { RebuyProductAddOnCard } from './RebuyProductAddOnCard.client';
12
-
13
- export const RebuyProductAddOns = ({
14
- product = {},
15
- products = [],
16
- metadata = {},
17
- title = `Popular Add-Ons for ${product.title}`,
18
- className = '',
19
- }) => {
20
- // Hooks
21
- const { linesAdd } = useCart();
22
- const { selectedVariant } = useProductOptions();
23
-
24
- // State
25
- const [addedItems, setAddedItems] = useState([]);
26
- const [totalMoney, _setTotalMoney] = useState(selectedVariant.priceV2);
27
- const isOutOfStock = !selectedVariant.availableForSale;
28
- const language = metadata.widget?.language ?? {};
29
- const componentTitle = language?.title || title;
30
- const styles = clsx('grid gap-4 py-6 md:py-8 lg:py-12', className);
31
-
32
- const AddToCartMarkup = ({ items, money, selectedVariant }) => {
33
- return (
34
- <>
35
- <Button
36
- onClick={() => addLineItemsToCart(items, selectedVariant)}
37
- variant="primary"
38
- as="button"
39
- >
40
- <Text
41
- as="span"
42
- className="flex items-center justify-center gap-2"
43
- >
44
- <span>Add to bag</span> <span>·</span>{' '}
45
- <Money withoutTrailingZeros data={money} />
46
- </Text>
47
- </Button>
48
- </>
49
- );
50
- };
51
-
52
- const addLineItemsToCart = useCallback(
53
- (items, selectedVariant) => {
54
- const lineItemsToAdd = items.map((item) => ({
55
- quantity: 1,
56
- merchandiseId: item.variantId,
57
- attributes: [
58
- { key: '_source', value: 'Rebuy' },
59
- { key: '_attribution', value: 'Rebuy Product AddOn' },
60
- ],
61
- }));
62
-
63
- // Add the selected variant of the main product
64
- lineItemsToAdd.push({
65
- quantity: 1,
66
- merchandiseId: selectedVariant.id,
67
- });
68
-
69
- // Add additional cart lines
70
- linesAdd(lineItemsToAdd);
71
-
72
- return;
73
- },
74
- [linesAdd]
75
- );
76
-
77
- // reference object for line data
78
- const refItems = useMemo(() => {
79
- const items = {};
80
-
81
- products.forEach((product) => {
82
- // NOTE: More work must be done if the add-ons will have variant selectors of their own
83
- const singleVariant = product.variants?.nodes[0];
84
-
85
- items[product.id] = {
86
- id: product.id,
87
- variantId: singleVariant.id,
88
- moneyPrice: singleVariant.priceV2, // full MoneyV2 object
89
- };
90
- });
91
-
92
- return items;
93
- }, [products]);
94
-
95
- const setTotalMoney = useCallback(
96
- (items) => {
97
- // get base product price
98
- const newTotalMoney = { ...selectedVariant.priceV2 };
99
-
100
- if (items.length === 0) {
101
- _setTotalMoney(newTotalMoney);
102
- return;
103
- }
104
-
105
- // cast amount to number for calculation
106
- let totalAmount = Number(newTotalMoney.amount);
107
- // sum up the prices of the added items
108
- items.forEach((item) => {
109
- totalAmount += Number(item.moneyPrice.amount);
110
- });
111
-
112
- // cast amount back to string for MoneyV2
113
- newTotalMoney.amount = String(totalAmount);
114
-
115
- _setTotalMoney(newTotalMoney);
116
- return;
117
- },
118
- [selectedVariant]
119
- );
120
-
121
- // listen for selected variant change from parent
122
- useEffect(() => {
123
- setTotalMoney(addedItems);
124
- }, [addedItems, setTotalMoney]);
125
-
126
- const handleChange = useCallback(
127
- (event, product) => {
128
- // const mainPrice = Number(selectedVariant.priceV2.amount);
129
- let newAddedItems = [...addedItems];
130
- if (event.target.checked) {
131
- newAddedItems = [...addedItems, refItems[product.id]];
132
- } else {
133
- newAddedItems = addedItems.filter(
134
- (item) => item.id !== product.id
135
- );
136
- }
137
-
138
- setAddedItems(newAddedItems);
139
- setTotalMoney(newAddedItems);
140
- },
141
- [addedItems, refItems, setTotalMoney]
142
- );
143
-
144
- return (
145
- /* Render Product AddOns with AddToCart Button */
146
- product &&
147
- products.length > 0 &&
148
- !isOutOfStock && (
149
- <div className={styles}>
150
- <Heading size="lead" className="px-6 md:px-8 lg:px-12">
151
- {componentTitle}
152
- </Heading>
153
- {/* Product Add-Ons */}
154
- <ul className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2 px-6 md:px-8 lg:px-12">
155
- {products.map((product) => (
156
- <li key={product.id}>
157
- <ProductOptionsProvider data={product}>
158
- <div className="mt-2">
159
- <label
160
- htmlFor={product.id}
161
- className="cursor-pointer flex items-center hover:bg-slate-200 p-2 gap-2"
162
- >
163
- <input
164
- type="checkbox"
165
- value=""
166
- disabled={
167
- !product.variants.nodes[0]
168
- .availableForSale
169
- }
170
- name={product.title}
171
- id={product.id}
172
- checked={
173
- addedItems.filter(
174
- (item) =>
175
- item.id === product.id
176
- ).length > 0
177
- }
178
- className="accent-black rounded-sm cursor-pointer"
179
- onChange={(event) =>
180
- handleChange(event, product)
181
- }
182
- />
183
- <RebuyProductAddOnCard
184
- product={product}
185
- />
186
- </label>
187
- </div>
188
- </ProductOptionsProvider>
189
- </li>
190
- ))}
191
- </ul>
192
-
193
- <div className="px-6 md:px-8 lg:px-12">
194
- {/* Subtotal */}
195
- {Number(totalMoney.amount) >
196
- Number(selectedVariant.priceV2.amount) && (
197
- <Text
198
- as="div"
199
- className="flex gap-1 pb-4 text-gray-600"
200
- >
201
- Subtotal:{' '}
202
- <Money
203
- data={{
204
- ...totalMoney,
205
- amount: String(
206
- totalMoney.amount -
207
- selectedVariant.priceV2.amount
208
- ),
209
- }}
210
- withoutTrailingZeros
211
- />
212
- </Text>
213
- )}
214
-
215
- {/* AddToCart Button */}
216
- <AddToCartMarkup
217
- items={addedItems}
218
- money={totalMoney}
219
- selectedVariant={selectedVariant}
220
- />
221
- </div>
222
- </div>
223
- )
224
- );
225
- };
226
-
227
- export default RebuyProductAddOns;
@@ -1,68 +0,0 @@
1
- import { AddToCartButton, ProductOptionsProvider } from '@shopify/hydrogen';
2
- import { Section } from '~/components';
3
- import { ProductCard } from '~/components/cards';
4
- import { Button } from '~/components/elements';
5
-
6
- const AddToCartMarkup = ({ product }) => {
7
- const selectedVariant = product.variants.nodes[0];
8
- const isOutOfStock = !selectedVariant.availableForSale;
9
-
10
- return (
11
- <div className="py-2">
12
- <AddToCartButton
13
- disabled={isOutOfStock}
14
- product={product}
15
- variantId={selectedVariant?.id}
16
- quantity={1}
17
- accessibleAddingToCartLabel="Adding item to your cart"
18
- type="button"
19
- attributes={[
20
- { key: '_source', value: 'Rebuy' },
21
- {
22
- key: '_attribution',
23
- value: 'Rebuy Product Recommendations',
24
- },
25
- ]}
26
- >
27
- <Button
28
- width="full"
29
- variant={isOutOfStock ? 'secondary' : 'primary'}
30
- as="span"
31
- >
32
- {isOutOfStock ? 'Out of stock' : 'Add to bag'}
33
- </Button>
34
- </AddToCartButton>
35
- </div>
36
- );
37
- };
38
-
39
- export const RebuyProductRecommendations = ({
40
- product,
41
- products,
42
- title = `Like ${product.title}? You may also like:`,
43
- }) => {
44
- return (
45
- products.length > 0 && (
46
- <Section heading={title} padding="y">
47
- <ul className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 px-6 md:px-8 lg:px-12">
48
- {products.map((product) => (
49
- <li key={product.id}>
50
- <ProductOptionsProvider
51
- data={product}
52
- initialVariantId={product.variants.nodes[0].id}
53
- >
54
- <ProductCard
55
- product={product}
56
- className={'w-100'}
57
- />
58
- <AddToCartMarkup product={product} />
59
- </ProductOptionsProvider>
60
- </li>
61
- ))}
62
- </ul>
63
- </Section>
64
- )
65
- );
66
- };
67
-
68
- export default RebuyProductRecommendations;
@@ -1,62 +0,0 @@
1
- import { RebuyClient } from '@rebuy/rebuy';
2
- import * as Utilities from '@rebuy/rebuy/utilities';
3
- import { useEffect, useMemo, useState } from 'react';
4
-
5
- const REBUY_API_KEY = import.meta.env.PUBLIC_REBUY_API_KEY;
6
-
7
- export const RebuyProductViewed = ({ children, ...props }) => {
8
- const { product, productId, productHandle } = props;
9
-
10
- // eslint-disable-next-line no-unused-vars
11
- const [event, setEvent] = useState(null);
12
- const [Rebuy, setRebuy] = useState(null);
13
- const [initialized, setInitialized] = useState(false);
14
- const shopifyProductId = product?.id ?? productId ?? null;
15
-
16
- const request = useMemo(() => {
17
- const request = {
18
- parameters: {},
19
- };
20
-
21
- if (shopifyProductId) {
22
- request.parameters.shopify_product_id = Utilities.getIdFromGraphUrl(
23
- shopifyProductId,
24
- 'Product'
25
- );
26
- } else if (productHandle) {
27
- request.parameters.shopify_product_handle = productHandle;
28
- }
29
-
30
- return request;
31
- }, [productHandle, shopifyProductId]);
32
-
33
- useEffect(() => {
34
- const recordView = async () => {
35
- const response = await Rebuy.trackProductViewed(request.parameters);
36
-
37
- setEvent(response.data);
38
- };
39
-
40
- if (!initialized) {
41
- const client = new RebuyClient(REBUY_API_KEY);
42
-
43
- setRebuy(client);
44
- setInitialized(true);
45
-
46
- // Abort
47
- return;
48
- }
49
-
50
- // Initialized
51
- recordView();
52
- }, [Rebuy, request, initialized]);
53
-
54
- if (Object.keys(request.parameters).length === 0) {
55
- console.warn('No parameters!');
56
- return;
57
- }
58
-
59
- return children;
60
- };
61
-
62
- export default RebuyProductViewed;
@@ -1,68 +0,0 @@
1
- import { AddToCartButton, ProductOptionsProvider } from '@shopify/hydrogen';
2
- import { Section } from '~/components';
3
- import { ProductCard } from '~/components/cards';
4
- import { Button } from '~/components/elements';
5
-
6
- const AddToCartMarkup = ({ product }) => {
7
- const selectedVariant = product.variants.nodes[0];
8
- const isOutOfStock = !selectedVariant.availableForSale;
9
-
10
- return (
11
- <div className="py-2">
12
- <AddToCartButton
13
- disabled={isOutOfStock}
14
- product={product}
15
- variantId={selectedVariant?.id}
16
- quantity={1}
17
- accessibleAddingToCartLabel="Adding item to your cart"
18
- type="button"
19
- attributes={[
20
- { key: '_source', value: 'Rebuy' },
21
- {
22
- key: '_attribution',
23
- value: 'Rebuy Recently Viewed Product',
24
- },
25
- ]}
26
- >
27
- <Button
28
- width="full"
29
- variant={isOutOfStock ? 'secondary' : 'primary'}
30
- as="span"
31
- >
32
- {isOutOfStock ? 'Out of stock' : 'Add to bag'}
33
- </Button>
34
- </AddToCartButton>
35
- </div>
36
- );
37
- };
38
-
39
- export const RebuyRecentlyViewedProducts = ({ product, products }) => {
40
- const title = 'Recently Viewed';
41
- // Filter current product from recently viewed
42
- const filtered = products.filter((recent) => recent.id !== product.id);
43
-
44
- return (
45
- filtered.length > 0 && (
46
- <Section heading={title} padding="y">
47
- <ul className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 mb-16 px-6 md:px-8 lg:px-12">
48
- {filtered.map((product) => (
49
- <li key={product.id}>
50
- <ProductOptionsProvider
51
- data={product}
52
- initialVariantId={product.variants.nodes[0].id}
53
- >
54
- <ProductCard
55
- product={product}
56
- className={'w-100'}
57
- />
58
- <AddToCartMarkup product={product} />
59
- </ProductOptionsProvider>
60
- </li>
61
- ))}
62
- </ul>
63
- </Section>
64
- )
65
- );
66
- };
67
-
68
- export default RebuyRecentlyViewedProducts;
@@ -1,136 +0,0 @@
1
- import { RebuyClient } from '@rebuy/rebuy';
2
- import { RebuyContext } from '@rebuy/rebuy-hydrogen/RebuyContexts.client';
3
- import * as Utilities from '@rebuy/rebuy/utilities';
4
- import { flattenConnection, useCart } from '@shopify/hydrogen';
5
- import React, { useContext, useEffect, useMemo, useState } from 'react';
6
-
7
- const REBUY_API_KEY = import.meta.env.PUBLIC_REBUY_API_KEY;
8
-
9
- export const RebuyWidgetContainer = ({ children, ...props }) => {
10
- const {
11
- collection,
12
- collectionId,
13
- dataSource,
14
- limit,
15
- options,
16
- product,
17
- productId,
18
- variant,
19
- variantId,
20
- } = props;
21
-
22
- const cart = useCart();
23
- const { contextParameters } = useContext(RebuyContext);
24
- const [products, setProducts] = useState([]);
25
- const [metadata, setMetadata] = useState({});
26
- const [Rebuy, setRebuy] = useState(null);
27
- const [initialized, setInitialized] = useState(false);
28
- const isCartReady = ['uninitialized', 'idle'].includes(cart.status); // uninitialized, creating, fetching, updating, idle
29
- const shopifyProductId = product?.id ?? productId ?? null;
30
- const shopifyVariantId = variant?.id ?? variantId ?? null;
31
- const shopifyCollectionId = collection?.id ?? collectionId ?? null;
32
-
33
- // Initialize Rebuy API client
34
- useEffect(() => {
35
- if (!Rebuy) {
36
- const client = new RebuyClient(REBUY_API_KEY);
37
- client.setContextParameters(contextParameters);
38
-
39
- if (options) {
40
- client.setDefaultParameters(options);
41
- }
42
-
43
- setRebuy(client);
44
- setInitialized(true);
45
- }
46
- }, [Rebuy, contextParameters, options]);
47
-
48
- // Update context params
49
- useEffect(() => {
50
- if (!Rebuy) return;
51
-
52
- Rebuy.setContextParameters(contextParameters);
53
- }, [Rebuy, contextParameters]);
54
-
55
- // Memoize request object on prop changes (e.g. product)
56
- const request = useMemo(() => {
57
- const request = {
58
- url: dataSource || '/api/v1/products/recommended',
59
- parameters: {},
60
- };
61
-
62
- if (shopifyProductId) {
63
- request.parameters.shopify_product_ids =
64
- Utilities.getIdFromGraphUrl(shopifyProductId, 'Product');
65
- }
66
-
67
- if (shopifyVariantId) {
68
- request.parameters.shopify_variant_ids =
69
- Utilities.getIdFromGraphUrl(shopifyVariantId, 'ProductVariant');
70
- }
71
-
72
- if (shopifyCollectionId) {
73
- request.parameters.shopify_collection_ids =
74
- Utilities.getIdFromGraphUrl(shopifyCollectionId, 'Collection');
75
- }
76
-
77
- if (limit) {
78
- request.parameters.limit = limit;
79
- }
80
-
81
- return request;
82
- }, [
83
- dataSource,
84
- limit,
85
- shopifyCollectionId,
86
- shopifyProductId,
87
- shopifyVariantId,
88
- ]);
89
-
90
- // Update product recommendations on cart or request change
91
- useEffect(() => {
92
- let ignore = false;
93
-
94
- // Initializing...
95
- if (!initialized || !Rebuy || !isCartReady) return;
96
-
97
- const fetchData = async () => {
98
- const { data, metadata } = await Rebuy.getStorefrontData(
99
- request.url,
100
- request.parameters
101
- );
102
-
103
- data.forEach((product) => {
104
- product.variants = {
105
- nodes: flattenConnection(product.variants),
106
- };
107
- });
108
-
109
- // Set state only if mounted
110
- if (!ignore) {
111
- setProducts(data);
112
- setMetadata(metadata);
113
- }
114
- };
115
-
116
- fetchData();
117
-
118
- // Unmounted?
119
- return () => {
120
- ignore = true;
121
- };
122
- }, [Rebuy, initialized, request, isCartReady]);
123
-
124
- const childrenWithProps = (props) =>
125
- React.Children.map(children, (child) =>
126
- React.isValidElement(child)
127
- ? React.cloneElement(child, props)
128
- : child
129
- );
130
-
131
- const childProps = { ...props, products, metadata, key: product.id };
132
-
133
- return childrenWithProps(childProps);
134
- };
135
-
136
- export default RebuyWidgetContainer;