-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.js
47 lines (41 loc) · 1.59 KB
/
serverless.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
var API_ENDPOINT = 'https://bkiqf73mel.execute-api.us-east-1.amazonaws.com/prod/dailyAffirmations';
var errorDiv = document.getElementById('error-message')
var successDiv = document.getElementById('success-message')
var resultsDiv = document.getElementById('results-message')
// function output returns input button contents
function waitSecondsValue() { return document.getElementById('waitSeconds').value }
function messageValue() { return document.getElementById('message').value }
function emailValue() { return document.getElementById('email').value }
function clearNotifications() {
errorDiv.textContent = '';
resultsDiv.textContent = '';
successDiv.textContent = '';
}
// When buttons are clicked, this is run passing values to API Gateway call
document.getElementById('emailButton').addEventListener('click', function(e) { sendData(e, 'email'); });
function sendData (e, pref) {
e.preventDefault()
clearNotifications()
fetch(API_ENDPOINT, {
headers:{
"Content-type": "application/json"
},
method: 'POST',
body: JSON.stringify({
waitSeconds: waitSecondsValue(),
message: messageValue(),
email: emailValue()
}),
mode: 'cors'
})
.then((resp) => resp.json())
.then(function(data) {
console.log(data)
successDiv.textContent = 'Submitted. But check the status below!';
resultsDiv.textContent = JSON.stringify(data);
})
.catch(function(err) {
errorDiv.textContent = 'Oops! There is an Error:\n' + err.toString();
console.log(err)
});
};