-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpage.html
306 lines (248 loc) · 8.22 KB
/
webpage.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Real-time Guest Reviews</title>
</head>
<body>
<h1>Guest Reviews of LA Veranda Hotel</h1>
<style>
#reviews {
max-height: 400px;
overflow-y: scroll;
border: 1px solid black;}
#review_fi {
max-height: 400px;
overflow-y: scroll;
border: 1px solid black;
}
</style>
<div id="reviews"></div>
<form id="review-form" style="font-family: Arial, sans-serif;">
<label for="_method">Method:</label>
<select id="_method" name="_method">
<option></option>
<option value="POST">POST</option>
<option value="PATCH">PATCH</option>
<option value="PUT">PUT</option>
</select>
<bl>
<label for="i_d"> ID:</label>
<input type="text" id="i_d" name="i_d">
<bl>
<label for="guest-name">Guest Name:</label>
<input type="text" id="guest-name" name="guestName">
<bl>
<label for="guest-country">Guest Country:</label>
<input type="text" id="guest-country" name="guestCountry">
<bl>
<label for="score">Score:</label>
<input type="number" id="score" name="score" step="0.1">
<br>
<label for="title">Title:</label>
<input type="text" id="title" name="title">
<bl>
<label for="positive-review">Positive Review:</label>
<textarea id="positive-review" name="positiveReview"></textarea>
<bl>
<label for="negative-review">Negative Review:</label>
<textarea id="negative-review" name="negativeReview"></textarea>
<br>
<label for="room-type">Room Type:</label>
<input type="text" id="room-type" name="roomType">
<bl>
<label for="number-of-nights">Number of Nights:</label>
<input type="number" id="number-of-nights" name="numberOfNights">
<bl>
<label for="visit-date">Visit Date:</label>
<input type="text" id="visit-date" name="visitDate">
<bl>
<label for="group-type">Group Type:</label>
<input type="text" id="group-type" name="groupType">
<bl>
<button type="submit">Submit Review</button>
</form>
<br><br>
<form id="delete-form" style="max-width: 1000px; font-family: Arial, sans-serif;">
<label for="delete-id">Enter ID to delete:</label>
<input type="text" id="delete-id" name="deleteId">
<button type="submit">Delete Review</button>
</form>
<br><br>
<form id="order-form">
<label for="orderBy">Order by:</label>
<input type="text" id="orderBy" name="orderBy"><br>
<label for="startAt">Start at:</label>
<input type="text" id="startAt" name="startAt"><br>
<label for="endAt">End at:</label>
<input type="text" id="endAt" name="endAt"><br>
<label for="limitToFirst">Limit to first:</label>
<input type="text" id="limitToFirst" name="limitToFirst"><br>
<label for="limitToLast">Limit to last:</label>
<input type="text" id="limitToLast" name="limitToLast"><br>
<label for="equalTo">Equal to:</label>
<input type="text" id="equalTo" name="equalTo"><br>
<input type="submit" value="Order reviews">
</form>
<div id="review_fi"></div>
<script>
const reviewsDiv = document.getElementById('reviews');
const socket = new WebSocket('ws://127.0.0.1:5678/');
socket.addEventListener('open', (event) => {
console.log('WebSocket connection opened.');
});
socket.addEventListener('message', (event) => {
const reviews = JSON.parse(event.data);
let reviewsHTML = '';
reviews.forEach((review) => {
reviewsHTML += `
<pre>${JSON.stringify(review, null, 2)}</pre>
`;
});
reviewsDiv.innerHTML = reviewsHTML;
});
socket.addEventListener('close', (event) => {
console.log('WebSocket connection closed.');
});
//Post
const socket_po = new WebSocket('ws://127.0.0.1:5680/');
const socket_pu = new WebSocket('ws://127.0.0.1:5683/');
const socket_pa = new WebSocket('ws://127.0.0.1:5684/');
const reviewForm = document.getElementById('review-form');
reviewForm.addEventListener('submit', (event) => {
event.preventDefault(); // prevent default form submission behavior
const formData = new FormData(reviewForm);
const title = formData.get('title');
const positiveReview = formData.get('positiveReview');
const negativeReview = formData.get('negativeReview');
const score = formData.get('score');
const guestName = formData.get('guestName');
const guestCountry = formData.get('guestCountry');
const roomType = formData.get('roomType');
const numberOfNights = formData.get('numberOfNights');
const visitDate = formData.get('visitDate');
const groupType = formData.get('groupType');
const i_d = formData.get('i_d');
const method = formData.get('_method');
const data = {};
if (method.trim() !=='') {
data._Method = method;
}
if (i_d.trim() !=='') {
data._id = i_d;
}
if (guestName.trim() !=='') {
data.GuestName = guestName;
}
if (title.trim() !== '') {
data.Title = title;
}
if (positiveReview.trim() !== '') {
data.PositiveReview = positiveReview;
}
if (score.trim() !== ''){
data.Score = parseFloat(score);
}
if (numberOfNights.trim() !== ''){
data.NumberOfNights = parseInt(numberOfNights);
}
if (negativeReview.trim() !== '') {
data.NegativeReview = negativeReview;
}
if (guestCountry.trim() !== '') {
data.GuestCountry = guestCountry;
}
if (roomType.trim() !== '') {
data.RoomType = roomType.trim();
}
if (visitDate.trim() !== '') {
data.VisitDate = visitDate;
}
if (groupType.trim() !== '') {
data.GroupType = groupType;
}
if (method === 'PUT') {
socket_pu.send(JSON.stringify(data));
} else if (method === 'PATCH') {
socket_pa.send(JSON.stringify(data));
} else if (method === 'POST') {
socket_po.send(JSON.stringify(data));
}
location.reload(); // reload the page after adding new data
reviewForm.reset(); // clear the form fields
});
//Delete By ID
const socket_d2 = new WebSocket('ws://127.0.0.1:5681/');
const deleteByID = document.getElementById('delete-form');
deleteByID.addEventListener('submit', (event) => {
event.preventDefault(); // prevent default form submission behavior
const formData = new FormData(deleteByID);
const id = formData.get('deleteId');
console.log(id)
if (id.trim() !== '') {
const confirmation = confirm(`Are you sure you want to delete review with _id=${id}?`);
if (confirmation) {
socket_d2.send(JSON.stringify({ id }));
socket_d2.addEventListener('message', (event) => {
const response = JSON.parse(event.data);
//alert(response.message);
});
}
}
location.reload(); // reload the page after deleting data
deleteByID.reset(); // clear the form field
});
// Filter
const socket_fi = new WebSocket('ws://127.0.0.1:5682/');
const orderForm = document.getElementById('order-form');
const review_fiDiv = document.getElementById('review_fi');
orderForm.addEventListener('submit', (event) => {
event.preventDefault(); // prevent default form submission behavior
const formData = new FormData(orderForm);
const orderBy = formData.get('orderBy');
const startAt = formData.get('startAt');
const endAt = formData.get('endAt');
const limitToFirst = formData.get('limitToFirst');
const limitToLast = formData.get('limitToLast');
const equalTo = formData.get('equalTo');
const orderData = {};
if (orderBy.trim() !== '') {
orderData.Orderby = orderBy;
}
if (startAt.trim() !== '') {
orderData.Startat = startAt;
}
if (endAt.trim() !== '') {
orderData.Endat = endAt;
}
if (limitToFirst.trim() !== '') {
orderData.LimitToFirst = limitToFirst;
}
if (limitToLast.trim() !== '') {
orderData.LimitToLast = limitToLast;
}
if (equalTo.trim() !== '') {
orderData.Equalto = equalTo;
}
socket_fi.send(JSON.stringify(orderData));
orderForm.reset(); // clear the form fields
});
socket_fi.addEventListener('open', (event) => {
console.log('WebSocket connection opened.');
});
socket_fi.addEventListener('message', (event) => {
const review_fi = JSON.parse(event.data);
let review_fiHTML = '';
review_fi.forEach((review) => {
review_fiHTML += `
<pre>${JSON.stringify(review,null,2)}</pre>
`;
});
review_fiDiv.innerHTML = review_fiHTML;
});
socket_fi.addEventListener('close', (event) => {
console.log('WebSocket connection closed.');
});
</script>
</body>
</html>