4
0
forked from crowetic/commerce

Added useLogout hook

This commit is contained in:
Luis Alvarez 2020-10-22 17:29:01 -05:00
parent 79d0c0a0cb
commit f81c507157
4 changed files with 42 additions and 4 deletions

View File

@ -0,0 +1,35 @@
import { useCallback } from 'react'
import type { HookFetcher } from '@lib/commerce/utils/types'
import useCommerceLogout from '@lib/commerce/use-logout'
const defaultOpts = {
url: '/api/bigcommerce/customers/logout',
method: 'GET',
}
export const fetcher: HookFetcher<null> = (options, _, fetch) => {
return fetch({
...defaultOpts,
...options,
})
}
export function extendHook(customFetcher: typeof fetcher) {
const useLogout = () => {
const fn = useCommerceLogout<null>(defaultOpts, customFetcher)
return useCallback(
async function login() {
const data = await fn(null)
return data
},
[fn]
)
}
useLogout.extend = extendHook
return useLogout
}
export default extendHook(fetcher)

View File

@ -0,0 +1,5 @@
import useAction from './utils/use-action'
const useLogout = useAction
export default useLogout

View File

@ -9,7 +9,7 @@ export type FetcherOptions = {
body?: any
}
export type HookFetcher<T, Input> = (
export type HookFetcher<T, Input = null> = (
options: HookFetcherOptions | null,
input: Input,
fetch: Fetcher<T>
@ -22,5 +22,3 @@ export type HookFetcherOptions = {
}
export type HookInput = [string, string | number | undefined][]
export type HookDeps = string | number | undefined[]

View File

@ -2,7 +2,7 @@ import { useCallback } from 'react'
import type { HookFetcher, HookFetcherOptions } from './types'
import { useCommerce } from '..'
export default function useAction<T, Input>(
export default function useAction<T, Input = null>(
options: HookFetcherOptions,
fetcher: HookFetcher<T, Input>
) {