feat(poc): add helpers

This commit is contained in:
Björn Meyer 2023-07-19 09:50:46 +02:00
parent b56e4828e3
commit a66652fc82
8 changed files with 59 additions and 40 deletions

View File

@ -1,10 +1,10 @@
import { getCollectionProducts } from 'lib/shopware'; import { getCollectionProducts } from 'lib/shopware';
import { isSeoUrls } from 'lib/shopware/helpers';
import Image from 'next/image'; import Image from 'next/image';
import Link from 'next/link'; import Link from 'next/link';
export async function Carousel() { export async function Carousel() {
const collectionName = const collectionName = isSeoUrls()
`${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'
? 'Summer-BBQ/Hidden-Carousel-Category' ? 'Summer-BBQ/Hidden-Carousel-Category'
: 'ff7bf3c59f1342a685844fbf8fdf9dc8'; : 'ff7bf3c59f1342a685844fbf8fdf9dc8';
const { products } = await getCollectionProducts({ const { products } = await getCollectionProducts({

View File

@ -1,5 +1,6 @@
import { GridTileImage } from 'components/grid/tile'; import { GridTileImage } from 'components/grid/tile';
import { getCollectionProducts } from 'lib/shopware'; import { getCollectionProducts } from 'lib/shopware';
import { isSeoUrls } from 'lib/shopware/helpers';
import type { Product } from 'lib/shopware/types'; import type { Product } from 'lib/shopware/types';
import Link from 'next/link'; import Link from 'next/link';
@ -37,8 +38,7 @@ function ThreeItemGridItem({
export async function ThreeItemGrid() { export async function ThreeItemGrid() {
// Collections that start with `hidden-*` are hidden from the search page. // Collections that start with `hidden-*` are hidden from the search page.
const collectionName = const collectionName = isSeoUrls()
`${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'
? 'Summer-BBQ/Hidden-Category' ? 'Summer-BBQ/Hidden-Category'
: '4ab73c06d90d4a5cb312209a64480d87'; : '4ab73c06d90d4a5cb312209a64480d87';
const { products: homepageItems } = await getCollectionProducts({ const { products: homepageItems } = await getCollectionProducts({

View File

@ -7,7 +7,7 @@ import { Fragment, useEffect, useState } from 'react';
import CloseIcon from 'components/icons/close'; import CloseIcon from 'components/icons/close';
import MenuIcon from 'components/icons/menu'; import MenuIcon from 'components/icons/menu';
import { Menu } from 'lib/shopify/types'; import { Menu } from 'lib/shopware/types';
import Search from './search'; import Search from './search';
export default function MobileMenu({ menu }: { menu: Menu[] }) { export default function MobileMenu({ menu }: { menu: Menu[] }) {

View File

@ -14,7 +14,6 @@ export default function Search() {
const val = e.target as HTMLFormElement; const val = e.target as HTMLFormElement;
const search = val.search as HTMLInputElement; const search = val.search as HTMLInputElement;
console.log(`Search:` + search);
const newParams = new URLSearchParams(searchParams.toString()); const newParams = new URLSearchParams(searchParams.toString());
if (search.value) { if (search.value) {

View File

@ -15,14 +15,12 @@ import {
SeoURLResultSW, SeoURLResultSW,
StoreNavigationTypeSW StoreNavigationTypeSW
} from './types'; } from './types';
import { getStoreDomainWithApiType, getAccessToken, getApiType } from 'lib/shopware/helpers';
const domainSW = `https://${process.env.SHOPWARE_STORE_DOMAIN!}/${process.env.SHOPWARE_API_TYPE!}`;
const accessTokenSW = `${process.env.SHOPWARE_ACCESS_TOKEN}`;
const apiInstance = createAPIClient<extendedOperations, extendedPaths>({ const apiInstance = createAPIClient<extendedOperations, extendedPaths>({
baseURL: domainSW, baseURL: getStoreDomainWithApiType(),
accessToken: accessTokenSW, accessToken: getAccessToken(),
apiType: 'store-api' apiType: getApiType()
}); });
// reimport operations return types to use it in application // reimport operations return types to use it in application

25
lib/shopware/helpers.ts Normal file
View File

@ -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';
}

View File

@ -40,7 +40,7 @@ import {
ProductListingCriteria, ProductListingCriteria,
StoreNavigationTypeSW StoreNavigationTypeSW
} from './types'; } from './types';
const useSeoUrls = `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'; import { isSeoUrls } from 'lib/shopware/helpers';
export async function getMenu(params?: { export async function getMenu(params?: {
type?: StoreNavigationTypeSW; type?: StoreNavigationTypeSW;
@ -57,7 +57,7 @@ export async function getPage(handle: string | []): Promise<Page | undefined> {
let seoUrlElement; let seoUrlElement;
let pageIdOrHandle = decodeURIComponent(transformHandle(handle)).replace('cms/', ''); let pageIdOrHandle = decodeURIComponent(transformHandle(handle)).replace('cms/', '');
if (useSeoUrls) { if (isSeoUrls()) {
seoUrlElement = await getFirstSeoUrlElement(pageIdOrHandle); seoUrlElement = await getFirstSeoUrlElement(pageIdOrHandle);
if (seoUrlElement) { if (seoUrlElement) {
pageIdOrHandle = seoUrlElement.foreignKey; pageIdOrHandle = seoUrlElement.foreignKey;
@ -103,7 +103,7 @@ export async function getSubCollections(collection: string) {
const parentCollectionName = const parentCollectionName =
Array.isArray(collection) && collection[0] ? collection[0] : undefined; Array.isArray(collection) && collection[0] ? collection[0] : undefined;
if (useSeoUrls) { if (isSeoUrls()) {
const seoUrlElement = await getFirstSeoUrlElement(collectionName); const seoUrlElement = await getFirstSeoUrlElement(collectionName);
if (seoUrlElement) { if (seoUrlElement) {
criteria = getDefaultSubCategoriesCriteria(seoUrlElement.foreignKey); criteria = getDefaultSubCategoriesCriteria(seoUrlElement.foreignKey);
@ -129,7 +129,7 @@ export async function getSearchCollectionProducts(params?: {
const searchCriteria = { ...criteria, ...sorting }; const searchCriteria = { ...criteria, ...sorting };
const search = await requestSearchCollectionProducts(searchCriteria); const search = await requestSearchCollectionProducts(searchCriteria);
if (useSeoUrls && search) { if (isSeoUrls() && search) {
search.elements = await changeVariantUrlToParentUrl(search); search.elements = await changeVariantUrlToParentUrl(search);
} }
@ -171,7 +171,7 @@ export async function getCollectionProducts(params?: {
const collectionName = decodeURIComponent(transformHandle(params?.collection ?? '')); const collectionName = decodeURIComponent(transformHandle(params?.collection ?? ''));
const sorting = getSortingCriteria(params?.sortKey, params?.reverse); const sorting = getSortingCriteria(params?.sortKey, params?.reverse);
if (useSeoUrls && !category && collectionName !== '') { if (isSeoUrls() && !category && collectionName !== '') {
const seoUrlElement = await getFirstSeoUrlElement(collectionName); const seoUrlElement = await getFirstSeoUrlElement(collectionName);
if (seoUrlElement) { if (seoUrlElement) {
category = seoUrlElement.foreignKey; category = seoUrlElement.foreignKey;
@ -184,8 +184,8 @@ export async function getCollectionProducts(params?: {
} }
} }
if (!useSeoUrls) { if (!isSeoUrls()) {
category = params?.collection ?? undefined; category = collectionName ?? undefined;
} }
if (category) { if (category) {
@ -222,7 +222,7 @@ export async function getCollection(handle: string | []) {
let seoUrlElement; let seoUrlElement;
let categoryIdOrHandle = decodeURIComponent(transformHandle(handle)); let categoryIdOrHandle = decodeURIComponent(transformHandle(handle));
if (useSeoUrls) { if (isSeoUrls()) {
seoUrlElement = await getFirstSeoUrlElement(categoryIdOrHandle); seoUrlElement = await getFirstSeoUrlElement(categoryIdOrHandle);
if (seoUrlElement) { if (seoUrlElement) {
categoryIdOrHandle = seoUrlElement.foreignKey; categoryIdOrHandle = seoUrlElement.foreignKey;
@ -261,7 +261,7 @@ export async function getProduct(handle: string | []): Promise<Product | undefin
const productHandle = decodeURIComponent(transformHandle(handle)); const productHandle = decodeURIComponent(transformHandle(handle));
productId = productHandle; // if we do not use seoUrls the handle should be the product id productId = productHandle; // if we do not use seoUrls the handle should be the product id
if (useSeoUrls) { if (isSeoUrls()) {
const seoUrlElement = await getFirstSeoUrlElement(productHandle); const seoUrlElement = await getFirstSeoUrlElement(productHandle);
if (seoUrlElement) { if (seoUrlElement) {
productId = seoUrlElement.foreignKey; productId = seoUrlElement.foreignKey;

View File

@ -15,6 +15,7 @@ import {
ExtendedProductListingResult ExtendedProductListingResult
} from './api-extended'; } from './api-extended';
import { ListItem } from 'components/layout/search/filter'; import { ListItem } from 'components/layout/search/filter';
import { isSeoUrls } from 'lib/shopware/helpers';
export function transformMenu(res: ExtendedCategory[], type: string) { export function transformMenu(res: ExtendedCategory[], type: string) {
const menu: Menu[] = []; const menu: Menu[] = [];
@ -25,8 +26,7 @@ export function transformMenu(res: ExtendedCategory[], type: string) {
} }
function transformMenuItem(item: ExtendedCategory, type: string): Menu { function transformMenuItem(item: ExtendedCategory, type: string): Menu {
const path = const path = isSeoUrls()
`${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'
? item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo ? item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo
? type === 'footer-navigation' ? type === 'footer-navigation'
? '/cms/' + item.seoUrls[0].seoPathInfo ? '/cms/' + item.seoUrls[0].seoPathInfo
@ -127,9 +127,7 @@ export function transformSubCollection(
.filter((item) => item.type !== 'link') .filter((item) => item.type !== 'link')
.map((item) => { .map((item) => {
const handle = const handle =
item.seoUrls && `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true' isSeoUrls() && item.seoUrls ? findHandle(item.seoUrls, parentCollectionName) : item.id;
? findHandle(item.seoUrls, parentCollectionName)
: item.id;
if (handle) { if (handle) {
collection.push({ collection.push({
handle: handle, handle: handle,
@ -196,12 +194,11 @@ export function transformProducts(res: ExtendedProductListingResult): Product[]
} }
export function transformProduct(item: ExtendedProduct): Product { export function transformProduct(item: ExtendedProduct): Product {
const useSeoUrls = `${process.env.SHOPWARE_USE_SEO_URLS}` === 'true';
const productOptions = transformOptions(item); const productOptions = transformOptions(item);
const productVariants = transformVariants(item); const productVariants = transformVariants(item);
let path = item.parentId ?? item.id ?? ''; let path = item.parentId ?? item.id ?? '';
if (useSeoUrls) { if (isSeoUrls()) {
path = path =
item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo
? item.seoUrls[0].seoPathInfo ? item.seoUrls[0].seoPathInfo