Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

useBytesSubmit.js

32 lines866 Bytes

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())
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 };