diff --git a/components/carousel.tsx b/components/carousel.tsx index d30ac01a9..e8a080c5b 100644 --- a/components/carousel.tsx +++ b/components/carousel.tsx @@ -1,12 +1,12 @@ import { getCollectionProducts } from 'lib/shopware'; +import { isSeoUrls } from 'lib/shopware/helpers'; import Image from 'next/image'; import Link from 'next/link'; export async function Carousel() { - const collectionName = - `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true' - ? 'Summer-BBQ/Hidden-Carousel-Category' - : 'ff7bf3c59f1342a685844fbf8fdf9dc8'; + const collectionName = isSeoUrls() + ? 'Summer-BBQ/Hidden-Carousel-Category' + : 'ff7bf3c59f1342a685844fbf8fdf9dc8'; const { products } = await getCollectionProducts({ collection: collectionName }); diff --git a/components/grid/three-items.tsx b/components/grid/three-items.tsx index 723bc3095..27242bef5 100644 --- a/components/grid/three-items.tsx +++ b/components/grid/three-items.tsx @@ -1,5 +1,6 @@ import { GridTileImage } from 'components/grid/tile'; import { getCollectionProducts } from 'lib/shopware'; +import { isSeoUrls } from 'lib/shopware/helpers'; import type { Product } from 'lib/shopware/types'; import Link from 'next/link'; @@ -37,10 +38,9 @@ function ThreeItemGridItem({ export async function ThreeItemGrid() { // Collections that start with `hidden-*` are hidden from the search page. - const collectionName = - `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true' - ? 'Summer-BBQ/Hidden-Category' - : '4ab73c06d90d4a5cb312209a64480d87'; + const collectionName = isSeoUrls() + ? 'Summer-BBQ/Hidden-Category' + : '4ab73c06d90d4a5cb312209a64480d87'; const { products: homepageItems } = await getCollectionProducts({ collection: collectionName }); diff --git a/components/layout/navbar/mobile-menu.tsx b/components/layout/navbar/mobile-menu.tsx index 6ede894ae..ececc0a96 100644 --- a/components/layout/navbar/mobile-menu.tsx +++ b/components/layout/navbar/mobile-menu.tsx @@ -7,7 +7,7 @@ import { Fragment, useEffect, useState } from 'react'; import CloseIcon from 'components/icons/close'; import MenuIcon from 'components/icons/menu'; -import { Menu } from 'lib/shopify/types'; +import { Menu } from 'lib/shopware/types'; import Search from './search'; export default function MobileMenu({ menu }: { menu: Menu[] }) { diff --git a/components/layout/navbar/search.tsx b/components/layout/navbar/search.tsx index bf4d12230..fff895f43 100644 --- a/components/layout/navbar/search.tsx +++ b/components/layout/navbar/search.tsx @@ -14,7 +14,6 @@ export default function Search() { const val = e.target as HTMLFormElement; const search = val.search as HTMLInputElement; - console.log(`Search:` + search); const newParams = new URLSearchParams(searchParams.toString()); if (search.value) { diff --git a/lib/shopware/api.ts b/lib/shopware/api.ts index 193cf5af8..5283390a5 100644 --- a/lib/shopware/api.ts +++ b/lib/shopware/api.ts @@ -15,14 +15,12 @@ import { SeoURLResultSW, StoreNavigationTypeSW } from './types'; - -const domainSW = `https://${process.env.SHOPWARE_STORE_DOMAIN!}/${process.env.SHOPWARE_API_TYPE!}`; -const accessTokenSW = `${process.env.SHOPWARE_ACCESS_TOKEN}`; +import { getStoreDomainWithApiType, getAccessToken, getApiType } from 'lib/shopware/helpers'; const apiInstance = createAPIClient({ - baseURL: domainSW, - accessToken: accessTokenSW, - apiType: 'store-api' + baseURL: getStoreDomainWithApiType(), + accessToken: getAccessToken(), + apiType: getApiType() }); // reimport operations return types to use it in application diff --git a/lib/shopware/helpers.ts b/lib/shopware/helpers.ts new file mode 100644 index 000000000..38ea90fb4 --- /dev/null +++ b/lib/shopware/helpers.ts @@ -0,0 +1,25 @@ +export function getAccessToken(): string { + return `${process.env.SHOPWARE_ACCESS_TOKEN}`; +} + +export function getStoreDomainWithApiType(): string { + return getStoreDomain() + '/' + getApiType(); +} + +export function getStoreDomain(protocol: boolean = true): string { + return protocol + ? `https://${process.env.SHOPWARE_STORE_DOMAIN!}` + : `${process.env.SHOPWARE_STORE_DOMAIN!}`; +} + +export function getApiType(): 'store-api' | 'admin-api' { + if (`${process.env.SHOPWARE_API_TYPE!}` === 'admin-api') { + return 'admin-api'; + } + + return 'store-api'; +} + +export function isSeoUrls(): boolean { + return `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'; +} diff --git a/lib/shopware/index.ts b/lib/shopware/index.ts index d915a3617..ee82563b7 100644 --- a/lib/shopware/index.ts +++ b/lib/shopware/index.ts @@ -40,7 +40,7 @@ import { ProductListingCriteria, StoreNavigationTypeSW } from './types'; -const useSeoUrls = `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'; +import { isSeoUrls } from 'lib/shopware/helpers'; export async function getMenu(params?: { type?: StoreNavigationTypeSW; @@ -57,7 +57,7 @@ export async function getPage(handle: string | []): Promise { let seoUrlElement; let pageIdOrHandle = decodeURIComponent(transformHandle(handle)).replace('cms/', ''); - if (useSeoUrls) { + if (isSeoUrls()) { seoUrlElement = await getFirstSeoUrlElement(pageIdOrHandle); if (seoUrlElement) { pageIdOrHandle = seoUrlElement.foreignKey; @@ -103,7 +103,7 @@ export async function getSubCollections(collection: string) { const parentCollectionName = Array.isArray(collection) && collection[0] ? collection[0] : undefined; - if (useSeoUrls) { + if (isSeoUrls()) { const seoUrlElement = await getFirstSeoUrlElement(collectionName); if (seoUrlElement) { criteria = getDefaultSubCategoriesCriteria(seoUrlElement.foreignKey); @@ -129,7 +129,7 @@ export async function getSearchCollectionProducts(params?: { const searchCriteria = { ...criteria, ...sorting }; const search = await requestSearchCollectionProducts(searchCriteria); - if (useSeoUrls && search) { + if (isSeoUrls() && search) { search.elements = await changeVariantUrlToParentUrl(search); } @@ -171,7 +171,7 @@ export async function getCollectionProducts(params?: { const collectionName = decodeURIComponent(transformHandle(params?.collection ?? '')); const sorting = getSortingCriteria(params?.sortKey, params?.reverse); - if (useSeoUrls && !category && collectionName !== '') { + if (isSeoUrls() && !category && collectionName !== '') { const seoUrlElement = await getFirstSeoUrlElement(collectionName); if (seoUrlElement) { category = seoUrlElement.foreignKey; @@ -184,8 +184,8 @@ export async function getCollectionProducts(params?: { } } - if (!useSeoUrls) { - category = params?.collection ?? undefined; + if (!isSeoUrls()) { + category = collectionName ?? undefined; } if (category) { @@ -222,7 +222,7 @@ export async function getCollection(handle: string | []) { let seoUrlElement; let categoryIdOrHandle = decodeURIComponent(transformHandle(handle)); - if (useSeoUrls) { + if (isSeoUrls()) { seoUrlElement = await getFirstSeoUrlElement(categoryIdOrHandle); if (seoUrlElement) { categoryIdOrHandle = seoUrlElement.foreignKey; @@ -261,7 +261,7 @@ export async function getProduct(handle: string | []): Promise 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo - ? type === 'footer-navigation' - ? '/cms/' + item.seoUrls[0].seoPathInfo - : '/search/' + item.seoUrls[0].seoPathInfo - : '' - : type === 'footer-navigation' - ? '/cms/' + item.id ?? '' - : '/search/' + item.id ?? ''; + const path = isSeoUrls() + ? item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo + ? type === 'footer-navigation' + ? '/cms/' + item.seoUrls[0].seoPathInfo + : '/search/' + item.seoUrls[0].seoPathInfo + : '' + : type === 'footer-navigation' + ? '/cms/' + item.id ?? '' + : '/search/' + item.id ?? ''; // @ToDo: currently only footer-navigation is used for cms pages, this need to be more dynamic (shoud depending on the item) return { @@ -127,9 +127,7 @@ export function transformSubCollection( .filter((item) => item.type !== 'link') .map((item) => { const handle = - item.seoUrls && `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true' - ? findHandle(item.seoUrls, parentCollectionName) - : item.id; + isSeoUrls() && item.seoUrls ? findHandle(item.seoUrls, parentCollectionName) : item.id; if (handle) { collection.push({ handle: handle, @@ -196,12 +194,11 @@ export function transformProducts(res: ExtendedProductListingResult): Product[] } export function transformProduct(item: ExtendedProduct): Product { - const useSeoUrls = `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'; const productOptions = transformOptions(item); const productVariants = transformVariants(item); let path = item.parentId ?? item.id ?? ''; - if (useSeoUrls) { + if (isSeoUrls()) { path = item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo ? item.seoUrls[0].seoPathInfo