1
0
mirror of https://github.com/vercel/commerce.git synced 2025-05-20 16:36:59 +00:00

product and product recommendations

This commit is contained in:
Jieren Chen 2024-09-08 12:27:59 -04:00
parent 4bb59981b4
commit 5812416bd3
No known key found for this signature in database
GPG Key ID: 2FF322D21B5DB10B
4 changed files with 28 additions and 34 deletions
app/product/[handle]
lib

@ -7,7 +7,7 @@ import { Gallery } from 'components/product/gallery';
import { ProductProvider } from 'components/product/product-context'; import { ProductProvider } from 'components/product/product-context';
import { ProductDescription } from 'components/product/product-description'; import { ProductDescription } from 'components/product/product-description';
import { HIDDEN_PRODUCT_TAG } from 'lib/constants'; import { HIDDEN_PRODUCT_TAG } from 'lib/constants';
import { getProduct, getProductRecommendations } from 'lib/shopify'; import { getProduct, getProductRecommendations } from 'lib/fourthwall';
import { Image } from 'lib/shopify/types'; import { Image } from 'lib/shopify/types';
import Link from 'next/link'; import Link from 'next/link';
import { Suspense } from 'react'; import { Suspense } from 'react';

@ -1,6 +1,6 @@
import { Cart, Menu, Product } from "lib/shopify/types"; import { Cart, Menu, Product } from "lib/shopify/types";
import { reshapeCart, reshapeProducts } from "./reshape"; import { reshapeCart, reshapeProduct, reshapeProducts } from "./reshape";
import { FourthwallCart } from "./types"; import { FourthwallCart, FourthwallProduct } from "./types";
/** /**
* Helpers * Helpers
@ -72,7 +72,7 @@ export async function getCollectionProducts({
reverse?: boolean; reverse?: boolean;
sortKey?: string; sortKey?: string;
}): Promise<Product[]> { }): Promise<Product[]> {
const res = await fourthwallGet<{results: any[]}>(`${process.env.FW_URL}/api/public/v1.0/collections/${collection}/products?secret=${process.env.FW_SECRET}`, { const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${process.env.FW_URL}/api/public/v1.0/collections/${collection}/products?secret=${process.env.FW_SECRET}`, {
headers: { headers: {
'X-ShopId': process.env.FW_SHOPID || '' 'X-ShopId': process.env.FW_SHOPID || ''
} }
@ -87,6 +87,27 @@ export async function getCollectionProducts({
return reshapeProducts(res.body.results); return reshapeProducts(res.body.results);
} }
/**
* Product operations
*/
export async function getProduct(handle: string): Promise<Product | undefined> {
// TODO: replace with real URL
const res = await fourthwallGet<{results: FourthwallProduct[]}>(`${process.env.FW_URL}/api/public/v1.0/collections/${process.env.FW_COLLECTION}/products?secret=${process.env.FW_SECRET}`, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
}
});
return res.body.results.filter((product) => {
return product.slug === handle
}).map((p: any) => reshapeProduct(p))[0];
}
export async function getProductRecommendations(productId: string): Promise<Product[]> {
// TODO: replace with real URL
return [];
}
/** /**
* Cart operations * Cart operations
*/ */

@ -38,7 +38,7 @@ export const reshapeProducts = (products: FourthwallProduct[]) => {
return reshapedProducts; return reshapedProducts;
}; };
const reshapeProduct = (product: FourthwallProduct): Product | undefined => { export const reshapeProduct = (product: FourthwallProduct): Product | undefined => {
if (!product) { if (!product) {
return undefined; return undefined;
} }
@ -117,6 +117,7 @@ const reshapeCartItem = (item: FourthwallCartItem): CartItem => {
// TODO: Stubbed out // TODO: Stubbed out
selectedOptions: [], selectedOptions: [],
product: { product: {
// TODO: need this product info in model
id: 'TT', id: 'TT',
handle: 'TT', handle: 'TT',
title: 'TT', title: 'TT',

@ -10,8 +10,6 @@ import {
} from './queries/collection'; } from './queries/collection';
import { getPageQuery, getPagesQuery } from './queries/page'; import { getPageQuery, getPagesQuery } from './queries/page';
import { import {
getProductQuery,
getProductRecommendationsQuery,
getProductsQuery getProductsQuery
} from './queries/product'; } from './queries/product';
import { import {
@ -28,8 +26,6 @@ import {
ShopifyPageOperation, ShopifyPageOperation,
ShopifyPagesOperation, ShopifyPagesOperation,
ShopifyProduct, ShopifyProduct,
ShopifyProductOperation,
ShopifyProductRecommendationsOperation,
ShopifyProductsOperation ShopifyProductsOperation
} from './types'; } from './types';
@ -243,30 +239,6 @@ export async function getPages(): Promise<Page[]> {
return removeEdgesAndNodes(res.body.data.pages); return removeEdgesAndNodes(res.body.data.pages);
} }
export async function getProduct(handle: string): Promise<Product | undefined> {
const res = await shopifyFetch<ShopifyProductOperation>({
query: getProductQuery,
tags: [TAGS.products],
variables: {
handle
}
});
return reshapeProduct(res.body.data.product, false);
}
export async function getProductRecommendations(productId: string): Promise<Product[]> {
const res = await shopifyFetch<ShopifyProductRecommendationsOperation>({
query: getProductRecommendationsQuery,
tags: [TAGS.products],
variables: {
productId
}
});
return reshapeProducts(res.body.data.productRecommendations);
}
export async function getProducts({ export async function getProducts({
query, query,
reverse, reverse,