-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
118 lines (98 loc) · 3.18 KB
/
contact.js
File metadata and controls
118 lines (98 loc) · 3.18 KB
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
window.addEventListener('load', function () {
Array.from(document.getElementsByClassName('contact-me')).map(e => e.addEventListener('click', function () {
const openDate = new Date();
contactForm(openDate);
}));
})
function contactForm(openDate) {
if (!openDate instanceof Date) {
console.error("Input must be a date object.");
return;
}
let form = `
<div class="modal">
<span data-action='close'>ⓧ</span>
<form name="contact">
<label for="name">* Name</label>
<input type="text" name="name" required />
<label for="email">* Email</label>
<input type="email" pattern=".+@.+" name="email" required />
<label for="company">Business Name</label>
<input name="company" />
<label for="message">What can Sam Heuck Web Development do for you?</label>
<textarea name="message" rows=5></textarea>
<input name="hpf" />
<input type="submit" value="Send" />
</form>
</div>
`;
let fragment = document.createRange().createContextualFragment(form);
fragment.querySelector('span[data-action="close"').addEventListener('click', function() {
closeContactForm();
})
fragment.querySelector('form').addEventListener('submit', function(evt) {
evt.preventDefault();
const submissionDate = new Date();
submitContactForm(openDate, submissionDate);
});
document.getElementById('contact-form').replaceChildren(fragment);
}
function closeContactForm() {
let formEl = document.getElementById('contact-form');
formEl.removeChild(formEl.querySelector("div.modal"));
}
function submitContactForm(openDate, submissionDate) {
const data = new FormData(document.forms.contact);
const tts = submissionDate - openDate;
if (tts < 1500) {
return;
}
if ("" !== data.get('hpf')) {
closeContactForm();
return;
}
// { name, email, company, message, hpf, tts }
fetch("https://lco6xjc4wjj5g2ckknlt5xffo40bisys.lambda-url.us-east-1.on.aws/", {
method: "POST",
body: JSON.stringify({
name: data.get('name'),
email: data.get('email'),
company: data.get('company'),
message: data.get('message'),
hpf: data.get('hpf'),
tts: tts,
}),
headers: {
"Content-Type": "application/json",
},
}).then((res) => {
console.log(res);
if (res.ok) {
showSuccess();
} else {
showError();
}
});
}
function showSuccess() {
let feedback = `
<div>
<p class="success">Thanks! We'll be in touch soon.</p>
</div>
`;
const fragment = document.createRange().createContextualFragment(feedback);
let parent = document.getElementById('contact-form').querySelector('div.modal');
parent.removeChild(parent.querySelector("form[name='contact']"));
parent.appendChild(fragment);
}
function showError() {
let feedback = `
<div>
<p class="error">Sorry! There was an error sending your message. Try again tomorrow.</p>
</div>
`;
const fragment = document.createRange().createContextualFragment(feedback);
let parent = document.getElementById('contact-form').querySelector('div.modal');
parent.removeChild(parent.querySelector("form[name='contact']"));
parent.appendChild(fragment);
}