mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 05:56:59 +00:00
feat(poc): add helpers
This commit is contained in:
parent
b56e4828e3
commit
a66652fc82
@ -1,10 +1,10 @@
|
||||
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'
|
||||
const collectionName = isSeoUrls()
|
||||
? 'Summer-BBQ/Hidden-Carousel-Category'
|
||||
: 'ff7bf3c59f1342a685844fbf8fdf9dc8';
|
||||
const { products } = await getCollectionProducts({
|
||||
|
@ -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,8 +38,7 @@ 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'
|
||||
const collectionName = isSeoUrls()
|
||||
? 'Summer-BBQ/Hidden-Category'
|
||||
: '4ab73c06d90d4a5cb312209a64480d87';
|
||||
const { products: homepageItems } = await getCollectionProducts({
|
||||
|
@ -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[] }) {
|
||||
|
@ -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) {
|
||||
|
@ -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<extendedOperations, extendedPaths>({
|
||||
baseURL: domainSW,
|
||||
accessToken: accessTokenSW,
|
||||
apiType: 'store-api'
|
||||
baseURL: getStoreDomainWithApiType(),
|
||||
accessToken: getAccessToken(),
|
||||
apiType: getApiType()
|
||||
});
|
||||
|
||||
// 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,
|
||||
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<Page | undefined> {
|
||||
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<Product | undefin
|
||||
const productHandle = decodeURIComponent(transformHandle(handle));
|
||||
productId = productHandle; // if we do not use seoUrls the handle should be the product id
|
||||
|
||||
if (useSeoUrls) {
|
||||
if (isSeoUrls()) {
|
||||
const seoUrlElement = await getFirstSeoUrlElement(productHandle);
|
||||
if (seoUrlElement) {
|
||||
productId = seoUrlElement.foreignKey;
|
||||
|
@ -15,6 +15,7 @@ import {
|
||||
ExtendedProductListingResult
|
||||
} from './api-extended';
|
||||
import { ListItem } from 'components/layout/search/filter';
|
||||
import { isSeoUrls } from 'lib/shopware/helpers';
|
||||
|
||||
export function transformMenu(res: ExtendedCategory[], type: string) {
|
||||
const menu: Menu[] = [];
|
||||
@ -25,8 +26,7 @@ export function transformMenu(res: ExtendedCategory[], type: string) {
|
||||
}
|
||||
|
||||
function transformMenuItem(item: ExtendedCategory, type: string): Menu {
|
||||
const path =
|
||||
`${process.env.SHOPWARE_USE_SEO_URLS}` === 'true'
|
||||
const path = isSeoUrls()
|
||||
? item.seoUrls && item.seoUrls.length > 0 && item.seoUrls[0] && item.seoUrls[0].seoPathInfo
|
||||
? type === 'footer-navigation'
|
||||
? '/cms/' + item.seoUrls[0].seoPathInfo
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user