@gem-sdk/core 1.5.17 → 1.5.22
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/cjs/components/AddOn.js +7 -7
- package/dist/cjs/components/ComponentWrapper.js +14 -8
- package/dist/cjs/components/ComponentWrapperPreview.js +55 -46
- package/dist/cjs/components/Empty.js +1 -1
- package/dist/cjs/components/Render.js +50 -13
- package/dist/cjs/components/Render.liquid.js +81 -57
- package/dist/cjs/components/RenderBlock.js +41 -8
- package/dist/cjs/components/RenderPreview.js +51 -13
- package/dist/cjs/components/RenderSection.js +11 -7
- package/dist/cjs/components/constant.js +24 -24
- package/dist/cjs/contexts/AddonContext.js +25 -14
- package/dist/cjs/contexts/BuilderComponent.js +23 -19
- package/dist/cjs/contexts/BuilderContext.js +25 -19
- package/dist/cjs/contexts/BuilderPreviewContext.js +349 -341
- package/dist/cjs/contexts/CartLineContext.js +19 -14
- package/dist/cjs/contexts/CollectionContext.js +21 -13
- package/dist/cjs/contexts/ProductContext.js +136 -115
- package/dist/cjs/contexts/ProductListContext.js +21 -13
- package/dist/cjs/contexts/SectionContext.js +41 -37
- package/dist/cjs/contexts/ShopContext.js +49 -31
- package/dist/cjs/graphql/fragments/analytic.generated.js +1 -1
- package/dist/cjs/graphql/fragments/collection-detail.generated.js +1 -1
- package/dist/cjs/graphql/fragments/collection-product.generated.js +1 -1
- package/dist/cjs/graphql/fragments/custom-code.generated.js +1 -1
- package/dist/cjs/graphql/fragments/data-seo.generated.js +1 -1
- package/dist/cjs/graphql/fragments/media.generated.js +1 -1
- package/dist/cjs/graphql/fragments/product-little.generated.js +1 -1
- package/dist/cjs/graphql/fragments/product-option-value.generated.js +1 -1
- package/dist/cjs/graphql/fragments/product-option.generated.js +1 -1
- package/dist/cjs/graphql/fragments/product.generated.js +1 -1
- package/dist/cjs/graphql/fragments/published-custom-section.generated.js +1 -1
- package/dist/cjs/graphql/fragments/published-page-section.generated.js +1 -1
- package/dist/cjs/graphql/fragments/published-theme-page.generated.js +1 -1
- package/dist/cjs/graphql/fragments/published-theme-style.generated.js +1 -1
- package/dist/cjs/graphql/fragments/selected-option.generated.js +1 -1
- package/dist/cjs/graphql/fragments/variant.generated.js +1 -1
- package/dist/cjs/graphql/mutations/page-view-up.generated.js +1 -1
- package/dist/cjs/graphql/queries/products.generated.js +1 -1
- package/dist/cjs/graphql/queries/store-property.generated.js +1 -1
- package/dist/cjs/helpers/GlobalEvent.js +12 -10
- package/dist/cjs/helpers/background.js +96 -96
- package/dist/cjs/helpers/borders.js +123 -127
- package/dist/cjs/helpers/clone-deep.js +1 -1
- package/dist/cjs/helpers/cls.js +11 -15
- package/dist/cjs/helpers/colors.js +121 -156
- package/dist/cjs/helpers/compose-advance-style.js +115 -121
- package/dist/cjs/helpers/constant.js +16 -11
- package/dist/cjs/helpers/css-variable.js +2 -2
- package/dist/cjs/helpers/email.js +3 -3
- package/dist/cjs/helpers/flatten-connection.js +4 -5
- package/dist/cjs/helpers/get-resonsive-value.js +24 -25
- package/dist/cjs/helpers/get-shortname.js +87 -87
- package/dist/cjs/helpers/is-browser.js +2 -2
- package/dist/cjs/helpers/is-defined.js +2 -2
- package/dist/cjs/helpers/is-empty-children.js +2 -2
- package/dist/cjs/helpers/layout.js +9 -10
- package/dist/cjs/helpers/load-script.js +26 -27
- package/dist/cjs/helpers/make-style.js +77 -86
- package/dist/cjs/helpers/normalize-builder-data.js +18 -12
- package/dist/cjs/helpers/prefetch-queries.js +72 -52
- package/dist/cjs/helpers/product.js +25 -29
- package/dist/cjs/helpers/queries/get-collection.js +179 -167
- package/dist/cjs/helpers/queries/get-product-by-slug.js +103 -94
- package/dist/cjs/helpers/queries/get-product.js +121 -106
- package/dist/cjs/helpers/queries/get-products.js +137 -125
- package/dist/cjs/helpers/radius.js +78 -81
- package/dist/cjs/helpers/render.js +56 -56
- package/dist/cjs/helpers/shadow.js +38 -38
- package/dist/cjs/helpers/size.js +12 -13
- package/dist/cjs/helpers/spacing.js +35 -37
- package/dist/cjs/helpers/tracking/fpixel.js +21 -22
- package/dist/cjs/helpers/tracking/gtag.js +84 -90
- package/dist/cjs/helpers/tracking/tiktokpixel.js +12 -12
- package/dist/cjs/helpers/typography.js +2 -2
- package/dist/cjs/hooks/cart/use-add-to-cart.js +10 -10
- package/dist/cjs/hooks/cart/use-cart-data.js +18 -16
- package/dist/cjs/hooks/cart/use-cart-discount-codes-update.js +10 -10
- package/dist/cjs/hooks/cart/use-cart-note-update.js +10 -10
- package/dist/cjs/hooks/cart/use-create-cart.js +12 -12
- package/dist/cjs/hooks/cart/use-remove-cart-item.js +10 -10
- package/dist/cjs/hooks/cart/use-update-cart-item.js +10 -10
- package/dist/cjs/hooks/shop/use-collection-query.js +12 -6
- package/dist/cjs/hooks/shop/use-product-query.js +12 -6
- package/dist/cjs/hooks/shop/use-products-query.js +14 -6
- package/dist/cjs/hooks/shop.js +86 -67
- package/dist/cjs/hooks/use-current-device.js +14 -16
- package/dist/cjs/hooks/use-lazy-video.js +7 -7
- package/dist/cjs/hooks/useCartId.js +35 -27
- package/dist/cjs/hooks/useCartLine.js +2 -2
- package/dist/cjs/hooks/useCartUI.js +37 -31
- package/dist/cjs/hooks/useCollection.js +2 -2
- package/dist/cjs/hooks/useFetchHandle.js +6 -6
- package/dist/cjs/hooks/useFormatMoney.js +11 -7
- package/dist/cjs/hooks/useIsomorphicLayoutEffect.js +1 -1
- package/dist/cjs/hooks/useLoadScript.js +22 -20
- package/dist/cjs/hooks/useMoney.js +59 -42
- package/dist/cjs/hooks/usePrevious.js +12 -10
- package/dist/cjs/hooks/useProduct.js +92 -69
- package/dist/cjs/hooks/useProductList.js +11 -11
- package/dist/cjs/hooks/useSuspenseFetch.js +20 -17
- package/dist/cjs/hooks/useSwatchesOptions.js +32 -30
- package/dist/cjs/types/global-style.js +9 -2
- package/dist/esm/components/AddOn.js +7 -7
- package/dist/esm/components/ComponentWrapper.js +14 -8
- package/dist/esm/components/ComponentWrapperPreview.js +55 -46
- package/dist/esm/components/Empty.js +1 -1
- package/dist/esm/components/Render.js +50 -13
- package/dist/esm/components/Render.liquid.js +81 -57
- package/dist/esm/components/RenderBlock.js +41 -8
- package/dist/esm/components/RenderPreview.js +51 -13
- package/dist/esm/components/RenderSection.js +11 -7
- package/dist/esm/components/constant.js +24 -24
- package/dist/esm/contexts/AddonContext.js +26 -15
- package/dist/esm/contexts/BuilderComponent.js +24 -20
- package/dist/esm/contexts/BuilderContext.js +26 -20
- package/dist/esm/contexts/BuilderPreviewContext.js +350 -342
- package/dist/esm/contexts/CartLineContext.js +20 -15
- package/dist/esm/contexts/CollectionContext.js +22 -14
- package/dist/esm/contexts/ProductContext.js +137 -116
- package/dist/esm/contexts/ProductListContext.js +22 -14
- package/dist/esm/contexts/SectionContext.js +42 -38
- package/dist/esm/contexts/ShopContext.js +50 -32
- package/dist/esm/graphql/fragments/analytic.generated.js +1 -1
- package/dist/esm/graphql/fragments/collection-detail.generated.js +1 -1
- package/dist/esm/graphql/fragments/collection-product.generated.js +1 -1
- package/dist/esm/graphql/fragments/custom-code.generated.js +1 -1
- package/dist/esm/graphql/fragments/data-seo.generated.js +1 -1
- package/dist/esm/graphql/fragments/media.generated.js +1 -1
- package/dist/esm/graphql/fragments/product-little.generated.js +1 -1
- package/dist/esm/graphql/fragments/product-option-value.generated.js +1 -1
- package/dist/esm/graphql/fragments/product-option.generated.js +1 -1
- package/dist/esm/graphql/fragments/product.generated.js +1 -1
- package/dist/esm/graphql/fragments/published-custom-section.generated.js +1 -1
- package/dist/esm/graphql/fragments/published-page-section.generated.js +1 -1
- package/dist/esm/graphql/fragments/published-theme-page.generated.js +1 -1
- package/dist/esm/graphql/fragments/published-theme-style.generated.js +1 -1
- package/dist/esm/graphql/fragments/selected-option.generated.js +1 -1
- package/dist/esm/graphql/fragments/variant.generated.js +1 -1
- package/dist/esm/graphql/mutations/page-view-up.generated.js +1 -1
- package/dist/esm/graphql/queries/products.generated.js +1 -1
- package/dist/esm/graphql/queries/store-property.generated.js +1 -1
- package/dist/esm/helpers/GlobalEvent.js +12 -10
- package/dist/esm/helpers/background.js +96 -96
- package/dist/esm/helpers/borders.js +123 -127
- package/dist/esm/helpers/clone-deep.js +1 -1
- package/dist/esm/helpers/cls.js +11 -15
- package/dist/esm/helpers/colors.js +121 -156
- package/dist/esm/helpers/compose-advance-style.js +115 -121
- package/dist/esm/helpers/constant.js +16 -11
- package/dist/esm/helpers/css-variable.js +2 -2
- package/dist/esm/helpers/email.js +3 -3
- package/dist/esm/helpers/flatten-connection.js +4 -5
- package/dist/esm/helpers/get-resonsive-value.js +24 -25
- package/dist/esm/helpers/get-shortname.js +87 -87
- package/dist/esm/helpers/is-browser.js +2 -2
- package/dist/esm/helpers/is-defined.js +2 -2
- package/dist/esm/helpers/is-empty-children.js +2 -2
- package/dist/esm/helpers/layout.js +9 -10
- package/dist/esm/helpers/load-script.js +26 -27
- package/dist/esm/helpers/make-style.js +77 -86
- package/dist/esm/helpers/normalize-builder-data.js +18 -12
- package/dist/esm/helpers/prefetch-queries.js +72 -52
- package/dist/esm/helpers/product.js +25 -29
- package/dist/esm/helpers/queries/get-collection.js +179 -167
- package/dist/esm/helpers/queries/get-product-by-slug.js +103 -94
- package/dist/esm/helpers/queries/get-product.js +121 -106
- package/dist/esm/helpers/queries/get-products.js +137 -125
- package/dist/esm/helpers/radius.js +78 -81
- package/dist/esm/helpers/render.js +56 -56
- package/dist/esm/helpers/shadow.js +38 -38
- package/dist/esm/helpers/size.js +12 -13
- package/dist/esm/helpers/spacing.js +35 -37
- package/dist/esm/helpers/tracking/fpixel.js +21 -22
- package/dist/esm/helpers/tracking/gtag.js +84 -90
- package/dist/esm/helpers/tracking/tiktokpixel.js +12 -12
- package/dist/esm/helpers/typography.js +2 -2
- package/dist/esm/hooks/cart/use-add-to-cart.js +10 -10
- package/dist/esm/hooks/cart/use-cart-data.js +18 -16
- package/dist/esm/hooks/cart/use-cart-discount-codes-update.js +10 -10
- package/dist/esm/hooks/cart/use-cart-note-update.js +10 -10
- package/dist/esm/hooks/cart/use-create-cart.js +12 -12
- package/dist/esm/hooks/cart/use-remove-cart-item.js +10 -10
- package/dist/esm/hooks/cart/use-update-cart-item.js +10 -10
- package/dist/esm/hooks/shop/use-collection-query.js +12 -6
- package/dist/esm/hooks/shop/use-product-query.js +12 -6
- package/dist/esm/hooks/shop/use-products-query.js +14 -6
- package/dist/esm/hooks/shop.js +86 -67
- package/dist/esm/hooks/use-current-device.js +14 -16
- package/dist/esm/hooks/use-lazy-video.js +7 -7
- package/dist/esm/hooks/useCartId.js +35 -27
- package/dist/esm/hooks/useCartLine.js +2 -2
- package/dist/esm/hooks/useCartUI.js +37 -31
- package/dist/esm/hooks/useCollection.js +2 -2
- package/dist/esm/hooks/useFetchHandle.js +6 -6
- package/dist/esm/hooks/useFormatMoney.js +11 -7
- package/dist/esm/hooks/useIsomorphicLayoutEffect.js +1 -1
- package/dist/esm/hooks/useLoadScript.js +22 -20
- package/dist/esm/hooks/useMoney.js +59 -42
- package/dist/esm/hooks/usePrevious.js +12 -10
- package/dist/esm/hooks/useProduct.js +92 -69
- package/dist/esm/hooks/useProductList.js +11 -11
- package/dist/esm/hooks/useSuspenseFetch.js +20 -17
- package/dist/esm/hooks/useSwatchesOptions.js +32 -30
- package/dist/esm/types/global-style.js +9 -2
- package/dist/types/index.d.ts +24 -2
- package/package.json +1 -1
|
@@ -3,58 +3,78 @@ import { getCollection } from './queries/get-collection.js';
|
|
|
3
3
|
import { getProduct } from './queries/get-product.js';
|
|
4
4
|
import { getProducts } from './queries/get-products.js';
|
|
5
5
|
|
|
6
|
-
const prefetchQueries = (input, isSample)
|
|
7
|
-
const queries = [];
|
|
8
|
-
Object.keys(input).forEach((key)
|
|
9
|
-
const item = input[key];
|
|
10
|
-
let data = undefined;
|
|
11
|
-
switch
|
|
12
|
-
case 'Product':
|
|
13
|
-
|
|
14
|
-
break;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
6
|
+
const prefetchQueries = (input, isSample)=>{
|
|
7
|
+
const queries = [];
|
|
8
|
+
Object.keys(input).forEach((key)=>{
|
|
9
|
+
const item = input[key];
|
|
10
|
+
let data = undefined;
|
|
11
|
+
switch(item.tag){
|
|
12
|
+
case 'Product':
|
|
13
|
+
{
|
|
14
|
+
if (item.settings?.isAuto) break;
|
|
15
|
+
const variables = {
|
|
16
|
+
id: item.settings?.productSetting?.productId ?? 'latest',
|
|
17
|
+
isSample
|
|
18
|
+
};
|
|
19
|
+
data = {
|
|
20
|
+
key: unstable_serialize([
|
|
21
|
+
'query/product',
|
|
22
|
+
variables
|
|
23
|
+
]),
|
|
24
|
+
func: getProduct,
|
|
25
|
+
variables
|
|
26
|
+
};
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
case 'ProductList':
|
|
30
|
+
{
|
|
31
|
+
if (item?.settings?.productSrc === 'Collection') {
|
|
32
|
+
const variables = {
|
|
33
|
+
id: item.settings?.collectionId ?? 'latest',
|
|
34
|
+
numberOfProducts: item.settings?.numberOfProducts ?? 4,
|
|
35
|
+
orderBy: item.settings?.orderBy,
|
|
36
|
+
isSample
|
|
37
|
+
};
|
|
38
|
+
data = {
|
|
39
|
+
key: unstable_serialize([
|
|
40
|
+
'query/collection',
|
|
41
|
+
variables
|
|
42
|
+
]),
|
|
43
|
+
func: getCollection,
|
|
44
|
+
variables
|
|
45
|
+
};
|
|
46
|
+
} else {
|
|
47
|
+
const variables = {
|
|
48
|
+
ids: [
|
|
49
|
+
...item?.settings?.productIds ?? []
|
|
50
|
+
].sort(),
|
|
51
|
+
isSample
|
|
52
|
+
};
|
|
53
|
+
data = {
|
|
54
|
+
key: unstable_serialize([
|
|
55
|
+
'query/products',
|
|
56
|
+
variables
|
|
57
|
+
]),
|
|
58
|
+
func: getProducts,
|
|
59
|
+
variables
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (data) {
|
|
66
|
+
queries.push(data);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return queries.reduce((accumulator, current)=>{
|
|
70
|
+
if (!accumulator.some((x)=>x.key === current.key)) {
|
|
71
|
+
return [
|
|
72
|
+
...accumulator,
|
|
73
|
+
current
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
return accumulator;
|
|
77
|
+
}, []);
|
|
58
78
|
};
|
|
59
79
|
|
|
60
80
|
export { prefetchQueries };
|
|
@@ -1,32 +1,28 @@
|
|
|
1
|
-
function getSelectedVariant(variants, choices) {
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
return acc;
|
|
29
|
-
}, {});
|
|
1
|
+
function getSelectedVariant(variants, choices) {
|
|
2
|
+
/**
|
|
3
|
+
* Ensure the user has selected all the required options, not just some.
|
|
4
|
+
*/ if (!variants || !variants.length || !choices || variants[0]?.selectedOptions.length !== Object.keys(choices).length) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
return variants.find((variant)=>{
|
|
8
|
+
return Object.entries(choices).every(([name, value])=>{
|
|
9
|
+
return variant?.selectedOptions.some((option)=>option.name === name && option.value === value);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function parseSelectedOption(options) {
|
|
14
|
+
if (!options || options && options.length === 0) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
return options.reduce((acc, { name , value })=>{
|
|
18
|
+
if (name && value) {
|
|
19
|
+
return {
|
|
20
|
+
...acc,
|
|
21
|
+
[name]: value
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return acc;
|
|
25
|
+
}, {});
|
|
30
26
|
}
|
|
31
27
|
|
|
32
28
|
export { getSelectedVariant, parseSelectedOption };
|
|
@@ -2,173 +2,185 @@ import { CollectionDetailFilterDocument } from '../../graphql/queries/collection
|
|
|
2
2
|
import { ProductVariantsDocument } from '../../graphql/queries/product-variants.generated.js';
|
|
3
3
|
import { isDefined } from '../is-defined.js';
|
|
4
4
|
|
|
5
|
-
function productSortedByOrder(key) {
|
|
6
|
-
switch
|
|
7
|
-
case 'TITLE_ASC':
|
|
8
|
-
return {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return
|
|
41
|
-
};
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
{
|
|
132
|
-
node
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
5
|
+
function productSortedByOrder(key) {
|
|
6
|
+
switch(key){
|
|
7
|
+
case 'TITLE_ASC':
|
|
8
|
+
return {
|
|
9
|
+
direction: 'ASC',
|
|
10
|
+
field: 'TITLE'
|
|
11
|
+
};
|
|
12
|
+
case 'TITLE_DESC':
|
|
13
|
+
return {
|
|
14
|
+
direction: 'DESC',
|
|
15
|
+
field: 'TITLE'
|
|
16
|
+
};
|
|
17
|
+
case 'CREATED_AT_ASC':
|
|
18
|
+
return {
|
|
19
|
+
direction: 'ASC',
|
|
20
|
+
field: 'CREATED_AT'
|
|
21
|
+
};
|
|
22
|
+
case 'CREATED_AT_DESC':
|
|
23
|
+
return {
|
|
24
|
+
direction: 'DESC',
|
|
25
|
+
field: 'CREATED_AT'
|
|
26
|
+
};
|
|
27
|
+
case 'none':
|
|
28
|
+
return undefined;
|
|
29
|
+
default:
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const getCollection = async (fetcher, arg)=>{
|
|
34
|
+
const collections = await loopFetchCollection(fetcher, arg);
|
|
35
|
+
if (!collections) {
|
|
36
|
+
throw new Error('collections not found');
|
|
37
|
+
}
|
|
38
|
+
// merge product collection variant
|
|
39
|
+
const mergeProducts = await mergeProductCollectionVariant(fetcher, collections, arg.isSample);
|
|
40
|
+
return mergeProducts;
|
|
41
|
+
};
|
|
42
|
+
const fetchCollection = async (fetcher, variables)=>{
|
|
43
|
+
return fetcher([
|
|
44
|
+
CollectionDetailFilterDocument,
|
|
45
|
+
variables
|
|
46
|
+
]);
|
|
47
|
+
};
|
|
48
|
+
const calculateFirstProduct = (numberOfProducts, currentPage, productsPerPage)=>{
|
|
49
|
+
const remainingProducts = numberOfProducts - productsPerPage * currentPage;
|
|
50
|
+
if (numberOfProducts < productsPerPage) return numberOfProducts;
|
|
51
|
+
return remainingProducts > 0 ? productsPerPage : productsPerPage + remainingProducts;
|
|
52
|
+
};
|
|
53
|
+
const loopFetchCollection = async (fetcher, arg)=>{
|
|
54
|
+
const productsPerPage = 8; // number of products per page
|
|
55
|
+
let variables; // variables of collection query
|
|
56
|
+
let productAfterFetcher; // product after of collection query
|
|
57
|
+
let dataCollection; // data of collection query
|
|
58
|
+
let hasNextPage; //
|
|
59
|
+
let firstCollection;
|
|
60
|
+
let currentPage = 1;
|
|
61
|
+
if (arg.numberOfProducts) {
|
|
62
|
+
const pages = Math.ceil(arg.numberOfProducts / productsPerPage);
|
|
63
|
+
while(currentPage <= pages && hasNextPage !== false){
|
|
64
|
+
variables = {
|
|
65
|
+
firstVariant: 1,
|
|
66
|
+
firstMedia: 30,
|
|
67
|
+
orderBy: {
|
|
68
|
+
field: 'UPDATED_AT',
|
|
69
|
+
direction: 'DESC'
|
|
70
|
+
},
|
|
71
|
+
orderByMedia: {
|
|
72
|
+
field: 'POSITION',
|
|
73
|
+
direction: 'ASC'
|
|
74
|
+
},
|
|
75
|
+
firstProduct: calculateFirstProduct(arg.numberOfProducts, currentPage, productsPerPage),
|
|
76
|
+
...!arg.id || arg.id.toLowerCase() === 'latest' ? {
|
|
77
|
+
where: {
|
|
78
|
+
hasCollectionProducts: true,
|
|
79
|
+
isSample: arg.isSample
|
|
80
|
+
}
|
|
81
|
+
} : {
|
|
82
|
+
where: {
|
|
83
|
+
baseID: arg.id
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
orderByProduct: productSortedByOrder(arg.orderBy),
|
|
87
|
+
whereProduct: {
|
|
88
|
+
isStorefront: true,
|
|
89
|
+
isSample: arg.isSample
|
|
90
|
+
},
|
|
91
|
+
productsAfter: productAfterFetcher
|
|
92
|
+
};
|
|
93
|
+
const pageData = await fetchCollection(fetcher, variables);
|
|
94
|
+
productAfterFetcher = pageData?.collections?.edges?.[0]?.node?.products?.pageInfo?.endCursor;
|
|
95
|
+
if (currentPage === 1) {
|
|
96
|
+
dataCollection = pageData; // set data of first page
|
|
97
|
+
firstCollection = dataCollection?.collections?.edges?.[0];
|
|
98
|
+
} else if (firstCollection?.node?.products && pageData?.collections?.edges?.[0]?.node?.products?.edges) {
|
|
99
|
+
// concat new values to the old ones
|
|
100
|
+
firstCollection.node.products = {
|
|
101
|
+
pageInfo: pageData?.collections?.edges?.[0]?.node?.products?.pageInfo,
|
|
102
|
+
edges: firstCollection.node?.products?.edges.concat(pageData?.collections?.edges?.[0]?.node?.products?.edges)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
currentPage++;
|
|
106
|
+
// hasNextPage = false if there is no more pages then break the loop
|
|
107
|
+
if (!firstCollection?.node?.products?.pageInfo?.hasNextPage) {
|
|
108
|
+
hasNextPage = false;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return dataCollection;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const mergeProductCollectionVariant = async (fetcher, collection, isSample)=>{
|
|
116
|
+
const node = collection?.collections?.edges?.[0]?.node;
|
|
117
|
+
if (!node) return collection;
|
|
118
|
+
const productList = node?.products?.edges;
|
|
119
|
+
const listBaseId = productList?.map((product)=>product?.node?.baseID).filter(isDefined);
|
|
120
|
+
const ProductListWithVariants = await fetchVariantsByBaseIds(fetcher, {
|
|
121
|
+
ids: listBaseId,
|
|
122
|
+
isSample
|
|
123
|
+
});
|
|
124
|
+
// mapping variant to product list in collections
|
|
125
|
+
const newProductList = productList?.map((product)=>{
|
|
126
|
+
const productVariants = ProductListWithVariants?.variants?.edges?.filter((productV)=>productV?.node?.id === product?.node?.id);
|
|
127
|
+
return {
|
|
128
|
+
...product,
|
|
129
|
+
node: product.node ? {
|
|
130
|
+
...product.node,
|
|
131
|
+
variants: product.node?.variants ? {
|
|
132
|
+
...product.node?.variants,
|
|
133
|
+
edges: productVariants?.[0]?.node?.variants?.edges ?? []
|
|
134
|
+
} : undefined
|
|
135
|
+
} : undefined
|
|
136
|
+
};
|
|
137
|
+
}) ?? [];
|
|
138
|
+
const newCollection = {
|
|
139
|
+
...collection,
|
|
140
|
+
collections: {
|
|
141
|
+
edges: [
|
|
142
|
+
{
|
|
143
|
+
node: {
|
|
144
|
+
...node,
|
|
145
|
+
products: {
|
|
146
|
+
edges: newProductList
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
cursor: undefined
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
return newCollection;
|
|
155
|
+
};
|
|
156
|
+
const fetchVariantsByBaseIds = async (fetcher, { ids , isSample })=>{
|
|
157
|
+
const initVariables = {
|
|
158
|
+
firstVariant: 100,
|
|
159
|
+
first: ids?.length || 1,
|
|
160
|
+
orderBy: {
|
|
161
|
+
field: 'UPDATED_AT',
|
|
162
|
+
direction: 'DESC'
|
|
163
|
+
},
|
|
164
|
+
where: {
|
|
165
|
+
isStorefront: true,
|
|
166
|
+
isSample,
|
|
167
|
+
...ids?.length && {
|
|
168
|
+
baseIDIn: ids.sort()
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
const query = async (variables)=>{
|
|
173
|
+
try {
|
|
174
|
+
const response = await fetcher([
|
|
175
|
+
ProductVariantsDocument,
|
|
176
|
+
variables
|
|
177
|
+
]);
|
|
178
|
+
return response;
|
|
179
|
+
} catch {
|
|
180
|
+
return {};
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
return query(initVariables);
|
|
172
184
|
};
|
|
173
185
|
|
|
174
186
|
export { calculateFirstProduct, getCollection };
|