forked from Weaverse/aspen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product-list.tsx
99 lines (91 loc) · 2.53 KB
/
product-list.tsx
1
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
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
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
import type {
HydrogenComponentProps,
HydrogenComponentSchema,
ComponentLoaderArgs,
} from '@weaverse/hydrogen';
import {getPaginationVariables} from '@shopify/hydrogen';
import {forwardRef} from 'react';
import {ProductSwimlane} from '~/components';
import {COLLECTION_QUERY} from '~/data/queries';
import type {SortParam} from '~/components/SortFilter';
import {getSortValuesFromParam} from '~/routes/($locale).collections.$collectionHandle';
import {PAGINATION_SIZE} from '~/lib/const';
interface ProductListProps
extends HydrogenComponentProps<Awaited<ReturnType<typeof loader>>> {
heading: string;
productsCount: number;
}
let ProductList = forwardRef<HTMLElement, ProductListProps>((props, ref) => {
let {loaderData, heading, productsCount, ...rest} = props;
let products = loaderData?.collection?.products;
return (
<section ref={ref} {...rest}>
{products?.nodes?.length ? (
<ProductSwimlane
products={products}
title={heading}
count={productsCount}
/>
) : null}
</section>
);
});
export default ProductList;
export let loader = async ({weaverse, data}: ComponentLoaderArgs) => {
let {language, country} = weaverse.storefront.i18n;
let collectionHandle = data?.collection?.handle;
const searchParams = new URL(weaverse.request.url).searchParams;
const {sortKey, reverse} = getSortValuesFromParam(
searchParams.get('sort') as SortParam,
);
const paginationVariables = getPaginationVariables(weaverse.request, {
pageBy: PAGINATION_SIZE,
});
return await weaverse.storefront.query(COLLECTION_QUERY, {
variables: {
...paginationVariables,
handle: collectionHandle,
country,
language,
sortKey,
reverse,
filters: [],
},
});
};
export let schema: HydrogenComponentSchema = {
type: 'product-list',
title: 'Product List',
limit: 1,
inspector: [
{
group: 'Product List',
inputs: [
{
type: 'collection',
name: 'collection',
label: 'Collection',
},
{
type: 'text',
name: 'heading',
label: 'Heading',
defaultValue: 'Product List',
placeholder: 'Product List',
},
{
type: 'range',
name: 'productsCount',
label: 'Number of products',
defaultValue: 4,
configs: {
min: 1,
max: 12,
step: 1,
},
},
],
},
],
toolbar: ['general-settings', ['duplicate', 'delete']],
};