From ae486fcfbcba30cdc4ac68d9b2d33ced927429ef Mon Sep 17 00:00:00 2001 From: Kristian Duda Date: Fri, 28 Jun 2024 08:43:31 +0200 Subject: [PATCH] categories --- lib/constants.ts | 30 -------------- lib/shopify/index.ts | 79 ++++++++++++++++-------------------- lib/shopify/payload-types.ts | 13 +++--- 3 files changed, 43 insertions(+), 79 deletions(-) diff --git a/lib/constants.ts b/lib/constants.ts index d41f7cfc4..56bc6cd12 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -1,5 +1,3 @@ -import { Collection, Page } from 'lib/shopify/types'; - export type SortFilterItem = { title: string; slug: string | null; @@ -31,31 +29,3 @@ export const TAGS = { export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const DEFAULT_OPTION = 'Default Title'; 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 - } -]; diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 331faf8ec..c4ee6be61 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -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 { 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 { ensureStartsWith } from 'lib/utils'; import { revalidateTag } from 'next/cache'; @@ -28,7 +28,6 @@ import { ShopifyAddToCartOperation, ShopifyCart, ShopifyCartOperation, - ShopifyCollection, ShopifyCreateCartOperation, ShopifyPageOperation, 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 { const res = await shopifyFetch({ query: createCartMutation, @@ -214,7 +186,8 @@ export async function getCart(cartId: string): Promise { } export async function getCollection(handle: string): Promise { - return COLLECTIONS.find((collection) => collection.handle === handle); + const category = await findByID('categories', handle); + return reshapeCategory(category); } 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 => { return { id: product.id, @@ -294,7 +263,7 @@ const reshapeProduct = (product: Product): ExProduct => { title: product.title, description: product.description }, - tags: reshapeTags(product.tags as Tag[]), + tags: product.tags ?? [], variants: reshapeVariants(product.variants), updatedAt: product.updatedAt }; @@ -309,14 +278,42 @@ export async function getCollectionProducts({ reverse?: boolean; sortKey?: string; }): Promise { - console.log(sortKey); + console.log(collection); const products = await find('products', {}); 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 { - return COLLECTIONS; + const categories = await find('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 { @@ -328,11 +325,7 @@ export async function getMenu(handle: string): Promise { { title: 'Medusa Blog', path: 'https://medusajs.com/blog' } ]; case 'next-js-frontend-header-menu': - return [ - { title: 'All', path: '/search' }, - { title: 'Shirts', path: '/search/shirts' }, - { title: 'Stickers', path: '/search/stickers' } - ]; + return await getCollections(); default: return []; } diff --git a/lib/shopify/payload-types.ts b/lib/shopify/payload-types.ts index fedcc2d6d..d45726bcd 100644 --- a/lib/shopify/payload-types.ts +++ b/lib/shopify/payload-types.ts @@ -9,7 +9,7 @@ export interface Config { collections: { posts: Post; - tags: Tag; + categories: Category; media: Media; users: User; options: Option; @@ -43,7 +43,7 @@ export interface Post { }; [k: string]: unknown; } | null; - tags?: (string | Tag)[] | null; + categories?: (string | Category)[] | null; publishedAt?: string | null; authors?: (string | User)[] | null; populatedAuthors?: @@ -63,11 +63,11 @@ export interface Post { } /** * 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; - name: string; + title: string; updatedAt: string; createdAt: string; } @@ -163,7 +163,8 @@ export interface Product { | null; id?: string | null; }[]; - tags?: (string | Tag)[] | null; + categories?: (string | Category)[] | null; + tags?: string[] | null; stripeProductID?: string | null; updatedAt: string; createdAt: string;