mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Dynamic API routes (#836)
* Add dynamic API endpoints * Add missing dependency * Update api handlers * Updates * Fix build errors * Update package.json * Add checkout endpoint parser & update errors * Update tsconfig.json * Update cart.ts * Update parser * Update errors.ts * Update errors.ts * Move to Edge runtime * Revert to local * Fix switchable runtimes * Make nodejs default runtime * Update pnpm-lock.yaml * Update handlers * Fix build errors * Change headers
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
selectDefaultOptionFromProduct,
|
||||
SelectedOptions,
|
||||
} from '../helpers'
|
||||
import ErrorMessage from '@components/ui/ErrorMessage'
|
||||
|
||||
interface ProductSidebarProps {
|
||||
product: Product
|
||||
@@ -19,6 +20,7 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ product, className }) => {
|
||||
const addItem = useAddItem()
|
||||
const { openSidebar, setSidebarView } = useUI()
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<null | Error>(null)
|
||||
const [selectedOptions, setSelectedOptions] = useState<SelectedOptions>({})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -28,6 +30,7 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ product, className }) => {
|
||||
const variant = getProductVariant(product, selectedOptions)
|
||||
const addToCart = async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
await addItem({
|
||||
productId: String(product.id),
|
||||
@@ -38,6 +41,13 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ product, className }) => {
|
||||
setLoading(false)
|
||||
} catch (err) {
|
||||
setLoading(false)
|
||||
if (err instanceof Error) {
|
||||
console.error(err)
|
||||
setError({
|
||||
...err,
|
||||
message: 'Could not add item to cart. Please try again.',
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +67,7 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ product, className }) => {
|
||||
<div className="text-accent-6 pr-1 font-medium text-sm">36 reviews</div>
|
||||
</div>
|
||||
<div>
|
||||
{error && <ErrorMessage error={error} className="my-5" />}
|
||||
{process.env.COMMERCE_CART_ENABLED && (
|
||||
<Button
|
||||
aria-label="Add to Cart"
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import cn from 'clsx'
|
||||
import { inherits } from 'util'
|
||||
import s from './ProductTag.module.css'
|
||||
|
||||
interface ProductTagProps {
|
||||
|
Reference in New Issue
Block a user