Skip to content

Commit

Permalink
[project-base] Fix adding product to cart from search page. (#3671)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMolcik authored Dec 18, 2024
2 parents 9b6ebdf + 3adaca3 commit 2051bde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 11 additions & 2 deletions storefront/utils/recommender/getRecommenderClientIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { logException } from 'utils/errors/logException';

const RECOMMENDER_PATHNAMES = {
'/': 'homepage',
'/products/[productSlug]': 'product-detail',
'/blogArticles/[blogArticleSlug]': 'blog-article-detail',
'/cart': 'cart',
'/categories/[categorySlug]': 'category-detail',
'/flags/[flagSlug]': 'flag-detail',
'/product-comparison': 'product-comparison',
'/products/[productSlug]': 'product-detail',
'/search': 'search',
'/wishlist': 'wishlist',
} as const;

export const getRecommenderClientIdentifier = (pathname: string): string => {
const splitPathname = pathname.split('?')[0];
if (!(splitPathname in RECOMMENDER_PATHNAMES)) {
throw new Error(`Pathname ${splitPathname} does not have a corresponding recommender client identifier`);
logException(`Pathname ${splitPathname} does not have a corresponding identifier in RECOMMENDER_PATHNAMES`);
return splitPathname;
}

return RECOMMENDER_PATHNAMES[splitPathname as RecommenderClientIdentifierKeyType];
Expand Down
11 changes: 7 additions & 4 deletions storefront/utils/requestProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getDomainConfig } from './domain/domainConfig';
import { logException } from './errors/logException';
import { GetServerSidePropsContext, NextPageContext } from 'next';

type Protocol = 'http' | 'https';
Expand All @@ -11,12 +12,13 @@ export const getProtocolClientSide = (): Protocol => {
return window.location.protocol === 'https:' ? 'https' : 'http';
};

export const getProtocol = (context: GetServerSidePropsContext | NextPageContext | undefined): Protocol => {
export const getProtocol = (context: GetServerSidePropsContext | NextPageContext | undefined): Protocol | undefined => {
if (!context) {
try {
return getProtocolClientSide();
} catch (e) {
throw new Error('context must be provided when running on the server side');
logException('context must be provided when running on the server side');
return undefined;
}
}

Expand All @@ -36,12 +38,13 @@ export const getProtocol = (context: GetServerSidePropsContext | NextPageContext
return protocol;
};

export const getIsHttps = (protocol?: string) => {
export const getIsHttps = (protocol?: string | undefined): boolean | undefined => {
if (!protocol) {
try {
return getProtocolClientSide() === 'https';
} catch (e) {
throw new Error('protocol must be provided when running on the server side');
logException('protocol must be provided when running on the server side');
return undefined;
}
}

Expand Down

0 comments on commit 2051bde

Please sign in to comment.