Skip to content

Commit 0a24043

Browse files
committed
Newsletter form
1 parent 885288c commit 0a24043

File tree

5 files changed

+82
-151
lines changed

5 files changed

+82
-151
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.next/
2+
out/
23
node_modules/
4+
35
/npm-debug.log
46
.DS_Store
5-
out/
7+
.env
8+
9+
# Local Netlify folder
10+
.netlify

components/NewsletterForm.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const NewletterForm = () => {
1111

1212
const handleSubmit = (event) => {
1313
event.preventDefault();
14-
const url = "https://tec-web.netlify.app/.netlify/functions/newsletter";
14+
const url = "/.netlify/functions/newsletter";
1515
const requestOptions = {
1616
method: "POST",
1717
headers: { "Content-Type": "application/json" },
@@ -25,7 +25,6 @@ const NewletterForm = () => {
2525
.catch((error) => {
2626
setSubmitted(true);
2727
setSuccess(false);
28-
console.log("Form submit error", error);
2928
});
3029
};
3130

@@ -46,13 +45,13 @@ const NewletterForm = () => {
4645
<form onSubmit={handleSubmit} className="flex flex-col sm:flex-row">
4746
<input
4847
type="email"
49-
class="p-4 border-t mr-0 border-b border-l text-gray-300 border-white bg-gray-800 uppercase font-semibold flex-grow mb-5 sm:mb-0 sm:w-auto"
48+
className="flex-grow p-4 mb-5 mr-0 font-semibold text-gray-300 uppercase bg-gray-800 border-t border-b border-l border-white sm:mb-0 sm:w-auto"
5049
placeholder="Your email address"
5150
onChange={handleEmailChange}
5251
value={email}
5352
/>
5453
<button
55-
class="px-8 bg-white text-gray-800 font-semibold p-4 uppercase"
54+
className="p-4 px-8 font-semibold text-gray-800 uppercase bg-white"
5655
type="submit"
5756
>
5857
Subscribe

functions/newsletter/newsletter.js

+33-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,41 @@ exports.handler = async (event, context) => {
55
return { statusCode: 400, body: null };
66
}
77

8-
const Hubspot = require("hubspot");
9-
const hubspot = new Hubspot({ apiKey: process.env.HUBSPOT_KEY });
108
const body = JSON.parse(event.body);
11-
const contact = await hubspot.contacts.createOrUpdate(body.form.email);
9+
const contactObj = {
10+
properties: {
11+
email: body.email,
12+
},
13+
};
1214

13-
return { statusCode: 200, body: JSON.stringify({ success: true }) };
15+
const hubspot = require("@hubspot/api-client");
16+
const hubspotClient = new hubspot.Client({
17+
apiKey: process.env.HUBSPOT_KEY,
18+
});
19+
20+
const createResponse = await hubspotClient.crm.contacts.basicApi.create(
21+
contactObj
22+
);
23+
24+
console.log(`Created contact, email: ${body.email}`);
25+
26+
return {
27+
statusCode: 200,
28+
body: "Contact created",
29+
};
1430
} catch (err) {
15-
return { statusCode: 500, body: err.toString() };
31+
const get = require("lodash/get");
32+
const statusCode = get(err, "response.statusCode", false);
33+
34+
if (!statusCode) {
35+
console.log(`Error creating contact: ${err.toString()}`);
36+
return { statusCode: 500, body: err.toString() };
37+
}
38+
39+
const body = get(err, "response.body.message", false);
40+
41+
console.log(`Error creating contact, code: ${statusCode} message: ${body}`);
42+
43+
return { statusCode, body };
1644
}
1745
};

0 commit comments

Comments
 (0)