Skip to content

Commit 8d6ebb8

Browse files
authored
Merge branch 'develop' into PWA-3233
2 parents f39a55d + d0eb0a4 commit 8d6ebb8

File tree

33 files changed

+1076
-43937
lines changed

33 files changed

+1076
-43937
lines changed

package-lock.json

-42,112
This file was deleted.

packages/extensions/experience-platform-connector/src/__tests__/__snapshots__/utils.spec.js.snap

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Array [
4747
},
4848
"productType": "ConfigurableProduct",
4949
"sku": "VSK02",
50+
"topLevelSku": "VSK02",
5051
},
5152
"quantity": 1,
5253
},
@@ -82,6 +83,7 @@ Array [
8283
},
8384
"productType": "SimpleProduct",
8485
"sku": "VA17-SI-NA",
86+
"topLevelSku": "VA17-SI-NA",
8587
},
8688
"quantity": 2,
8789
},
@@ -130,6 +132,7 @@ Array [
130132
},
131133
"productType": "ConfigurableProduct",
132134
"sku": "VA04",
135+
"topLevelSku": "VA04",
133136
},
134137
"quantity": 3,
135138
},

packages/extensions/experience-platform-connector/src/handlers/__tests__/__snapshots__/shoppingMiniCartView.spec.js.snap

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Object {
4747
},
4848
"productType": "ConfigurableProduct",
4949
"sku": "VSK02",
50+
"topLevelSku": "VSK02",
5051
},
5152
"quantity": 1,
5253
},
@@ -78,6 +79,7 @@ Object {
7879
},
7980
"productType": "SimpleProduct",
8081
"sku": "VA17-SI-NA",
82+
"topLevelSku": "VA17-SI-NA",
8183
},
8284
"quantity": 2,
8385
},
@@ -122,6 +124,7 @@ Object {
122124
},
123125
"productType": "ConfigurableProduct",
124126
"sku": "VA04",
127+
"topLevelSku": "VA04",
125128
},
126129
"quantity": 3,
127130
},

packages/extensions/experience-platform-connector/src/handlers/__tests__/shoppingCartPageView.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('handle()', () => {
8181
},
8282
"productType": "ConfigurableProduct",
8383
"sku": "VSK02",
84+
"topLevelSku": "VSK02",
8485
},
8586
"quantity": 1,
8687
},
@@ -116,6 +117,7 @@ describe('handle()', () => {
116117
},
117118
"productType": "SimpleProduct",
118119
"sku": "VA17-SI-NA",
120+
"topLevelSku": "VA17-SI-NA",
119121
},
120122
"quantity": 2,
121123
},
@@ -164,6 +166,7 @@ describe('handle()', () => {
164166
},
165167
"productType": "ConfigurableProduct",
166168
"sku": "VA04",
169+
"topLevelSku": "VA04",
167170
},
168171
"quantity": 3,
169172
},

packages/extensions/experience-platform-connector/src/handlers/__tests__/startCheckout.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe('handle()', () => {
9696
},
9797
"productType": "ConfigurableProduct",
9898
"sku": "VP08",
99+
"topLevelSku": "VP08",
99100
},
100101
"quantity": 3,
101102
},
@@ -138,6 +139,7 @@ describe('handle()', () => {
138139
},
139140
"productType": "ConfigurableProduct",
140141
"sku": "VA09",
142+
"topLevelSku": "VA09",
141143
},
142144
"quantity": 1,
143145
},
@@ -173,6 +175,7 @@ describe('handle()', () => {
173175
},
174176
"productType": "SimpleProduct",
175177
"sku": "VA22-SI-NA",
178+
"topLevelSku": "VA22-SI-NA",
176179
},
177180
"quantity": 1,
178181
},

packages/extensions/experience-platform-connector/src/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const getFormattedProducts = products => {
8282
product: {
8383
name: name,
8484
sku: sku,
85+
topLevelSku: product.sku,
8586
productType: type,
8687
pricing: {
8788
regularPrice: prices.price.value,

packages/peregrine/lib/hooks/useDropdown.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useDropdown = () => {
1414
const triggerRef = useRef(null);
1515
const [expanded, setExpanded] = useState(false);
1616

17-
// collapse on mousedown outside of the element and trigger.
17+
// collapse on mousedown or tab outside of the element and trigger.
1818
const maybeCollapse = useCallback(({ target }) => {
1919
const isOutsideElement =
2020
!elementRef.current || !elementRef.current.contains(target);
@@ -27,7 +27,7 @@ export const useDropdown = () => {
2727
}, []);
2828

2929
// add listener to document, as an effect
30-
useEventListener(globalThis.document, 'mousedown', maybeCollapse);
30+
useEventListener(globalThis.document, 'click', maybeCollapse);
3131

3232
/**
3333
* The object returned contains the pieces needed to add the dropdown logic to your components

packages/peregrine/lib/talons/MagentoRoute/magentoRoute.gql.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const RESOLVE_URL = gql`
1212
# eslint-disable-next-line @graphql-eslint/require-id-when-available
1313
... on ProductInterface {
1414
uid
15+
sku
1516
__typename
1617
}
1718
# eslint-disable-next-line @graphql-eslint/require-id-when-available

packages/peregrine/lib/talons/RootComponents/Category/__tests__/__snapshots__/useCategoryContent.spec.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Object {
55
"availableSortMethods": null,
66
"categoryDescription": "Jewelry category",
77
"categoryName": "Jewelry",
8+
"filterOptions": undefined,
89
"filters": null,
910
"items": Array [
1011
null,
@@ -17,6 +18,7 @@ Object {
1718
null,
1819
null,
1920
],
21+
"setFilterOptions": [Function],
2022
"totalCount": null,
2123
"totalPagesFromData": null,
2224
}
@@ -32,6 +34,7 @@ Object {
3234
],
3335
"categoryDescription": "Jewelry category",
3436
"categoryName": "Jewelry",
37+
"filterOptions": undefined,
3538
"filters": Array [
3639
Object {
3740
"label": "Label",
@@ -47,6 +50,7 @@ Object {
4750
"name": "Necklace",
4851
},
4952
],
53+
"setFilterOptions": [Function],
5054
"totalCount": 2,
5155
"totalPagesFromData": 1,
5256
}

packages/peregrine/lib/talons/RootComponents/Category/__tests__/useCategoryContent.spec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ jest.mock('@apollo/client', () => {
2626
};
2727
});
2828

29+
const mockGetFiltersAttributeCode = {
30+
data: {
31+
products: {
32+
aggregations: [
33+
{
34+
label: 'Label'
35+
}
36+
]
37+
}
38+
}
39+
};
40+
2941
const mockProductFiltersByCategoryData = {
3042
data: {
3143
products: {
@@ -91,21 +103,22 @@ const mockCategoryData = {
91103

92104
const mockGetSortMethods = jest.fn();
93105
const mockGetFilters = jest.fn();
106+
const mockfilterData = jest.fn();
94107

95108
jest.mock('@magento/peregrine/lib/context/eventing', () => ({
96109
useEventingContext: jest.fn().mockReturnValue([{}, { dispatch: jest.fn() }])
97110
}));
98111

99112
const Component = props => {
100113
const talonprops = useCategoryContent(props);
101-
102114
return <i {...talonprops} />;
103115
};
104116

105117
useQuery.mockReturnValue({ data: mockCategoryData });
106118
describe('useCategoryContent tests', () => {
107119
it('returns the proper shape', () => {
108120
useLazyQuery
121+
.mockReturnValueOnce([mockfilterData, mockGetFiltersAttributeCode])
109122
.mockReturnValueOnce([
110123
mockGetFilters,
111124
mockProductFiltersByCategoryData
@@ -124,6 +137,7 @@ describe('useCategoryContent tests', () => {
124137

125138
it('handles default category id', () => {
126139
useLazyQuery
140+
.mockReturnValueOnce([mockfilterData, mockGetFiltersAttributeCode])
127141
.mockReturnValueOnce([
128142
mockGetFilters,
129143
mockProductFiltersByCategoryData

packages/peregrine/lib/talons/RootComponents/Category/categoryContent.gql.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { gql } from '@apollo/client';
22

3-
export const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
3+
const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
44
query getProductFiltersByCategory(
55
$categoryIdFilter: FilterEqualTypeInput!
66
) {
@@ -11,9 +11,8 @@ export const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
1111
attribute_code
1212
options {
1313
label
14-
value
14+
count
1515
}
16-
position
1716
}
1817
}
1918
}

packages/peregrine/lib/talons/RootComponents/Category/useCategoryContent.js

+97-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect } from 'react';
2-
import { useLazyQuery, useQuery } from '@apollo/client';
1+
import { useEffect, useState } from 'react';
2+
import { useLazyQuery, useQuery, gql } from '@apollo/client';
33

44
import mergeOperations from '../../../util/shallowMerge';
55
import { useEventingContext } from '../../../context/eventing';
@@ -29,15 +29,98 @@ export const useCategoryContent = props => {
2929
getCategoryAvailableSortMethodsQuery
3030
} = operations;
3131

32-
const placeholderItems = Array.from({ length: pageSize }).fill(null);
32+
const [
33+
getFiltersAttributeCode,
34+
{ data: filterAttributeData }
35+
] = useLazyQuery(getProductFiltersByCategoryQuery, {
36+
fetchPolicy: 'cache-and-network',
37+
nextFetchPolicy: 'cache-first'
38+
});
3339

34-
const [getFilters, { data: filterData }] = useLazyQuery(
35-
getProductFiltersByCategoryQuery,
36-
{
37-
fetchPolicy: 'cache-and-network',
38-
nextFetchPolicy: 'cache-first'
40+
useEffect(() => {
41+
if (categoryId) {
42+
getFiltersAttributeCode({
43+
variables: {
44+
categoryIdFilter: {
45+
eq: categoryId
46+
}
47+
}
48+
});
3949
}
40-
);
50+
}, [categoryId, getFiltersAttributeCode]);
51+
52+
const availableFilterData = filterAttributeData
53+
? filterAttributeData.products?.aggregations
54+
: null;
55+
const availableFilters = availableFilterData
56+
?.map(eachitem => eachitem.attribute_code)
57+
?.sort();
58+
59+
// Function to generate the dynamic query based on filter parameters
60+
const generateDynamicFiltersQuery = filterParams => {
61+
const categoryUid = `category_uid:{eq:"${categoryId}"}`;
62+
let filterConditions = Object.keys(filterParams)
63+
.map(key => {
64+
const condition = Array.isArray(filterParams[key])
65+
? 'in'
66+
: 'eq';
67+
const value = Array.isArray(filterParams[key])
68+
? JSON.stringify(filterParams[key])
69+
: `"${filterParams[key]}"`;
70+
return `${key}:{${condition}:${value}}`;
71+
})
72+
.join(',');
73+
74+
filterConditions = [categoryUid, filterConditions].join(',');
75+
76+
return gql`
77+
query getProductFiltersByCategory{
78+
products(
79+
filter: {
80+
${filterConditions}
81+
}
82+
) {
83+
aggregations {
84+
label
85+
attribute_code
86+
count
87+
options {
88+
label
89+
value
90+
count
91+
}
92+
}
93+
}
94+
}
95+
`;
96+
};
97+
98+
const [filterOptions, setFilterOptions] = useState();
99+
100+
const selectedFilters = {};
101+
102+
if (filterOptions) {
103+
for (const [group, items] of filterOptions) {
104+
availableFilters?.map(eachitem => {
105+
if (eachitem === group && eachitem !== 'price') {
106+
const sampleArray = [];
107+
for (const item of items) {
108+
sampleArray.push(item.value);
109+
}
110+
selectedFilters[group] = sampleArray;
111+
}
112+
});
113+
}
114+
}
115+
116+
const dynamicQuery = generateDynamicFiltersQuery(selectedFilters);
117+
118+
const placeholderItems = Array.from({ length: pageSize }).fill(null);
119+
120+
const [getFilters, { data: filterData }] = useLazyQuery(dynamicQuery, {
121+
fetchPolicy: 'cache-and-network',
122+
nextFetchPolicy: 'cache-first'
123+
});
41124

42125
const [getSortMethods, { data: sortData }] = useLazyQuery(
43126
getCategoryAvailableSortMethodsQuery,
@@ -58,20 +141,13 @@ export const useCategoryContent = props => {
58141
}
59142
}
60143
);
61-
62144
const [, { dispatch }] = useEventingContext();
63145

64146
useEffect(() => {
65147
if (categoryId) {
66-
getFilters({
67-
variables: {
68-
categoryIdFilter: {
69-
eq: categoryId
70-
}
71-
}
72-
});
148+
getFilters();
73149
}
74-
}, [categoryId, getFilters]);
150+
}, [categoryId, filterOptions, getFilters]);
75151

76152
useEffect(() => {
77153
if (categoryId) {
@@ -85,7 +161,7 @@ export const useCategoryContent = props => {
85161
}
86162
}, [categoryId, getSortMethods]);
87163

88-
const filters = filterData ? filterData.products.aggregations : null;
164+
const filters = filterData ? filterData.products?.aggregations : null;
89165
const items = data ? data.products.items : placeholderItems;
90166
const totalPagesFromData = data
91167
? data.products.page_info.total_pages
@@ -122,6 +198,8 @@ export const useCategoryContent = props => {
122198
categoryName,
123199
categoryDescription,
124200
filters,
201+
filterOptions,
202+
setFilterOptions,
125203
items,
126204
totalCount,
127205
totalPagesFromData

0 commit comments

Comments
 (0)