mirror of
https://github.com/vercel/commerce.git
synced 2025-06-27 17:01:21 +00:00
fix: store api
This commit is contained in:
parent
b250d83534
commit
d5dee1c261
@ -38,7 +38,10 @@ export async function POST(req: NextRequest) {
|
|||||||
return NextResponse.json(cart, { status: 200 });
|
return NextResponse.json(cart, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to add item to cart', message: JSON.stringify(error) },
|
{
|
||||||
|
error: 'Failed to add item to cart',
|
||||||
|
message: JSON.stringify(error)
|
||||||
|
},
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,23 @@ export default function CheckoutReview() {
|
|||||||
const { checkout } = useCheckout();
|
const { checkout } = useCheckout();
|
||||||
|
|
||||||
const handleCreateOrder = async () => {
|
const handleCreateOrder = async () => {
|
||||||
const order = await fetch('/api/customer/order', {
|
try {
|
||||||
method: 'POST',
|
const order = await fetch('/api/customer/order', {
|
||||||
body: JSON.stringify({
|
method: 'POST',
|
||||||
billing_address: checkout?.billing,
|
body: JSON.stringify({
|
||||||
shipping_address: checkout?.shipping,
|
billing_address: checkout?.billing,
|
||||||
payment_method: checkout?.payment_method
|
shipping_address: checkout?.shipping,
|
||||||
})
|
payment_method: checkout?.payment_method
|
||||||
}).catch((err) => {
|
})
|
||||||
console.error('Error creating order', err);
|
});
|
||||||
});
|
if (!order.ok) {
|
||||||
|
const errorData = await order.json();
|
||||||
|
console.error('Error creating order:', errorData);
|
||||||
|
throw new Error(errorData.error || 'Failed to create order');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating order', error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<section className="mt-4 grid w-full gap-4 px-4 pb-4">
|
<section className="mt-4 grid w-full gap-4 px-4 pb-4">
|
||||||
|
@ -20,7 +20,8 @@ import Bg2 from '../assets/images/slide-bg-02.webp';
|
|||||||
|
|
||||||
async function Products({ category }: { category: Category }) {
|
async function Products({ category }: { category: Category }) {
|
||||||
const products: Product[] = await woocommerce.get('products', {
|
const products: Product[] = await woocommerce.get('products', {
|
||||||
category: category.id.toString()
|
category: category.id.toString(),
|
||||||
|
author: 1, // Use admin user to get all products
|
||||||
});
|
});
|
||||||
|
|
||||||
return <ThreeItemGrid products={products} />;
|
return <ThreeItemGrid products={products} />;
|
||||||
|
@ -5,7 +5,6 @@ import clsx from 'clsx';
|
|||||||
import { useProduct } from 'components/product/product-context';
|
import { useProduct } from 'components/product/product-context';
|
||||||
import { Product, ProductVariations } from 'lib/woocomerce/models/product';
|
import { Product, ProductVariations } from 'lib/woocomerce/models/product';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { toast } from 'sonner';
|
|
||||||
import { useCart } from './cart-context';
|
import { useCart } from './cart-context';
|
||||||
|
|
||||||
function SubmitButton({ disabled = false }: { disabled: boolean }) {
|
function SubmitButton({ disabled = false }: { disabled: boolean }) {
|
||||||
@ -45,18 +44,18 @@ export function AddToCart({
|
|||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
action={async () => {
|
action={async () => {
|
||||||
try {
|
const response = await fetch('/api/cart', {
|
||||||
const cart = await (
|
method: 'POST',
|
||||||
await fetch('/api/cart', {
|
body: JSON.stringify({ id: product.id, quantity: 1, variation })
|
||||||
method: 'POST',
|
});
|
||||||
body: JSON.stringify({ id: product.id, quantity: 1, variation })
|
|
||||||
})
|
if (!response.ok) {
|
||||||
).json();
|
console.error('Error adding to cart');
|
||||||
setNewCart(cart);
|
return;
|
||||||
toast('Item added to cart');
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cart = await response.json();
|
||||||
|
setNewCart(cart);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<SubmitButton disabled={variations?.length && !state.variation ? true : false} />
|
<SubmitButton disabled={variations?.length && !state.variation ? true : false} />
|
||||||
|
@ -45,8 +45,9 @@ function createStoreApiClient({
|
|||||||
method,
|
method,
|
||||||
url: baseURL + url,
|
url: baseURL + url,
|
||||||
data,
|
data,
|
||||||
headers
|
headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
@ -55,8 +56,9 @@ function createStoreApiClient({
|
|||||||
console.debug('Token expired, regenerating...');
|
console.debug('Token expired, regenerating...');
|
||||||
const newAuthToken = await regenerateAuthToken();
|
const newAuthToken = await regenerateAuthToken();
|
||||||
headers.Authorization = `Bearer ${newAuthToken}`;
|
headers.Authorization = `Bearer ${newAuthToken}`;
|
||||||
return _request(method, url, data);
|
return _request(method, url, data); // Retry the request with the new token
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Request failed with status ${error.response.status}: ${error.response.data.message}`
|
`Request failed with status ${error.response.status}: ${error.response.data.message}`
|
||||||
);
|
);
|
||||||
@ -76,6 +78,7 @@ function createStoreApiClient({
|
|||||||
params?: Record<string, string | number>
|
params?: Record<string, string | number>
|
||||||
): Promise<{ cart: Cart; cartToken?: string }> {
|
): Promise<{ cart: Cart; cartToken?: string }> {
|
||||||
const res = await _request('get', '/cart', { params });
|
const res = await _request('get', '/cart', { params });
|
||||||
|
|
||||||
return { cart: res.data, cartToken: res.headers['cart-token'] };
|
return { cart: res.data, cartToken: res.headers['cart-token'] };
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user