categories

This commit is contained in:
Kristian Duda 2024-06-28 08:43:31 +02:00
parent ccf6d51eee
commit ae486fcfbc
3 changed files with 43 additions and 79 deletions

View File

@ -1,5 +1,3 @@
import { Collection, Page } from 'lib/shopify/types';
export type SortFilterItem = { export type SortFilterItem = {
title: string; title: string;
slug: string | null; slug: string | null;
@ -31,31 +29,3 @@ export const TAGS = {
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
export const DEFAULT_OPTION = 'Default Title'; export const DEFAULT_OPTION = 'Default Title';
export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json'; export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json';
export const PAGES: Page[] = [];
const CURRENT_DATE = new Date().toISOString();
export const COLLECTIONS: Collection[] = [
{
handle: '',
title: 'All',
description: 'All products',
seo: {
title: 'All',
description: 'All products'
},
path: '/search',
updatedAt: CURRENT_DATE
},
{
handle: 'shirts',
title: 'Shirts',
description: 'Shirts',
seo: {
title: 'Shirts',
description: 'Shirts'
},
path: '/search/shirts',
updatedAt: CURRENT_DATE
}
];

View File

@ -1,6 +1,6 @@
import { COLLECTIONS, SHOPIFY_GRAPHQL_API_ENDPOINT, TAGS } from 'lib/constants'; import { SHOPIFY_GRAPHQL_API_ENDPOINT, TAGS } from 'lib/constants';
import { find, findByID } from 'lib/shopify/payload'; import { find, findByID } from 'lib/shopify/payload';
import { Media, Option, Product, Tag } from 'lib/shopify/payload-types'; import { Category, Media, Option, Product } from 'lib/shopify/payload-types';
import { isShopifyError } from 'lib/type-guards'; import { isShopifyError } from 'lib/type-guards';
import { ensureStartsWith } from 'lib/utils'; import { ensureStartsWith } from 'lib/utils';
import { revalidateTag } from 'next/cache'; import { revalidateTag } from 'next/cache';
@ -28,7 +28,6 @@ import {
ShopifyAddToCartOperation, ShopifyAddToCartOperation,
ShopifyCart, ShopifyCart,
ShopifyCartOperation, ShopifyCartOperation,
ShopifyCollection,
ShopifyCreateCartOperation, ShopifyCreateCartOperation,
ShopifyPageOperation, ShopifyPageOperation,
ShopifyRemoveFromCartOperation, ShopifyRemoveFromCartOperation,
@ -117,33 +116,6 @@ const reshapeCart = (cart: ShopifyCart): Cart => {
}; };
}; };
const reshapeCollection = (collection: ShopifyCollection): Collection | undefined => {
if (!collection) {
return undefined;
}
return {
...collection,
path: `/search/${collection.handle}`
};
};
const reshapeCollections = (collections: ShopifyCollection[]) => {
const reshapedCollections = [];
for (const collection of collections) {
if (collection) {
const reshapedCollection = reshapeCollection(collection);
if (reshapedCollection) {
reshapedCollections.push(reshapedCollection);
}
}
}
return reshapedCollections;
};
export async function createCart(): Promise<Cart> { export async function createCart(): Promise<Cart> {
const res = await shopifyFetch<ShopifyCreateCartOperation>({ const res = await shopifyFetch<ShopifyCreateCartOperation>({
query: createCartMutation, query: createCartMutation,
@ -214,7 +186,8 @@ export async function getCart(cartId: string): Promise<Cart | undefined> {
} }
export async function getCollection(handle: string): Promise<Collection | undefined> { export async function getCollection(handle: string): Promise<Collection | undefined> {
return COLLECTIONS.find((collection) => collection.handle === handle); const category = await findByID<Category>('categories', handle);
return reshapeCategory(category);
} }
const reshapeImage = (media: Media): Image => { const reshapeImage = (media: Media): Image => {
@ -271,10 +244,6 @@ const reshapeVariants = (variants: Product['variants']): ProductVariant[] => {
})); }));
}; };
const reshapeTags = (tags: Tag[]): string[] => {
return tags.map((tag) => tag.name);
};
const reshapeProduct = (product: Product): ExProduct => { const reshapeProduct = (product: Product): ExProduct => {
return { return {
id: product.id, id: product.id,
@ -294,7 +263,7 @@ const reshapeProduct = (product: Product): ExProduct => {
title: product.title, title: product.title,
description: product.description description: product.description
}, },
tags: reshapeTags(product.tags as Tag[]), tags: product.tags ?? [],
variants: reshapeVariants(product.variants), variants: reshapeVariants(product.variants),
updatedAt: product.updatedAt updatedAt: product.updatedAt
}; };
@ -309,14 +278,42 @@ export async function getCollectionProducts({
reverse?: boolean; reverse?: boolean;
sortKey?: string; sortKey?: string;
}): Promise<ExProduct[]> { }): Promise<ExProduct[]> {
console.log(sortKey); console.log(collection);
const products = await find<Product>('products', {}); const products = await find<Product>('products', {});
return products.docs.map(reshapeProduct); return products.docs.map(reshapeProduct);
} }
const reshapeCategory = (category: Category): Collection => {
return {
handle: category.id,
title: category.title,
description: category.title,
seo: {
title: category.title,
description: category.title
},
path: `/search/${category.id}`,
updatedAt: category.updatedAt
};
};
export async function getCollections(): Promise<Collection[]> { export async function getCollections(): Promise<Collection[]> {
return COLLECTIONS; const categories = await find<Category>('categories', {});
return [
{
handle: '',
title: 'All',
description: 'All products',
seo: {
title: 'All',
description: 'All products'
},
path: '/search',
updatedAt: new Date().toISOString()
},
...categories.docs.map(reshapeCategory)
];
} }
export async function getMenu(handle: string): Promise<Menu[]> { export async function getMenu(handle: string): Promise<Menu[]> {
@ -328,11 +325,7 @@ export async function getMenu(handle: string): Promise<Menu[]> {
{ title: 'Medusa Blog', path: 'https://medusajs.com/blog' } { title: 'Medusa Blog', path: 'https://medusajs.com/blog' }
]; ];
case 'next-js-frontend-header-menu': case 'next-js-frontend-header-menu':
return [ return await getCollections();
{ title: 'All', path: '/search' },
{ title: 'Shirts', path: '/search/shirts' },
{ title: 'Stickers', path: '/search/stickers' }
];
default: default:
return []; return [];
} }

View File

@ -9,7 +9,7 @@
export interface Config { export interface Config {
collections: { collections: {
posts: Post; posts: Post;
tags: Tag; categories: Category;
media: Media; media: Media;
users: User; users: User;
options: Option; options: Option;
@ -43,7 +43,7 @@ export interface Post {
}; };
[k: string]: unknown; [k: string]: unknown;
} | null; } | null;
tags?: (string | Tag)[] | null; categories?: (string | Category)[] | null;
publishedAt?: string | null; publishedAt?: string | null;
authors?: (string | User)[] | null; authors?: (string | User)[] | null;
populatedAuthors?: populatedAuthors?:
@ -63,11 +63,11 @@ export interface Post {
} }
/** /**
* This interface was referenced by `Config`'s JSON-Schema * This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "tags". * via the `definition` "categories".
*/ */
export interface Tag { export interface Category {
id: string; id: string;
name: string; title: string;
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;
} }
@ -163,7 +163,8 @@ export interface Product {
| null; | null;
id?: string | null; id?: string | null;
}[]; }[];
tags?: (string | Tag)[] | null; categories?: (string | Category)[] | null;
tags?: string[] | null;
stripeProductID?: string | null; stripeProductID?: string | null;
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;