commerce/lib/cart.js

106 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-10-01 20:40:40 -05:00
import { useState, useCallback } from 'react'
import useSWR, { mutate } from 'swr'
2020-09-30 11:44:38 -05:00
async function getText(res) {
try {
2020-10-01 20:40:40 -05:00
return (await res.text()) || res.statusText
2020-09-30 11:44:38 -05:00
} catch (error) {
2020-10-01 20:40:40 -05:00
return res.statusText
2020-09-30 11:44:38 -05:00
}
}
async function getError(res) {
2020-10-01 20:40:40 -05:00
if (res.headers.get('Content-Type')?.includes('application/json')) {
const data = await res.json()
return data.errors[0]
2020-09-30 11:44:38 -05:00
}
2020-10-01 20:40:40 -05:00
return { message: await getText(res) }
2020-09-30 11:44:38 -05:00
}
async function fetcher(url) {
2020-10-01 20:40:40 -05:00
const res = await fetch(url)
2020-09-30 11:44:38 -05:00
if (res.status === 200) {
2020-10-01 20:40:40 -05:00
return res.json()
2020-09-30 11:44:38 -05:00
}
2020-10-01 20:40:40 -05:00
throw await getError(res)
2020-09-30 11:44:38 -05:00
}
export function useCart() {
2020-10-01 20:40:40 -05:00
return useSWR('/api/cart', fetcher)
2020-09-30 11:44:38 -05:00
}
export function useAddToCart() {
const [{ addingToCart, error }, setStatus] = useState({
addingToCart: false,
2020-10-01 20:40:40 -05:00
})
2020-09-30 11:44:38 -05:00
const addToCart = useCallback(async ({ product }) => {
2020-10-01 20:40:40 -05:00
setStatus({ addingToCart: true })
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
const res = await fetch('/api/cart', {
method: 'POST',
2020-09-30 11:44:38 -05:00
headers: {
2020-10-01 20:40:40 -05:00
'Content-Type': 'application/json',
2020-09-30 11:44:38 -05:00
},
body: JSON.stringify({ product }),
2020-10-01 20:40:40 -05:00
})
2020-09-30 11:44:38 -05:00
// Product added as expected
if (res.status === 200) {
2020-10-01 20:40:40 -05:00
setStatus({ addingToCart: false })
return mutate('/api/cart')
2020-09-30 11:44:38 -05:00
}
2020-10-01 20:40:40 -05:00
const error = await getError(res)
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
console.error('Adding product to cart failed with:', res.status, error)
setStatus({ addingToCart: false, error })
}, [])
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
return { addToCart, addingToCart, error }
2020-09-30 11:44:38 -05:00
}
export function useUpdateCart() {
const [{ updatingCart, error }, setStatus] = useState({
updatingCart: false,
2020-10-01 20:40:40 -05:00
})
2020-09-30 11:44:38 -05:00
const updateCart = useCallback(async ({ product, item }) => {
2020-10-01 20:40:40 -05:00
setStatus({ updatingCart: true })
2020-09-30 11:44:38 -05:00
const res = await fetch(
`/api/cart?itemId=${item.id}`,
product.quantity < 1
2020-10-01 20:40:40 -05:00
? { method: 'DELETE' }
2020-09-30 11:44:38 -05:00
: {
2020-10-01 20:40:40 -05:00
method: 'PUT',
2020-09-30 11:44:38 -05:00
headers: {
2020-10-01 20:40:40 -05:00
'Content-Type': 'application/json',
2020-09-30 11:44:38 -05:00
},
body: JSON.stringify({ product }),
}
2020-10-01 20:40:40 -05:00
)
2020-09-30 11:44:38 -05:00
// Product updated as expected
if (res.status === 200) {
2020-10-01 20:40:40 -05:00
setStatus({ updatingCart: false })
return mutate('/api/cart')
2020-09-30 11:44:38 -05:00
}
2020-10-01 20:40:40 -05:00
const error = await getError(res)
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
console.error('Update to cart failed with:', res.status, error)
setStatus({ updatingCart: false, error })
}, [])
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
return { updateCart, updatingCart, error }
2020-09-30 11:44:38 -05:00
}
export function useRemoveFromCart() {
2020-10-01 20:40:40 -05:00
const { updateCart, updatingCart, error } = useUpdateCart()
2020-09-30 11:44:38 -05:00
const removeFromCart = async ({ item }) => {
2020-10-01 20:40:40 -05:00
updateCart({ item, product: { quantity: 0 } })
}
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -05:00
return { removeFromCart, removingFromCart: updatingCart, error }
2020-09-30 11:44:38 -05:00
}