Skip to content

Commit

Permalink
Fix code after changes in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Jul 24, 2023
1 parent 2798e41 commit 665e058
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
15 changes: 11 additions & 4 deletions components/shared/ui/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FC, useState } from "react";
import { mainColorBgClass } from "../../../lib/constants/colors";
import { externalUrlsMapping } from "../../../lib/constants/menu";
import { createItemSmartLink } from "../../../lib/utils/smartLinkUtils";
import { Block_Navigation, contentTypes } from "../../../models";
import { Block_Navigation, contentTypes, WSL_Page, WSL_WebSpotlightRoot } from "../../../models";
import { useSiteCodename } from "../siteCodenameContext";

type Link = Readonly<Block_Navigation>;
Expand All @@ -26,12 +26,15 @@ type DropdownMenuProps = Readonly<{
links: ReadonlyArray<Link>;
}>;

const isPage = (item: WSL_Page | WSL_WebSpotlightRoot): item is WSL_Page =>
item.system.type === contentTypes.page.codename;

const isCurrentNavigationItemActive = (navigation: Block_Navigation, router: NextRouter) => {
const pathWithoutQuerystring = router.asPath.replace(/\?.*/, '');
const pathSegments = pathWithoutQuerystring.split("/");
const topLevelSegment = pathSegments[1];
const pageLink = navigation.elements.pageLink.linkedItems[0];
return pageLink?.system.type === contentTypes.page.codename && pageLink.elements.url.value === topLevelSegment;
return pageLink && isPage(pageLink) && pageLink.elements.url.value === topLevelSegment;
};

const resolveLink = (link: Readonly<Block_Navigation>) => {
Expand All @@ -42,11 +45,15 @@ const resolveLink = (link: Readonly<Block_Navigation>) => {
const pageLink = link.elements.pageLink.linkedItems[0];
const collectionDomain = externalUrlsMapping[pageLink?.system.collection ?? ""] || "";

if (pageLink?.system.type === contentTypes.web_spotlight_root.codename) {
if (!pageLink) {
return "/invalid-link";
}

if (!isPage(pageLink)) {
return collectionDomain;
}

return collectionDomain + "/" + pageLink?.elements.url.value;
return collectionDomain + "/" + pageLink.elements.url.value;
}

const MenuList: FC<MenuListProps> = props => {
Expand Down
4 changes: 2 additions & 2 deletions lib/kontentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const getProductsForListing = (usePreview: boolean, page?: number, catego
contentTypes.product.elements.title.codename,
contentTypes.product.elements.product_image.codename,
contentTypes.product.elements.slug.codename,
contentTypes.product.elements.category.codename,
contentTypes.product.elements.product_category.codename,
contentTypes.product.elements.price.codename,
])
.queryConfig({
Expand All @@ -102,7 +102,7 @@ export const getProductsForListing = (usePreview: boolean, page?: number, catego
}

if (categories) {
query.anyFilter(`elements.${contentTypes.product.elements.category.codename}`, categories);
query.anyFilter(`elements.${contentTypes.product.elements.product_category.codename}`, categories);
}

return query
Expand Down
2 changes: 1 addition & 1 deletion pages/products/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ProductListing: FC<ProductListingProps> = (props) => {
title={p.elements.title.value}
detailUrl={`products/${p.elements.slug.value}`}
price={p.elements.price.value}
category={p.elements.category.value[0]?.name || ""}
category={p.elements.productCategory.value[0]?.name || ""}
itemId={p.system.id}
/>
))}
Expand Down

3 comments on commit 665e058

@vercel
Copy link

@vercel vercel bot commented on 665e058 Jul 24, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 665e058 Jul 24, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 665e058 Jul 24, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.