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:
Catalin Pinte
2022-10-30 20:41:21 +02:00
committed by GitHub
parent a5b367a747
commit c75b0fc001
316 changed files with 2482 additions and 2176 deletions

View File

@@ -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"