forked from crowetic/commerce
Allow custom swr
This commit is contained in:
parent
d67c728e80
commit
939bc388bb
@ -23,7 +23,8 @@ export function extendHook(customFetcher: typeof fetcher) {
|
|||||||
const useCart = () => {
|
const useCart = () => {
|
||||||
const cart = useCommerceCart<Cart | null>(
|
const cart = useCommerceCart<Cart | null>(
|
||||||
[defaultOpts.url, undefined],
|
[defaultOpts.url, undefined],
|
||||||
customFetcher
|
customFetcher,
|
||||||
|
{ revalidateOnFocus: false }
|
||||||
)
|
)
|
||||||
|
|
||||||
// Uses a getter to only calculate the prop when required
|
// Uses a getter to only calculate the prop when required
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import useSWR, { responseInterface } from 'swr'
|
import useSWR, { responseInterface, ConfigInterface } from 'swr'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
import { HookDeps, HookFetcher } from '../utils/types'
|
import { HookDeps, HookFetcher } from '../utils/types'
|
||||||
import { useCommerce } from '..'
|
import { useCommerce } from '..'
|
||||||
@ -9,16 +9,15 @@ export type CartResponse<C> = responseInterface<C, Error> & {
|
|||||||
|
|
||||||
export function useCart<T>(
|
export function useCart<T>(
|
||||||
deps: [string | undefined, string | undefined, ...HookDeps[]],
|
deps: [string | undefined, string | undefined, ...HookDeps[]],
|
||||||
fetcherFn: HookFetcher<T, HookDeps[]>
|
fetcherFn: HookFetcher<T, HookDeps[]>,
|
||||||
|
swrOptions?: ConfigInterface<T | null>
|
||||||
) {
|
) {
|
||||||
const { fetcherRef, cartCookie } = useCommerce()
|
const { fetcherRef, cartCookie } = useCommerce()
|
||||||
const fetcher = (url?: string, query?: string, ...args: HookDeps[]) =>
|
const fetcher = (url?: string, query?: string, ...args: HookDeps[]) =>
|
||||||
Cookies.get(cartCookie)
|
Cookies.get(cartCookie)
|
||||||
? fetcherFn({ url, query }, args, fetcherRef.current)
|
? fetcherFn({ url, query }, args, fetcherRef.current)
|
||||||
: null
|
: null
|
||||||
const response = useSWR(deps, fetcher, {
|
const response = useSWR(deps, fetcher, swrOptions)
|
||||||
revalidateOnFocus: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
return Object.assign(response, { isEmpty: true }) as CartResponse<T>
|
return Object.assign(response, { isEmpty: true }) as CartResponse<T>
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user