/
Newsletter.js
150 lines (139 loc) · 3.73 KB
/
Newsletter.js
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
import React from 'react'
import styled, { Box, up, css } from '@xstyled/styled-components'
import jsonp from 'jsonp'
import { Form } from 'react-final-form'
import { FORM_ERROR } from 'final-form'
import { Container } from '../components/Container'
import { useLangKey } from '../components/I18nContext'
import { useMustBeEmail, InputField, Button } from '../components/Form'
export const FormLayout = styled.div`
display: flex;
flex-direction: column;
margin: -2 -3;
${up(
'md',
css`
flex-direction: row;
`,
)}
`
const Teaser = styled.h2`
font-size: 26;
font-weight: 500;
color: lighter;
margin: 4 0;
`
const Success = styled.div`
text-align: center;
p:first-child {
font-size: 26;
color: lighter;
margin: 2 0;
}
p:last-child {
font-size: 18;
}
`
const locales = {
en: {
success: (
<>
<p>Thanks, one last thing...</p>
<p>
Please <strong>check your inbox</strong> to confirm your subscription!
</p>
</>
),
firstName: 'First Name',
teaser:
'Get emails from me about open source, business, learning and teaching.',
submit: 'Submit',
submitting: 'Submitting',
},
fr: {
success: (
<>
<p>Merci, une dernière chose...</p>
<p>
Pouvez-vous <strong>regarder votre boîte mail</strong> pour confirmer
votre inscription.
</p>
</>
),
firstName: 'Prénom',
teaser:
'Retrouvez toute l’actualité open source, business, workshops dans ma newsletter.',
submit: 'Envoyer',
submitting: 'Envoi en cours...',
},
}
export function Newsletter() {
const langKey = useLangKey()
const t = locales[langKey]
const mustBeEmail = useMustBeEmail()
function handleSubmit({ FNAME, EMAIL }) {
const params = new URLSearchParams(
'u=52fd0cbf3e5a6413c71ca38a5&id=25b7eb1ae7',
)
params.append('FNAME', FNAME)
params.append('EMAIL', EMAIL)
const url = `https://gregberge.us4.list-manage.com/subscribe/post-json?${params.toString()}`
return new Promise(resolve => {
jsonp(url, { param: 'c' }, error => {
if (error) {
resolve({ [FORM_ERROR]: error.message })
return
}
resolve()
})
})
}
return (
<Container my={6}>
<Form onSubmit={handleSubmit}>
{({ handleSubmit, submitting, submitSucceeded }) => (
<form noValidate onSubmit={handleSubmit}>
{submitSucceeded ? (
<Success>{t.success}</Success>
) : (
<>
<Teaser>{t.teaser}</Teaser>
<FormLayout>
<Box py={2} px={3}>
<InputField
name="FNAME"
label={t.firstName}
placeholder="Hubert"
/>
</Box>
<Box py={2} px={3}>
<InputField
name="EMAIL"
label="Email"
type="email"
placeholder="hubert@oss.com"
required
validate={mustBeEmail}
/>
</Box>
<Box
display="flex"
flexDirection="column"
justifyContent="flex-end"
flex="1 0 auto"
py={2}
px={3}
>
<Button type="submit" disabled={submitting}>
{submitting ? t.submitting : t.submit}
</Button>
</Box>
</FormLayout>
</>
)}
</form>
)}
</Form>
</Container>
)
}