Allow custom swr

This commit is contained in:
Luis Alvarez 2020-10-09 17:18:02 -05:00
parent d67c728e80
commit 939bc388bb
2 changed files with 6 additions and 6 deletions

View File

@ -23,7 +23,8 @@ export function extendHook(customFetcher: typeof fetcher) {
const useCart = () => {
const cart = useCommerceCart<Cart | null>(
[defaultOpts.url, undefined],
customFetcher
customFetcher,
{ revalidateOnFocus: false }
)
// Uses a getter to only calculate the prop when required

View File

@ -1,4 +1,4 @@
import useSWR, { responseInterface } from 'swr'
import useSWR, { responseInterface, ConfigInterface } from 'swr'
import Cookies from 'js-cookie'
import { HookDeps, HookFetcher } from '../utils/types'
import { useCommerce } from '..'
@ -9,16 +9,15 @@ export type CartResponse<C> = responseInterface<C, Error> & {
export function useCart<T>(
deps: [string | undefined, string | undefined, ...HookDeps[]],
fetcherFn: HookFetcher<T, HookDeps[]>
fetcherFn: HookFetcher<T, HookDeps[]>,
swrOptions?: ConfigInterface<T | null>
) {
const { fetcherRef, cartCookie } = useCommerce()
const fetcher = (url?: string, query?: string, ...args: HookDeps[]) =>
Cookies.get(cartCookie)
? fetcherFn({ url, query }, args, fetcherRef.current)
: null
const response = useSWR(deps, fetcher, {
revalidateOnFocus: false,
})
const response = useSWR(deps, fetcher, swrOptions)
return Object.assign(response, { isEmpty: true }) as CartResponse<T>
}