Skip to content

Commit

Permalink
Merge pull request #2117 from iFixit/refactor-product-list-path
Browse files Browse the repository at this point in the history
Refactor product list path helper
  • Loading branch information
masonmcelvain authored Nov 21, 2023
2 parents 711f7f1 + ced2146 commit 2211da5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
17 changes: 11 additions & 6 deletions frontend/helpers/path-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ type ProductListPathAttributes = Pick<
ProductList,
'type' | 'handle' | 'deviceTitle'
>;

export function productListPath(
productList: ProductListPathAttributes,
itemType?: string,
variant?: string | undefined
): string {
type ProductListPathProps = {
productList: ProductListPathAttributes;
itemType?: string;
variant?: string | undefined;
};

export function productListPath({
productList,
itemType,
variant,
}: ProductListPathProps): string {
switch (productList.type) {
case ProductListType.AllParts: {
return '/Parts';
Expand Down
8 changes: 4 additions & 4 deletions frontend/seo/product-list/hreflangs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ export function useHreflangs(
productList: ProductList,
itemType: string | null
): Hreflangs | null {
const path = productListPath(productList, itemType ?? undefined).replace(
/^\//,
''
);
const path = productListPath({
productList,
itemType: itemType ?? undefined,
}).replace(/^\//, '');
const collection = pathToCollection[path] ?? null;
return collection ? hreflangs[collection] : null;
}
2 changes: 1 addition & 1 deletion frontend/templates/page/sections/BrowseSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function FeaturedCategories({ categories }: FeaturedCategoriesProps) {
}}
>
<NextLink
href={productListPath(category)}
href={productListPath({ productList: category })}
passHref
legacyBehavior
>
Expand Down
16 changes: 5 additions & 11 deletions frontend/templates/product-list/MetaTags.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PRODUCT_LIST_PAGE_PARAM } from '@config/constants';
import { metaTitleWithSuffix } from '@helpers/metadata-helpers';
import { productListPath } from '@helpers/path-helpers';
import { stylizeDeviceItemType } from '@helpers/product-list-helpers';
import { useAppContext } from '@ifixit/app';
import type { ProductList } from '@models/product-list';
import { noIndexExemptions, useHreflangs } from '@seo/product-list';
Expand Down Expand Up @@ -115,19 +114,14 @@ function useCanonicalUrl(
): string {
const appContext = useAppContext();
const pagination = usePagination();

const page = pagination.currentRefinement + 1;

const itemTypeHandle = itemType
? `/${encodeURIComponent(stylizeDeviceItemType(itemType))}`
: '';
const variant = useVariant();
const page = pagination.currentRefinement + 1;

return `${appContext.ifixitOrigin}${productListPath(
return `${appContext.ifixitOrigin}${productListPath({
productList,
undefined,
productList.indexVariantsInsteadOfDevice ? variant : undefined
)}${itemTypeHandle}${page > 1 ? `?${PRODUCT_LIST_PAGE_PARAM}=${page}` : ''}`;
itemType: itemType ?? undefined,
variant: productList.indexVariantsInsteadOfDevice ? variant : undefined,
})}${page > 1 ? `?${PRODUCT_LIST_PAGE_PARAM}=${page}` : ''}`;
}

function useShouldNoIndex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function useProductListBreadcrumbs(
productList.ancestors.forEach((ancestor) => {
breadcrumbs.push({
label: ancestor.title,
url: productListPath(ancestor),
url: productListPath({ productList: ancestor }),
});
});

Expand All @@ -27,7 +27,7 @@ export function useProductListBreadcrumbs(
if (productList.type === ProductListType.DeviceParts && itemType) {
breadcrumbs.push({
label: productList.title,
url: productListPath(productList),
url: productListPath({ productList }),
});
breadcrumbs.push({
label: itemType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const ProductListLink = ({ productList }: ProductListLinkProps) => {
)}
<Divider orientation="vertical" />
<NextLink
href={productListPath(productList)}
href={productListPath({ productList })}
passHref
legacyBehavior
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const ProductListEmptyState = forwardRef<EmptyStateProps, 'div'>(
This collection does not have products.
</Text>
{parentCategory && (
<Link href={productListPath(parentCategory)}>
<Link href={productListPath({ productList: parentCategory })}>
Return to {parentCategory.title}
</Link>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ export function ProductListChildrenSection({
<ProductListCard
as="a"
href={productListPath({
deviceTitle: child.deviceTitle,
handle: child.handle,
type: child.type,
productList: {
deviceTitle: child.deviceTitle,
handle: child.handle,
type: child.type,
},
})}
productList={{
title: child.title,
Expand Down

2 comments on commit 2211da5

@vercel
Copy link

@vercel vercel bot commented on 2211da5 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-commerce – ./frontend

react-commerce-git-main-ifixit.vercel.app
react-commerce.vercel.app
react-commerce-ifixit.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 2211da5 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-commerce-prod – ./frontend

react-commerce-prod-git-main-ifixit.vercel.app
react-commerce-prod-ifixit.vercel.app
react-commerce-prod.vercel.app

Please sign in to comment.