Skip to content

Commit

Permalink
feat: show sub-categories
Browse files Browse the repository at this point in the history
  • Loading branch information
migueloller committed Jan 3, 2025
1 parent 4b65f5c commit 4bbfb41
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.

This file was deleted.

20 changes: 16 additions & 4 deletions core/app/[locale]/(default)/(faceted)/category/[slug]/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { graphql, VariablesOf } from '~/client/graphql';
import { revalidate } from '~/client/revalidate-target';
import { BreadcrumbsFragment } from '~/components/breadcrumbs/fragment';

import { CategoryTreeFragment } from './_components/sub-categories';

const CategoryPageQuery = graphql(
`
query CategoryPageQuery($categoryId: Int!) {
Expand All @@ -22,11 +20,25 @@ const CategoryPageQuery = graphql(
metaKeywords
}
}
...CategoryTreeFragment
categoryTree(rootEntityId: $categoryId) {
entityId
name
path
children {
entityId
name
path
children {
entityId
name
path
}
}
}
}
}
`,
[BreadcrumbsFragment, CategoryTreeFragment],
[BreadcrumbsFragment],
);

type Variables = VariablesOf<typeof CategoryPageQuery>;
Expand Down
27 changes: 26 additions & 1 deletion core/app/[locale]/(default)/(faceted)/category/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ async function getBreadcrumbs(props: Props): Promise<Breadcrumb[]> {
}));
}

async function getSubCategoriesFilters(props: Props): Promise<Filter[]> {
const { slug } = await props.params;
const categoryId = Number(slug);
const data = await getCategoryPageData({ categoryId });
const t = await getTranslations('FacetedGroup.MobileSideNav');

const categoryTree = data.categoryTree[0];

if (categoryTree == null || categoryTree.children.length === 0) return [];

return [
{
type: 'link-group',
label: t('subCategories'),
links: categoryTree.children.map((category) => ({
label: category.name,
href: category.path,
})),
},
];
}

async function getTitle(props: Props): Promise<string | null> {
const category = await getCategory(props);

Expand Down Expand Up @@ -143,7 +165,10 @@ async function getFilters(props: Props): Promise<Filter[]> {
searchParams: { ...searchParams, ...parsedSearchParams },
});

return transformedFacets.filter((facet) => facet != null);
const filters = transformedFacets.filter((facet) => facet != null);
const subCategoriesFilters = await getSubCategoriesFilters(props);

return [...subCategoriesFilters, ...filters];
}

async function getSortLabel(): Promise<string> {
Expand Down

0 comments on commit 4bbfb41

Please sign in to comment.