Skip to content

Instantly share code, notes, and snippets.

@Kalimaha
Created December 26, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kalimaha/1c11308f7bd3bb66ecb48d54a0674952 to your computer and use it in GitHub Desktop.
Save Kalimaha/1c11308f7bd3bb66ecb48d54a0674952 to your computer and use it in GitHub Desktop.
index
<!DOCTYPE html>
<html>
<head>
<title>Serverless Guestbook</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript">
</script>
<script type="text/javascript">
const get_url = 'https://tfh1cxol33.execute-api.ap-southeast-2.amazonaws.com/serverlessguestbookdeployment'
const post_url = 'https://tfh1cxol33.execute-api.ap-southeast-2.amazonaws.com/serverlessguestbookdeployment'
const list_messages = (messages) => { for (m of messages) $('#list').append(format(m)) }
const format = (m) => '<li>' + m.message + '<br><small>posted by ' + m.author + ' on ' + m.date + '<\/small><\/li>'
const load_messages = () => $.get({ url: get_url }).then((messages) => list_messages(messages))
const form_is_valid = () => $('#m').val().length > 0 && $('#a').val().length > 0
const post_message = () => {
if (form_is_valid()) {
$('#list').empty()
$.post({
url: post_url,
data: JSON.stringify({ message: $('#m').val(), author: $('#a').val(), date: new Date().toISOString() })
}).then(() => load_messages())
} else alert('Please type a message and the author name!')
}
</script>
<style>
li { margin-bottom: 1rem; }
</style>
</head>
<body onload="load_messages()">
<hr>
<b>New message:</b>
<form>
<input id="m" placeholder="e.g. Hallo, world!"> <b>by</b> <input id="a" placeholder="e.g. John Doe"> <button onclick="post_message()">Post</button>
</form>
<hr>
<ul id="list">
<li>Hallo, world!<br>
<small>posted by Guido Barbaglia on 2016-12-26</small></li>
</ul>
<hr>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment