-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
73 lines (68 loc) · 1.62 KB
/
index.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
import React from "react";
import PropTypes from "prop-types";
import jsonp from "jsonp";
import toQueryString from "to-querystring";
import SimpleForm from "./SimpleForm";
const getAjaxUrl = url => url.replace("/post?", "/post-json?");
class MailchimpSubscribe extends React.Component {
state = {
status: null,
message: null
};
subscribe = data => {
const params = toQueryString(data);
const url = getAjaxUrl(this.props.url) + "&" + params;
this.setState(
{
status: "sending",
message: null
},
() =>
jsonp(
url,
{
param: "c"
},
(err, data) => {
if (err) {
this.setState({
status: "error",
message: err
});
} else if (data.result !== "success") {
this.setState({
status: "error",
message: data.msg
});
} else {
this.setState({
status: "success",
message: data.msg
});
}
}
)
);
};
render() {
return this.props.render({
subscribe: this.subscribe,
status: this.state.status,
message: this.state.message
});
}
}
MailchimpSubscribe.propTypes = {
render: PropTypes.func,
url: PropTypes.string.isRequired
};
MailchimpSubscribe.defaultProps = {
render: ({ subscribe, status, message }) => (
<SimpleForm
status={status}
message={message}
onSubmitted={formData => subscribe(formData)}
/>
)
};
export default MailchimpSubscribe;