-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
292 lines (249 loc) · 10.2 KB
/
scripts.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
$(document).ready(function() {
/*
* GET or READ button call
* gets all the tweets in the favs.json file and outputs them
* in a table format.
*/
$("#get-all-button").on('click', function(event) {
// function default and message when button is pressed
event.preventDefault();
console.log('get button was pressed');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a default 'GET' method request
url: '/getTweets',
contentType: 'application/json',
// When the request is successful we enter into the output function
success: function(response) {
console.log(response);
// get the thead and tbody format from the html file to update it
let theadEl = $("#myTable > thead");
let tbodyEl = $("#myTable > tbody");
// empty the thead and tbody if not empty
theadEl.html('');
tbodyEl.html('');
// create the thead format with the titles of the output data
theadEl.append('\
<tr>\
<th> ID </th>\
<th> Tweet </th>\
<th> Created </th>\
</tr>\
');
// loop through elements in the favs.json file to output all the
// tweets, ids and time created in the tbody format
response.forEach(function (std){
tbodyEl.append('\
<tr>\
<td class="id">'+ std.id + '</td>\
<td class="text">'+ std.text + '</td>\
<td class="created_at">' + std.created_at + '</td>\
</tr>\
');
});
}
});
});
/*
* GET User button call
* gets all the users IDs in the favs.json file and outputs them
* in a table format.
*/
$("#get-user-button").on('click', function(event) {
// function default and message when button is pressed
event.preventDefault();
console.log('get user was pressed');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a default 'GET' method request
url: '/getTweets',
contentType: 'application/json',
// When the request is successful we enter into the output function
success: function(response) {
console.log(response);
// get the thead and tbody format from the html file to update it
let theadEl = $("#myTable > thead");
let tbodyEl = $("#myTable > tbody");
// empty the thead and tbody if not empty
theadEl.html('');
tbodyEl.html('');
// create the thead format with the titles of the output data
theadEl.append('\
<tr>\
<th> ID </th>\
<th> Name </th>\
<th> ScreenName </th>\
</tr>\
');
// loop through elements in the favs.json file to output all the
// ids, names and screen name in the tbody format
response.forEach(function (std){
tbodyEl.append('\
<tr>\
<td class="id">'+ std.user.id + '</td>\
<td class="name">'+ std.user.name + '</td>\
<td class="screen_name">'+ std.user.screen_name + '</td>\
</tr>\
');
});
}
});
});
/*
* GET Tweet button call
* given the ID number of a tweet, get the tweet text information
* from favs.json and output it in table format
*/
$("#get-tweet-button").on('submit', function(event) {
// function default and message when button is pressed
event.preventDefault();
console.log('get tweet pressed');
// variable to store the input ID number
var textId = $('#get-text');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a 'POST' method request
url: '/getTweetId',
method: 'POST',
contentType: 'application/json',
// send the data to the express server
data: JSON.stringify({
id: textId.val(),
}),
// When the request is successful we enter into the output function
success: function(response) {
// get the thead and tbody format from the html file to update it
let theadEl = $("#myTable > thead");
let tbodyEl = $("#myTable > tbody");
// empty the thead and tbody if not empty
theadEl.html('');
tbodyEl.html('');
// create the thead format with the titles of the output data
theadEl.append('\
<tr>\
<th> ID </th>\
<th> Text </th>\
<th> Created </th>\
</tr>\
');
// From the favs.json file to output the ID, tweet text
// and time created for the element with the given ID
tbodyEl.append('\
<tr>\
<td class="id">'+ response[0].id + '</td>\
<td class="text">'+ response[0].text + '</td>\
<td class="created_at">'+ response[0].created_at + '</td>\
</tr>\
');
// Empty the textID variable for future use
textId.val('');
}
});
});
/*
* CREATE button call
* take in inputs of an ID and tweet text that are added to the
* favs.json server and output in the table format along with other
* tweets
*/
$("#create-form").on('submit', function (event){
// function default and message when button is pressed
event.preventDefault();
console.log('create button was pressed');
// variable to store the input ID number and input text
var createInputId = $('#create-id-input');
var createInputText = $('#create-text-input');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a 'POST' method request
url: '/createTweets',
method: 'POST',
contentType: 'application/json',
// send the data to the express server
data: JSON.stringify({
id: createInputId.val(),
text: createInputText.val()
}),
// When the request is successful we enter into the output function
success: function (response) {
// empty the variables and click the get all tweets button
// to see if the entries have been created.
createInputId.val('');
createInputText.val('');
$('#get-all-button').click();
}
});
});
/*
* UPDATE button call
* take in inputs of a name and screen names, with the name search for
* entry in favs.json file and update the screen name for that given element
* and print all IDs and names in table format
*/
$('#update-button').on('submit', function (){
// function default and message when button is pressed
event.preventDefault();
console.log('update button was pressed');
// variable to store the input name and screen name to be
// updated
var name = $('#update-name');
var updateScreen = $('#update-screen-name');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a 'PUT' method request
url: '/updateScreenName',
method: 'PUT',
contentType: 'application/json',
// send the data to the express server
data: JSON.stringify({
name: name.val(),
screen_name: updateScreen.val()
}),
dataType: 'json',
// When the request is successful we enter into the output function
success: function (response){
console.log(response);
// click the get users button and check if the screen name has
// been updated
$('#get-user-button').click();
}
});
});
/*
* DELETE button call
* take in input of an ID, with the ID search for entry in favs.json file
* and delete the entry for that given element and print all remaining texts
*/
$('#delete-button').on('submit', function (){
// function default and message when button is pressed
event.preventDefault();
console.log('delete button was pressed');
// variable to store the input ID number
var id = $('#delete-text');
// ajax request for the button call
$.ajax({
// the url used by the server to complete this request with
// a 'DELETE' method request
url: '/deleteTweets',
method: 'DELETE',
contentType: 'application/json',
// send the data to the express server
data: JSON.stringify({
id: id.val()
}),
dataType: 'json',
// When the request is successful we enter into the output function
success: function (response){
console.log(response);
// click the get all tweets button and check if the entry has
// been deleted from the server
$('#get-all-button').click();
}
});
});
});