From e2b26715b9b8309a5012a92efda4f966d1434504 Mon Sep 17 00:00:00 2001
From: Luis Alvarez <luis@vercel.com>
Date: Tue, 20 Oct 2020 13:49:37 -0500
Subject: [PATCH] use-signup

---
 lib/bigcommerce/use-signup.tsx    | 52 +++++++++++++++++++++++++++++++
 lib/commerce/use-signup.tsx       |  5 +++
 lib/commerce/utils/use-action.tsx |  4 +--
 3 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 lib/bigcommerce/use-signup.tsx
 create mode 100644 lib/commerce/use-signup.tsx

diff --git a/lib/bigcommerce/use-signup.tsx b/lib/bigcommerce/use-signup.tsx
new file mode 100644
index 000000000..699f0d9b1
--- /dev/null
+++ b/lib/bigcommerce/use-signup.tsx
@@ -0,0 +1,52 @@
+import { useCallback } from 'react'
+import { HookFetcher } from '@lib/commerce/utils/types'
+import useCommerceSignup from '@lib/commerce/use-signup'
+import type { CreateCustomerBody } from './api/customers'
+
+const defaultOpts = {
+  url: '/api/bigcommerce/customers',
+  method: 'POST',
+}
+
+export type SignupInput = CreateCustomerBody
+
+export const fetcher: HookFetcher<undefined, CreateCustomerBody> = (
+  options,
+  { firstName, lastName, email, password },
+  fetch
+) => {
+  if (!(firstName && lastName && email && password)) {
+    throw new Error(
+      'A first name, last name, email and password are required to signup'
+    )
+  }
+
+  return fetch({
+    url: options?.url ?? defaultOpts.url,
+    method: options?.method ?? defaultOpts.method,
+    body: { firstName, lastName, email, password },
+  })
+}
+
+export function extendHook(customFetcher: typeof fetcher) {
+  const useSignup = () => {
+    const fn = useCommerceSignup<undefined, SignupInput>(
+      defaultOpts,
+      customFetcher
+    )
+
+    return useCallback(
+      async function signup(input: SignupInput) {
+        const data = await fn(input)
+        return data
+      },
+      [fn]
+    )
+  }
+
+  useSignup.extend = extendHook
+
+  return useSignup
+}
+
+export default extendHook(fetcher)
diff --git a/lib/commerce/use-signup.tsx b/lib/commerce/use-signup.tsx
new file mode 100644
index 000000000..08ddb22c0
--- /dev/null
+++ b/lib/commerce/use-signup.tsx
@@ -0,0 +1,5 @@
+import useAction from './utils/use-action'
+
+const useSignup = useAction
+
+export default useSignup
diff --git a/lib/commerce/utils/use-action.tsx b/lib/commerce/utils/use-action.tsx
index ef68a7641..e60cae06b 100644
--- a/lib/commerce/utils/use-action.tsx
+++ b/lib/commerce/utils/use-action.tsx
@@ -9,9 +9,7 @@ export default function useAction<T, Input>(
   const { fetcherRef } = useCommerce()
 
   return useCallback(
-    function addItem(input: Input) {
-      return fetcher(options, input, fetcherRef.current)
-    },
+    (input: Input) => fetcher(options, input, fetcherRef.current),
     [fetcher]
   )
 }