Skip to content

Commit

Permalink
Merge branch 'develop' into PWA-3233
Browse files Browse the repository at this point in the history
  • Loading branch information
glo82145 committed Mar 20, 2024
2 parents f39a55d + d0eb0a4 commit 8d6ebb8
Show file tree
Hide file tree
Showing 33 changed files with 1,076 additions and 43,937 deletions.
42,112 changes: 0 additions & 42,112 deletions package-lock.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Array [
},
"productType": "ConfigurableProduct",
"sku": "VSK02",
"topLevelSku": "VSK02",
},
"quantity": 1,
},
Expand Down Expand Up @@ -82,6 +83,7 @@ Array [
},
"productType": "SimpleProduct",
"sku": "VA17-SI-NA",
"topLevelSku": "VA17-SI-NA",
},
"quantity": 2,
},
Expand Down Expand Up @@ -130,6 +132,7 @@ Array [
},
"productType": "ConfigurableProduct",
"sku": "VA04",
"topLevelSku": "VA04",
},
"quantity": 3,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Object {
},
"productType": "ConfigurableProduct",
"sku": "VSK02",
"topLevelSku": "VSK02",
},
"quantity": 1,
},
Expand Down Expand Up @@ -78,6 +79,7 @@ Object {
},
"productType": "SimpleProduct",
"sku": "VA17-SI-NA",
"topLevelSku": "VA17-SI-NA",
},
"quantity": 2,
},
Expand Down Expand Up @@ -122,6 +124,7 @@ Object {
},
"productType": "ConfigurableProduct",
"sku": "VA04",
"topLevelSku": "VA04",
},
"quantity": 3,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('handle()', () => {
},
"productType": "ConfigurableProduct",
"sku": "VSK02",
"topLevelSku": "VSK02",
},
"quantity": 1,
},
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('handle()', () => {
},
"productType": "SimpleProduct",
"sku": "VA17-SI-NA",
"topLevelSku": "VA17-SI-NA",
},
"quantity": 2,
},
Expand Down Expand Up @@ -164,6 +166,7 @@ describe('handle()', () => {
},
"productType": "ConfigurableProduct",
"sku": "VA04",
"topLevelSku": "VA04",
},
"quantity": 3,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('handle()', () => {
},
"productType": "ConfigurableProduct",
"sku": "VP08",
"topLevelSku": "VP08",
},
"quantity": 3,
},
Expand Down Expand Up @@ -138,6 +139,7 @@ describe('handle()', () => {
},
"productType": "ConfigurableProduct",
"sku": "VA09",
"topLevelSku": "VA09",
},
"quantity": 1,
},
Expand Down Expand Up @@ -173,6 +175,7 @@ describe('handle()', () => {
},
"productType": "SimpleProduct",
"sku": "VA22-SI-NA",
"topLevelSku": "VA22-SI-NA",
},
"quantity": 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const getFormattedProducts = products => {
product: {
name: name,
sku: sku,
topLevelSku: product.sku,
productType: type,
pricing: {
regularPrice: prices.price.value,
Expand Down
4 changes: 2 additions & 2 deletions packages/peregrine/lib/hooks/useDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useDropdown = () => {
const triggerRef = useRef(null);
const [expanded, setExpanded] = useState(false);

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

// add listener to document, as an effect
useEventListener(globalThis.document, 'mousedown', maybeCollapse);
useEventListener(globalThis.document, 'click', maybeCollapse);

/**
* The object returned contains the pieces needed to add the dropdown logic to your components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const RESOLVE_URL = gql`
# eslint-disable-next-line @graphql-eslint/require-id-when-available
... on ProductInterface {
uid
sku
__typename
}
# eslint-disable-next-line @graphql-eslint/require-id-when-available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Object {
"availableSortMethods": null,
"categoryDescription": "Jewelry category",
"categoryName": "Jewelry",
"filterOptions": undefined,
"filters": null,
"items": Array [
null,
Expand All @@ -17,6 +18,7 @@ Object {
null,
null,
],
"setFilterOptions": [Function],
"totalCount": null,
"totalPagesFromData": null,
}
Expand All @@ -32,6 +34,7 @@ Object {
],
"categoryDescription": "Jewelry category",
"categoryName": "Jewelry",
"filterOptions": undefined,
"filters": Array [
Object {
"label": "Label",
Expand All @@ -47,6 +50,7 @@ Object {
"name": "Necklace",
},
],
"setFilterOptions": [Function],
"totalCount": 2,
"totalPagesFromData": 1,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ jest.mock('@apollo/client', () => {
};
});

const mockGetFiltersAttributeCode = {
data: {
products: {
aggregations: [
{
label: 'Label'
}
]
}
}
};

const mockProductFiltersByCategoryData = {
data: {
products: {
Expand Down Expand Up @@ -91,21 +103,22 @@ const mockCategoryData = {

const mockGetSortMethods = jest.fn();
const mockGetFilters = jest.fn();
const mockfilterData = jest.fn();

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

const Component = props => {
const talonprops = useCategoryContent(props);

return <i {...talonprops} />;
};

useQuery.mockReturnValue({ data: mockCategoryData });
describe('useCategoryContent tests', () => {
it('returns the proper shape', () => {
useLazyQuery
.mockReturnValueOnce([mockfilterData, mockGetFiltersAttributeCode])
.mockReturnValueOnce([
mockGetFilters,
mockProductFiltersByCategoryData
Expand All @@ -124,6 +137,7 @@ describe('useCategoryContent tests', () => {

it('handles default category id', () => {
useLazyQuery
.mockReturnValueOnce([mockfilterData, mockGetFiltersAttributeCode])
.mockReturnValueOnce([
mockGetFilters,
mockProductFiltersByCategoryData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client';

export const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
query getProductFiltersByCategory(
$categoryIdFilter: FilterEqualTypeInput!
) {
Expand All @@ -11,9 +11,8 @@ export const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
attribute_code
options {
label
value
count
}
position
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { useLazyQuery, useQuery } from '@apollo/client';
import { useEffect, useState } from 'react';
import { useLazyQuery, useQuery, gql } from '@apollo/client';

import mergeOperations from '../../../util/shallowMerge';
import { useEventingContext } from '../../../context/eventing';
Expand Down Expand Up @@ -29,15 +29,98 @@ export const useCategoryContent = props => {
getCategoryAvailableSortMethodsQuery
} = operations;

const placeholderItems = Array.from({ length: pageSize }).fill(null);
const [
getFiltersAttributeCode,
{ data: filterAttributeData }
] = useLazyQuery(getProductFiltersByCategoryQuery, {
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first'
});

const [getFilters, { data: filterData }] = useLazyQuery(
getProductFiltersByCategoryQuery,
{
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first'
useEffect(() => {
if (categoryId) {
getFiltersAttributeCode({
variables: {
categoryIdFilter: {
eq: categoryId
}
}
});
}
);
}, [categoryId, getFiltersAttributeCode]);

const availableFilterData = filterAttributeData
? filterAttributeData.products?.aggregations
: null;
const availableFilters = availableFilterData
?.map(eachitem => eachitem.attribute_code)
?.sort();

// Function to generate the dynamic query based on filter parameters
const generateDynamicFiltersQuery = filterParams => {
const categoryUid = `category_uid:{eq:"${categoryId}"}`;
let filterConditions = Object.keys(filterParams)
.map(key => {
const condition = Array.isArray(filterParams[key])
? 'in'
: 'eq';
const value = Array.isArray(filterParams[key])
? JSON.stringify(filterParams[key])
: `"${filterParams[key]}"`;
return `${key}:{${condition}:${value}}`;
})
.join(',');

filterConditions = [categoryUid, filterConditions].join(',');

return gql`
query getProductFiltersByCategory{
products(
filter: {
${filterConditions}
}
) {
aggregations {
label
attribute_code
count
options {
label
value
count
}
}
}
}
`;
};

const [filterOptions, setFilterOptions] = useState();

const selectedFilters = {};

if (filterOptions) {
for (const [group, items] of filterOptions) {
availableFilters?.map(eachitem => {
if (eachitem === group && eachitem !== 'price') {
const sampleArray = [];
for (const item of items) {
sampleArray.push(item.value);
}
selectedFilters[group] = sampleArray;
}
});
}
}

const dynamicQuery = generateDynamicFiltersQuery(selectedFilters);

const placeholderItems = Array.from({ length: pageSize }).fill(null);

const [getFilters, { data: filterData }] = useLazyQuery(dynamicQuery, {
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first'
});

const [getSortMethods, { data: sortData }] = useLazyQuery(
getCategoryAvailableSortMethodsQuery,
Expand All @@ -58,20 +141,13 @@ export const useCategoryContent = props => {
}
}
);

const [, { dispatch }] = useEventingContext();

useEffect(() => {
if (categoryId) {
getFilters({
variables: {
categoryIdFilter: {
eq: categoryId
}
}
});
getFilters();
}
}, [categoryId, getFilters]);
}, [categoryId, filterOptions, getFilters]);

useEffect(() => {
if (categoryId) {
Expand All @@ -85,7 +161,7 @@ export const useCategoryContent = props => {
}
}, [categoryId, getSortMethods]);

const filters = filterData ? filterData.products.aggregations : null;
const filters = filterData ? filterData.products?.aggregations : null;
const items = data ? data.products.items : placeholderItems;
const totalPagesFromData = data
? data.products.page_info.total_pages
Expand Down Expand Up @@ -122,6 +198,8 @@ export const useCategoryContent = props => {
categoryName,
categoryDescription,
filters,
filterOptions,
setFilterOptions,
items,
totalCount,
totalPagesFromData
Expand Down
Loading

0 comments on commit 8d6ebb8

Please sign in to comment.