-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmitFormToSalesJet.js
42 lines (38 loc) · 1.16 KB
/
submitFormToSalesJet.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
function connectWithSalesJet(apiKey, event_name, selector, noAlert) {
const form = document.querySelector(selector);
form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(form);
submitFormToSalesJet({
apiKey,
event_name,
formData,
})
.then((response) => {
console.log({ success: true, error: false, data: response });
})
.catch((err) => {
if (!noAlert && noAlert !== "no-alert")
alert(
"An error has occurred while trying to submit the form. Please try later or contact the system administrator."
);
console.log({ success: false, error: err, data: {} });
});
});
}
async function submitFormToSalesJet({ apiKey, event_name, formData }) {
const requestBody = {};
for (let [name, value] of formData) requestBody[name] = value;
const response = await fetch("https://sj-api.com/externalapp/track", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: apiKey,
},
body: JSON.stringify({
event_name,
contact: requestBody,
}),
});
return response;
}