forked from crowetic/commerce
Updated hooks
This commit is contained in:
parent
0ad9ac0d5d
commit
6c378d98ea
@ -33,7 +33,7 @@ const cartApi: BigcommerceApiHandler = async (req, res, config) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json({ cart: result.data ?? null })
|
return res.status(200).json({ data: result.data ?? null })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create or add a product to the cart
|
// Create or add a product to the cart
|
||||||
@ -62,9 +62,7 @@ const cartApi: BigcommerceApiHandler = async (req, res, config) => {
|
|||||||
getCartCookie(name, data.id, config.cartCookieMaxAge)
|
getCartCookie(name, data.id, config.cartCookieMaxAge)
|
||||||
)
|
)
|
||||||
|
|
||||||
// There's no need to send any additional data here, the UI can use this response to display a
|
return res.status(200).json({ done: { data } })
|
||||||
// "success" for the operation and revalidate the GET request for this same endpoint right after.
|
|
||||||
return res.status(200).json({ done: true })
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
const message =
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
useCart as useCommerceCart,
|
useCart as useCommerceCart,
|
||||||
} from 'lib/commerce/cart'
|
} from 'lib/commerce/cart'
|
||||||
|
|
||||||
export type Cart = any
|
export type Cart = {}
|
||||||
|
|
||||||
export const CartProvider: FC = ({ children }) => {
|
export const CartProvider: FC = ({ children }) => {
|
||||||
return <CommerceCartProvider url="/api/cart">{children}</CommerceCartProvider>
|
return <CommerceCartProvider url="/api/cart">{children}</CommerceCartProvider>
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
import { Fetcher } from '@lib/commerce'
|
import type { Fetcher } from '@lib/commerce'
|
||||||
import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
|
import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
|
||||||
import { Cart } from '.'
|
import { Cart } from '.'
|
||||||
|
|
||||||
async function fetcher(fetch: Fetcher<Cart>, { item }: { item: any }) {
|
async function fetcher(fetch: Fetcher<Cart>, { item }: { item: any }) {
|
||||||
const res = await fetch({ url: '/api/cart' })
|
return fetch({ url: '/api/cart', method: 'POST', body: { item } })
|
||||||
|
|
||||||
// {
|
|
||||||
// method: 'POST',
|
|
||||||
// headers: {
|
|
||||||
// 'Content-Type': 'application/json',
|
|
||||||
// },
|
|
||||||
// body: JSON.stringify({ product }),
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useAddItem() {
|
export default function useAddItem() {
|
||||||
|
@ -23,11 +23,17 @@ async function getError(res: Response) {
|
|||||||
|
|
||||||
export const bigcommerceConfig: CommerceConfig = {
|
export const bigcommerceConfig: CommerceConfig = {
|
||||||
locale: 'en-us',
|
locale: 'en-us',
|
||||||
async fetcher({ url, query }) {
|
async fetcher({ url, method = 'GET', variables, body: bodyObj }) {
|
||||||
const res = await fetch(url!)
|
const hasBody = Boolean(variables || bodyObj)
|
||||||
|
const body = hasBody
|
||||||
|
? JSON.stringify(variables ? { variables } : bodyObj)
|
||||||
|
: undefined
|
||||||
|
const headers = hasBody ? { 'Content-Type': 'application/json' } : undefined
|
||||||
|
const res = await fetch(url!, { method, body, headers })
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
return res.json()
|
const { data } = await res.json()
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
throw await getError(res)
|
throw await getError(res)
|
||||||
|
@ -24,6 +24,9 @@ export type Fetcher<T> = (options: FetcherOptions) => T | Promise<T>
|
|||||||
export type FetcherOptions = {
|
export type FetcherOptions = {
|
||||||
url?: string
|
url?: string
|
||||||
query?: string
|
query?: string
|
||||||
|
method?: string
|
||||||
|
variables?: any
|
||||||
|
body?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CommerceProvider({ children, config }: CommerceProps) {
|
export function CommerceProvider({ children, config }: CommerceProps) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user