Skip to content

Commit 896a050

Browse files
committed
Add bytes email signup
1 parent 9d112af commit 896a050

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/components/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import Helmet from "react-helmet";
3-
import { StaticQuery, graphql, withPrefix } from "gatsby";
3+
import { StaticQuery, graphql } from "gatsby";
44
import ogImage from "./../../static/img/og-image-2.png";
55
import "./global.css";
66

src/components/EmailSignup.js

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,59 @@
11
import React, { useState } from "react";
2+
import { useStaticQuery } from "gatsby";
23
import fetch from "unfetch";
34
import styled from "styled-components";
5+
import toast from "react-hot-toast";
46
import analytics from "./../utils/analytics.js";
57

8+
function sendBytesOptIn({ email, source }) {
9+
return fetch(`https://bytes.dev/api/bytes-optin-cors`, {
10+
method: "POST",
11+
body: JSON.stringify({ email, source }),
12+
headers: {
13+
Accept: "application/json",
14+
"Content-Type": "application/json",
15+
},
16+
}).then((res) => res.json());
17+
}
18+
19+
function useBytesCount() {
20+
const {
21+
bytes: { subcount },
22+
} = useStaticQuery(graphql`
23+
query subCountQuery {
24+
bytes {
25+
subcount
26+
}
27+
}
28+
`);
29+
30+
return subcount;
31+
}
32+
633
const EmailSignup = () => {
34+
const subcount = useBytesCount();
35+
const [isLoading, setIsLoading] = useState(false);
736
const [subscribed, setSubscribed] = useState(false);
837
const [email, setEmail] = useState("");
938

1039
const subscribe = (event) => {
1140
event.preventDefault();
1241
if (!email) return;
13-
analytics.track("subscribe");
14-
setSubscribed(true);
15-
return fetch("https://usehooks-next.vercel.app/api/subscribe", {
16-
method: "POST",
17-
headers: {
18-
"Content-Type": "application/json",
19-
},
20-
body: JSON.stringify({ email }),
21-
}).then((r) => r.json());
42+
setIsLoading(true);
43+
return sendBytesOptIn({ email, source: "useHooks" }).then((res) => {
44+
if (res.error) {
45+
setEmail("");
46+
setIsLoading(false);
47+
return toast.error(
48+
"There was an error. Check to see if your email is correct."
49+
);
50+
}
51+
analytics.track("subscribe");
52+
setSubscribed(true);
53+
setIsLoading(false);
54+
setEmail("");
55+
return toast.success(`Check your email to confirm your subscription.`);
56+
});
2257
};
2358

2459
return (
@@ -33,12 +68,21 @@ const EmailSignup = () => {
3368
</div>
3469
) : (
3570
<>
36-
<Title>
71+
<h4 className="subtitle is-5">
3772
<span role="img" aria-label="letter">
3873
📩
3974
</span>
40-
&nbsp;&nbsp;Get new recipes in your inbox
41-
</Title>
75+
&nbsp;&nbsp;Subscribe to Bytes
76+
</h4>
77+
<p>
78+
Most newsletters are terrible. Thats why we created Bytes. Our
79+
goal was to create a JavaScript newsletter that was both
80+
educational and entertaining. <b>{subcount.toLocaleString()}</b>{" "}
81+
subscribers and an almost 50% weekly open rate later, it looks
82+
like we did it...
83+
</p>
84+
<br />
85+
4286
<form onSubmit={subscribe}>
4387
<div className="field has-addons">
4488
<div className="control is-expanded">
@@ -53,26 +97,28 @@ const EmailSignup = () => {
5397
</div>
5498
<div className="control">
5599
<button
100+
disabled={isLoading}
56101
className="button is-primary has-text-weight-semibold"
57102
type="submit"
58103
>
59-
Subscribe
104+
{isLoading ? "Loading..." : "Subscribe"}
60105
</button>
61106
</div>
62107
</div>
63108
</form>
64-
<Extra>Join 7,031 subscribers. No spam ever.</Extra>
109+
<Extra>
110+
Join {subcount.toLocaleString()} subscribers. No spam ever. <br />
111+
<a href="https://bytes.dev/archives" target="_blank">
112+
See the most recent issue.
113+
</a>
114+
</Extra>
65115
</>
66116
)}
67117
</div>
68118
</div>
69119
);
70120
};
71121

72-
const Title = styled("div").attrs({ className: "subtitle is-5" })`
73-
margin-bottom: 1.2rem;
74-
`;
75-
76122
const Extra = styled("div")`
77123
margin-top: 1rem;
78124
font-size: 0.8rem;

0 commit comments

Comments
 (0)