Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed filters of aggregations query in PLP #4362

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { gql } from '@apollo/client';

export const GET_PRODUCT_FILTERS_BY_CATEGORY = gql`
query getProductFiltersByCategory(
$categoryIdFilter: FilterEqualTypeInput!
$filters: ProductAttributeFilterInput!
) {
products(filter: { category_uid: $categoryIdFilter }) {
products(filter: $filters) {
aggregations {
label
count
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { useLazyQuery, useQuery } from '@apollo/client';

import mergeOperations from '../../../util/shallowMerge';
Expand Down Expand Up @@ -39,6 +39,30 @@ export const useCategoryContent = props => {
}
);

const [filterOptions, setFilterOptions] = useState();

const availableFilterData = filterData
? filterData.products?.aggregations
: null;
const availableFilters = availableFilterData
?.map(eachitem => eachitem.attribute_code)
?.sort();
const selectedFilters = {};

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

const [getSortMethods, { data: sortData }] = useLazyQuery(
getCategoryAvailableSortMethodsQuery,
{
Expand All @@ -63,11 +87,22 @@ export const useCategoryContent = props => {

useEffect(() => {
if (categoryId) {
let dynamicFilters = {
category_uid: { eq: categoryId }
};
Object.keys(selectedFilters).forEach(key => {
let filter = {};
if (key === 'price') {
let [from, to] = selectedFilters[key][0].split('_');
filter = { [key]: { from: from, to: to }};
} else {
filter = { [key]: { in: selectedFilters[key]}};
}
dynamicFilters = { ...dynamicFilters, ...filter };
});
getFilters({
variables: {
categoryIdFilter: {
eq: categoryId
}
filters: dynamicFilters
}
});
}
Expand Down Expand Up @@ -122,6 +157,7 @@ export const useCategoryContent = props => {
categoryName,
categoryDescription,
filters,
setFilterOptions,
items,
totalCount,
totalPagesFromData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const CategoryContent = props => {
categoryName,
categoryDescription,
filters,
setFilterOptions,
items,
totalCount,
totalPagesFromData
Expand Down Expand Up @@ -79,7 +80,7 @@ const CategoryContent = props => {
) : null;

const sidebar = shouldShowFilterButtons ? (
<FilterSidebar filters={filters} />
<FilterSidebar filters={filters} setFilterOptions={setFilterOptions} />
) : shouldShowFilterShimmer ? (
<FilterSidebarShimmer />
) : null;
Expand Down
10 changes: 8 additions & 2 deletions packages/venia-ui/lib/components/FilterSidebar/filterSidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useCallback, useRef } from 'react';
import React, { useMemo, useCallback, useRef, useEffect } from 'react';
import { FormattedMessage } from 'react-intl';
import { array, arrayOf, shape, string, number } from 'prop-types';
import { useFilterSidebar } from '@magento/peregrine/lib/talons/FilterSidebar';
Expand All @@ -17,7 +17,7 @@ const SCROLL_OFFSET = 150;
* @param {Object} props.filters - filters to display
*/
const FilterSidebar = props => {
const { filters, filterCountToOpen } = props;
const { filters, filterCountToOpen, setFilterOptions } = props;
const talonProps = useFilterSidebar({ filters });
const {
filterApi,
Expand Down Expand Up @@ -50,6 +50,12 @@ const FilterSidebar = props => {
[handleApply, filterRef]
);

useEffect(() => {
if (filterState) {
setFilterOptions(filterState);
}
}, [filterState, setFilterOptions]);

const filtersList = useMemo(
() =>
Array.from(filterItems, ([group, items], iteration) => {
Expand Down
Loading