Skip to content

Commit

Permalink
cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondřej Chrastina committed Sep 11, 2023
1 parent bc618ae commit ed2b544
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
15 changes: 10 additions & 5 deletions lib/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export const resolveUrlPath = (context: ResolutionContext) => {
}
case contentTypes.product.codename: {
if ("terms" in context) {
const query = createQueryStringUrl({
const query = createQueryString({
category: context.terms as string[],
page: context.page?.toString() || undefined
})
return `/${reservedListingSlugs.products}${query}`
});
return `/${reservedListingSlugs.products}${query && '?' + query}`
}

return `/${reservedListingSlugs.products}/${context.slug}`;
Expand Down Expand Up @@ -120,7 +120,12 @@ export const resolveReference = (reference: Reference) => {
return collectionDomain + urlPath;
}

export const createQueryStringUrl = (params: Record<string, string | string[] | undefined>) => {
export const createQueryString = (params: Record<string, string | string[] | undefined>) => {

if (Object.keys(params).length <= 0) {
return '';
}

const queryString = Object.entries(params).map(
([paramKey, paramValue]) => {
if (!paramValue) {
Expand All @@ -133,5 +138,5 @@ export const createQueryStringUrl = (params: Record<string, string | string[] |
},
).filter(p => p !== undefined).join('&');

return Object.keys(params).length > 0 ? `?${queryString}` : '';
return queryString;
}
6 changes: 3 additions & 3 deletions pages/products/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AppPage } from "../../components/shared/ui/appPage";
import { mainColorBgClass } from "../../lib/constants/colors";
import { ProductsPageSize } from "../../lib/constants/paging";
import { getDefaultMetadata, getItemBySlug, getProductsForListing, getSiteMenu } from "../../lib/kontentClient";
import { createQueryStringUrl, reservedListingSlugs, resolveUrlPath } from "../../lib/routing";
import { createQueryString, reservedListingSlugs, resolveUrlPath } from "../../lib/routing";
import { ValidCollectionCodename } from "../../lib/types/perCollection";
import { changeUrlQueryString } from "../../lib/utils/changeUrlQueryString";
import { siteCodename } from "../../lib/utils/env";
Expand Down Expand Up @@ -80,9 +80,9 @@ export const Products: FC<Props> = props => {


const getProducts = useCallback(async () => {
const queryStringUrl = createQueryStringUrl({ preview: props.isPreview.toString(), page, category })
const queryString = createQueryString({ preview: props.isPreview.toString(), page, category })

const response = await fetch(`/api/products${queryStringUrl}`);
const response = await fetch(`/api/products${queryString && '?' + queryString}`);
const newData = await response.json();

setProducts(newData.products);
Expand Down
7 changes: 7 additions & 0 deletions tests/routing.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ describe("resolveUrlPath", () => {
} as ResolutionContext,
"/products?category=cutting"
],
[
{
type: "product",
terms: []
} as ResolutionContext,
"/products"
],
[
{
type: "product",
Expand Down

0 comments on commit ed2b544

Please sign in to comment.