-
Notifications
You must be signed in to change notification settings - Fork 3
/
Newsletter.tsx
executable file
·139 lines (131 loc) · 4.31 KB
/
Newsletter.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
import React, { useState } from "react"
import addToMailchimp from "gatsby-plugin-mailchimp"
import {
Button,
Container,
CircularProgress,
Grid,
Theme,
createStyles,
WithStyles,
withStyles,
TextField,
} from "@material-ui/core"
import EmailIcon from "@material-ui/icons/Email"
import { ContainerProps } from "@material-ui/core/Container"
import Text from "components/Typography/Text"
const styles = (theme: Theme) =>
createStyles({
root: {
marginTop: theme.spacing(2),
backgroundColor: "white",
padding: theme.spacing(2),
paddingLeft: theme.spacing(3),
width: "100%",
borderRadius: theme.shape.borderRadius,
},
textField: {
marginBottom: theme.spacing(1),
[theme.breakpoints.down("xs")]: {
marginLeft: theme.spacing(2),
},
},
icon: {
marginTop: theme.spacing(1),
},
button: {
height: "100%",
},
})
type Props = WithStyles<typeof styles> & {
maxWidth?: ContainerProps["maxWidth"]
}
function Newsletter(props: Props) {
const { classes, maxWidth = "lg" } = props
const [email, setEmail] = useState<string>("")
const [msg, setMsg] = useState<React.ReactNode>()
const [disabled, setDisabled] = useState<boolean>(false)
const handleChange = (event: React.FormEvent<EventTarget>) =>
setEmail((event.target as HTMLInputElement).value)
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault()
setDisabled(true)
setMsg(
<Text align="center">
Sending... <CircularProgress size={20} />
</Text>
)
const response = await addToMailchimp(email)
if (response.result === "error") {
if (response.msg.toLowerCase().includes("already subscribed")) {
setMsg(
<Text color="error" align="center">
You are already on this list!
</Text>
)
} else {
setMsg(
<Text color="error" align="center">
Some error occured while subscribing to list.
</Text>
)
}
setDisabled(false)
} else {
setMsg(
<Text color="success" align="center">
Successfully added to list! Please check your email and
confirm registration.
</Text>
)
}
}
return (
<Container maxWidth={maxWidth} className={classes.root}>
<form onSubmit={handleSubmit}>
<Grid
container
spacing={2}
alignItems="center"
alignContent="center"
justify="center"
>
<Grid item xs={1}>
<EmailIcon className={classes.icon} />
</Grid>
<Grid item xs={10} sm={8}>
<TextField
required
className={classes.textField}
label="Email"
name="email"
fullWidth
value={email}
onChange={handleChange}
/>
</Grid>
<Grid item xs={12} sm={3}>
<Button
disabled={disabled}
type="submit"
className={classes.button}
fullWidth
variant="contained"
color="secondary"
>
Subscribe
</Button>
</Grid>
{msg !== undefined ? (
<Grid item xs={12}>
{msg}
</Grid>
) : (
<></>
)}
</Grid>
</form>
</Container>
)
}
export default withStyles(styles)(Newsletter)