From f81c5071570bc33a4def036be6d58fac8261dc21 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 22 Oct 2020 17:29:01 -0500 Subject: [PATCH] Added useLogout hook --- lib/bigcommerce/use-logout.tsx | 35 +++++++++++++++++++++++++++++++ lib/commerce/use-logout.tsx | 5 +++++ lib/commerce/utils/types.ts | 4 +--- lib/commerce/utils/use-action.tsx | 2 +- 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 lib/bigcommerce/use-logout.tsx create mode 100644 lib/commerce/use-logout.tsx diff --git a/lib/bigcommerce/use-logout.tsx b/lib/bigcommerce/use-logout.tsx new file mode 100644 index 000000000..f4b5f9beb --- /dev/null +++ b/lib/bigcommerce/use-logout.tsx @@ -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 = (options, _, fetch) => { + return fetch({ + ...defaultOpts, + ...options, + }) +} + +export function extendHook(customFetcher: typeof fetcher) { + const useLogout = () => { + const fn = useCommerceLogout(defaultOpts, customFetcher) + + return useCallback( + async function login() { + const data = await fn(null) + return data + }, + [fn] + ) + } + + useLogout.extend = extendHook + + return useLogout +} + +export default extendHook(fetcher) diff --git a/lib/commerce/use-logout.tsx b/lib/commerce/use-logout.tsx new file mode 100644 index 000000000..ef3fc4135 --- /dev/null +++ b/lib/commerce/use-logout.tsx @@ -0,0 +1,5 @@ +import useAction from './utils/use-action' + +const useLogout = useAction + +export default useLogout diff --git a/lib/commerce/utils/types.ts b/lib/commerce/utils/types.ts index 0af754fc0..81f05da3e 100644 --- a/lib/commerce/utils/types.ts +++ b/lib/commerce/utils/types.ts @@ -9,7 +9,7 @@ export type FetcherOptions = { body?: any } -export type HookFetcher = ( +export type HookFetcher = ( options: HookFetcherOptions | null, input: Input, fetch: Fetcher @@ -22,5 +22,3 @@ export type HookFetcherOptions = { } export type HookInput = [string, string | number | undefined][] - -export type HookDeps = string | number | undefined[] diff --git a/lib/commerce/utils/use-action.tsx b/lib/commerce/utils/use-action.tsx index e60cae06b..24593383f 100644 --- a/lib/commerce/utils/use-action.tsx +++ b/lib/commerce/utils/use-action.tsx @@ -2,7 +2,7 @@ import { useCallback } from 'react' import type { HookFetcher, HookFetcherOptions } from './types' import { useCommerce } from '..' -export default function useAction( +export default function useAction( options: HookFetcherOptions, fetcher: HookFetcher ) {