mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 14:06:59 +00:00
feat(poc): add helpers
This commit is contained in:
parent
b56e4828e3
commit
a66652fc82
@ -1,12 +1,12 @@
|
|||||||
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({
|
||||||
collection: collectionName
|
collection: collectionName
|
||||||
});
|
});
|
||||||
|
@ -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,10 +38,9 @@ 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({
|
||||||
collection: collectionName
|
collection: collectionName
|
||||||
});
|
});
|
||||||
|
@ -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[] }) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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
25
lib/shopware/helpers.ts
Normal 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';
|
||||||
|
}
|
@ -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;
|
||||||
|
@ -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,16 +26,15 @@ 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
|
: '/search/' + item.seoUrls[0].seoPathInfo
|
||||||
: '/search/' + item.seoUrls[0].seoPathInfo
|
: ''
|
||||||
: ''
|
: type === 'footer-navigation'
|
||||||
: type === 'footer-navigation'
|
? '/cms/' + item.id ?? ''
|
||||||
? '/cms/' + item.id ?? ''
|
: '/search/' + 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)
|
// @ToDo: currently only footer-navigation is used for cms pages, this need to be more dynamic (shoud depending on the item)
|
||||||
return {
|
return {
|
||||||
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user