-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathNewsletterSubscription.tsx
178 lines (170 loc) · 6.58 KB
/
NewsletterSubscription.tsx
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { useState } from "react"
import * as React from "react"
import { faTimes, faEnvelopeOpenText } from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { SiteAnalytics } from "./SiteAnalytics.js"
import { TextInput } from "@ourworldindata/components"
import { NewsletterSubscriptionContext } from "./newsletter.js"
const analytics = new SiteAnalytics()
export const NewsletterSubscription = ({
context,
}: {
context?:
| NewsletterSubscriptionContext.Homepage
| NewsletterSubscriptionContext.Floating
}) => {
const [isOpen, setIsOpen] = useState(false)
const subscribeText = "Subscribe"
return (
<div className={`newsletter-subscription${isOpen ? " active" : ""}`}>
{isOpen && (
<>
<div
className="overlay"
onClick={() => {
setIsOpen(false)
}}
/>
<div className="box">
<NewsletterSubscriptionForm context={context} />
</div>
</>
)}
{isOpen ? (
<button
aria-label="Close subscription form"
className="prompt"
onClick={() => setIsOpen(false)}
>
<FontAwesomeIcon icon={faTimes} /> Close
</button>
) : (
<button
aria-label={subscribeText}
className="prompt"
data-track-note="dialog_open_newsletter"
onClick={() => {
setIsOpen(!isOpen)
}}
>
<FontAwesomeIcon icon={faEnvelopeOpenText} />
{subscribeText}
</button>
)}
</div>
)
}
export const NewsletterSubscriptionForm = ({
context,
}: {
context?: NewsletterSubscriptionContext
}) => {
const DATA_INSIGHTS = "16"
const BIWEEKLY = "2"
const idDataInsights = `mce-group[85302]-85302-0${
context ? "-" + context : ""
}`
const idBiweekly = `mce-group[85302]-85302-1${context ? "-" + context : ""}`
const [frequencies, setFrequencies] = useState([DATA_INSIGHTS, BIWEEKLY])
const isSubmittable = frequencies.length !== 0
const updateFrequencies = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.checked) {
setFrequencies([e.target.value, ...frequencies])
} else {
setFrequencies(
frequencies.filter((frequency) => frequency !== e.target.value)
)
}
}
return (
<form
action="https://ourworldindata.us8.list-manage.com/subscribe/post?u=18058af086319ba6afad752ec&id=2e166c1fc1"
method="post"
id="mc-embedded-subscribe-form"
name="mc-embedded-subscribe-form"
target="_blank"
onSubmit={() =>
analytics.logSiteFormSubmit(
"newsletter-subscribe",
`Subscribe [${context ?? "other-contexts"}]`
)
}
>
<span className="NewsletterSubscriptionForm__header">
Receive our latest work by email.
</span>
<div className="owid-checkbox-block">
<input
type="checkbox"
value={DATA_INSIGHTS}
name={`group[85302][${DATA_INSIGHTS}]`}
id={idDataInsights}
checked={frequencies.includes(DATA_INSIGHTS)}
onChange={updateFrequencies}
/>
<label htmlFor={idDataInsights}>
<div className="label-title">Daily Data Insights</div>
<div className="label-text">
Receive our bite-sized insights on how the world is
changing, every weekday.
</div>
</label>
</div>
<div className="owid-checkbox-block">
<input
type="checkbox"
value={BIWEEKLY}
name={`group[85302][${BIWEEKLY}]`}
id={idBiweekly}
checked={frequencies.includes(BIWEEKLY)}
onChange={updateFrequencies}
/>
<label htmlFor={idBiweekly}>
<div className="label-title">Biweekly Digest</div>
<div className="label-text">
Receive an overview of our recent work and highlights of
our other work every two weeks.
</div>
</label>
</div>
{frequencies.length === 0 && (
<div className="alert">Please select at least one option.</div>
)}
<div className="NewsletterSubscription__email-submit">
<TextInput
placeholder="Your email address"
type="email"
className="NewsletterSubscription__email sentry-mask"
name="EMAIL"
required={true}
/>
<button
aria-label="Subscribe to newsletter"
disabled={!isSubmittable}
onClick={() =>
analytics.logSiteClick(
"newsletter-subscribe",
`Subscribe [${context ?? "other-contexts"}]`
)
}
className="NewsletterSubscription__submit"
>
Subscribe
</button>
</div>
{/* This hidden field should not be the last element in the form as long as we use the row-gap mixin
to space elements vertically. When placed as the last element of the form, this hidden element becomes
the target of the :last-child selector of the row-gap mixin, when it should be applied to the last visible
element instead */}
<input
type="hidden"
name="b_18058af086319ba6afad752ec_2e166c1fc1"
tabIndex={-1}
/>
<div className="NewsletterSubscription__privacy">
By subscribing you are agreeing to the terms of our{" "}
<a href="/privacy-policy">privacy policy</a>.
</div>
</form>
)
}