Open History to see the last commit
1import { useState } from 'react';
2β
3function sendBytesOptIn({ email, influencer, source, referral }) {
4 return fetch(`https://bytes.dev/api/bytes-optin-cors`, {
5 method: 'POST',
6 body: JSON.stringify({ email, influencer, source, referral }),
7 headers: {
8 Accept: 'application/json',
9 'Content-Type': 'application/json',
10 },
11 }).then((res) => res.json())
12}
13β
14export default function useBytesSubmit() {
15 const [state, setState] = useState("initial");
16 const [error, setError] = useState(null);
17β
18 const handleSubmit = (e) => {
19 e.preventDefault();
20 const email = e.target.email_address.value;
21 setState("loading");
22 sendBytesOptIn({ email, influencer: "tanstack" })
23 .then(() => {
24 setState("submitted");
25 })
26 .catch((err) => {
27 setError(err);
28 });
29 };
30β
31 return { handleSubmit, state, error };
32}