forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseBytesSubmit.tsx
More file actions
42 lines (38 loc) · 949 Bytes
/
Copy pathuseBytesSubmit.tsx
File metadata and controls
42 lines (38 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useState } from 'react'
function sendBytesOptIn({
email,
influencer,
source,
referral,
}: {
email: string
influencer: string
source?: string
referral?: string
}) {
return fetch(`https://bytes.dev/api/bytes-optin-cors`, {
method: 'POST',
body: JSON.stringify({ email, influencer, source, referral }),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then((res) => res.json())
}
export default function useBytesSubmit() {
const [state, setState] = useState('initial')
const [error, setError] = useState(null)
const handleSubmit = (e: any) => {
e.preventDefault()
const email = e.target.email_address.value
setState('loading')
sendBytesOptIn({ email, influencer: 'tanstack' })
.then(() => {
setState('submitted')
})
.catch((err) => {
setError(err)
})
}
return { handleSubmit, state, error }
}