This commit is contained in:
Kristian Duda 2024-06-28 08:21:04 +02:00
parent 8b1218fc35
commit ccf6d51eee
2 changed files with 53 additions and 61 deletions

View File

@ -1,3 +1,5 @@
import { Collection, Page } from 'lib/shopify/types';
export type SortFilterItem = { export type SortFilterItem = {
title: string; title: string;
slug: string | null; slug: string | null;
@ -29,3 +31,31 @@ 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,4 +1,4 @@
import { SHOPIFY_GRAPHQL_API_ENDPOINT, TAGS } from 'lib/constants'; import { COLLECTIONS, 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 { Media, Option, Product, Tag } from 'lib/shopify/payload-types';
import { isShopifyError } from 'lib/type-guards'; import { isShopifyError } from 'lib/type-guards';
@ -13,8 +13,7 @@ import {
removeFromCartMutation removeFromCartMutation
} from './mutations/cart'; } from './mutations/cart';
import { getCartQuery } from './queries/cart'; import { getCartQuery } from './queries/cart';
import { getCollectionQuery, getCollectionsQuery } from './queries/collection'; import { getPageQuery } from './queries/page';
import { getPageQuery, getPagesQuery } from './queries/page';
import { import {
Cart, Cart,
Collection, Collection,
@ -30,11 +29,8 @@ import {
ShopifyCart, ShopifyCart,
ShopifyCartOperation, ShopifyCartOperation,
ShopifyCollection, ShopifyCollection,
ShopifyCollectionOperation,
ShopifyCollectionsOperation,
ShopifyCreateCartOperation, ShopifyCreateCartOperation,
ShopifyPageOperation, ShopifyPageOperation,
ShopifyPagesOperation,
ShopifyRemoveFromCartOperation, ShopifyRemoveFromCartOperation,
ShopifyUpdateCartOperation ShopifyUpdateCartOperation
} from './types'; } from './types';
@ -218,15 +214,7 @@ 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> {
const res = await shopifyFetch<ShopifyCollectionOperation>({ return COLLECTIONS.find((collection) => collection.handle === handle);
query: getCollectionQuery,
tags: [TAGS.collections],
variables: {
handle
}
});
return reshapeCollection(res.body.data.collection);
} }
const reshapeImage = (media: Media): Image => { const reshapeImage = (media: Media): Image => {
@ -321,54 +309,33 @@ export async function getCollectionProducts({
reverse?: boolean; reverse?: boolean;
sortKey?: string; sortKey?: string;
}): Promise<ExProduct[]> { }): Promise<ExProduct[]> {
console.log(sortKey);
const products = await find<Product>('products', {}); const products = await find<Product>('products', {});
return products.docs.map(reshapeProduct); return products.docs.map(reshapeProduct);
} }
export async function getCollections(): Promise<Collection[]> { export async function getCollections(): Promise<Collection[]> {
const res = await shopifyFetch<ShopifyCollectionsOperation>({ return COLLECTIONS;
query: getCollectionsQuery,
tags: [TAGS.collections]
});
const shopifyCollections = removeEdgesAndNodes(res.body?.data?.collections);
const collections = [
{
handle: '',
title: 'All',
description: 'All products',
seo: {
title: 'All',
description: 'All products'
},
path: '/search',
updatedAt: new Date().toISOString()
},
// Filter out the `hidden` collections.
// Collections that start with `hidden-*` need to be hidden on the search page.
...reshapeCollections(shopifyCollections).filter(
(collection) => !collection.handle.startsWith('hidden')
)
];
return collections;
} }
export async function getMenu(handle: string): Promise<Menu[]> { export async function getMenu(handle: string): Promise<Menu[]> {
return []; switch (handle) {
// const res = await shopifyFetch<ShopifyMenuOperation>({ case 'next-js-frontend-footer-menu':
// query: getMenuQuery, return [
// tags: [TAGS.collections], { title: 'About Medusa', path: 'https://medusajs.com/' },
// variables: { { title: 'Medusa Docs', path: 'https://docs.medusajs.com/' },
// handle { title: 'Medusa Blog', path: 'https://medusajs.com/blog' }
// } ];
// }); case 'next-js-frontend-header-menu':
return [
// return ( { title: 'All', path: '/search' },
// res.body?.data?.menu?.items.map((item: { title: string; url: string }) => ({ { title: 'Shirts', path: '/search/shirts' },
// title: item.title, { title: 'Stickers', path: '/search/stickers' }
// path: item.url.replace(domain, '').replace('/collections', '/search').replace('/pages', '') ];
// })) || [] default:
// ); return [];
}
} }
export async function getPage(handle: string): Promise<Page> { export async function getPage(handle: string): Promise<Page> {
@ -382,12 +349,7 @@ export async function getPage(handle: string): Promise<Page> {
} }
export async function getPages(): Promise<Page[]> { export async function getPages(): Promise<Page[]> {
const res = await shopifyFetch<ShopifyPagesOperation>({ return [];
query: getPagesQuery,
cache: 'no-store'
});
return removeEdgesAndNodes(res.body.data.pages);
} }
export async function getProduct(handle: string): Promise<ExProduct | undefined> { export async function getProduct(handle: string): Promise<ExProduct | undefined> {