-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
104 lines (88 loc) · 3.01 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js"></script>
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script src="airtable.browser.js"></script>
</head>
<body>
<div id="error_msg"></div>
<h1>Messages</h1>
<div id="messages"></div>
<hr/>
<div id="create_msg_section">
<h2>Send new message</h2>
<div>To <input id="newmsg_to"></div>
<div>Msg <input id="newmsg_msg"></div>
<button id='create'>Create</button>
<div id="created"></div>
</div>
</body>
<script>
var Airtable = require('airtable');
// Get a base ID for an instance of art gallery example
var base = new Airtable({ apiKey: 'keyDUEhF6Gtx76Myx' }).base('appPgm3ltdbRypFvH');
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var deleteMessage = function(record) {
record.destroy(function(err) {
if (err) {
console.log('Error destroying ', recordId, err);
} else {
console.log('Destroyed ', record.getId());
$('div[data-record-id="'+record.getId()+'"]').remove();
}
});
};
var loadMessages = function() {
$('#messages').empty();
base('Messages').select({
sort: [
{field: 'From', direction: 'asc'}
]
}).eachPage(function page(records, fetchNextPage) {
records.forEach(function(record) {
console.log('Retrieved ', record.get('From'));
var $msgInfo = $('<div>');
$msgInfo.append($('<h3>').text('From: ' + record.get('From')));
$msgInfo.append($('<div>').text('Date: ' + record.get('Sent')));
$msgInfo.append($('<div>').text('To: ' + record.get('To')));
$msgInfo.append($('<div>').text(record.get('Msg')));
var x = $('<button>').text('Delete').click(function() {
deleteMessage(record);
});
if (record.get('To') == getParameterByName("me") || record.get('From') == getParameterByName("me"))
{
$msgInfo.append(x)
$msgInfo.attr('data-record-id', record.getId());
$('#messages').append($msgInfo);
}
});
fetchNextPage();
}, function done(error) {
console.log(error);
});
};
$('#create').click(function(){
base('Messages').create({
"From": getParameterByName("me"),
"To": $('#newmsg_to').val(),
"Msg": $('#newmsg_msg').val()
}, function(err, record) {
if (err) { console.log(err); return; }
loadMessages();
});
});
if (!getParameterByName("me"))
{
$('#error_msg').text("Please login by putting your user name in the URL like ?me=Mia");
$('#create_msg_section').hide();
}
else
{
loadMessages();
}
</script>
</html>