forked from IRON-M4N/pair-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
293 lines (271 loc) · 10 KB
/
admin.html
File metadata and controls
293 lines (271 loc) · 10 KB
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
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #121212;
color: #eee;
margin: 0;
}
.container {
max-width: 800px;
width: 90%;
padding: 2rem;
margin: 1rem auto; /* Add some margin */
background-color: #1e1e1e;
border-radius: 0.75rem;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
h1 {
font-size: 2rem;
font-weight: 700;
margin-bottom: 1.5rem;
color: #f59e0b;
text-align: center;
}
/* Form Styles */
label {
display: block;
margin-bottom: 0.5rem;
color: #aaa; /* Lighter label color */
}
input[type="text"],
input[type="number"],
textarea {
width: 100%;
padding: 0.75rem;
margin-bottom: 1rem;
background-color: #27272a;
color: #eee;
border: 1px solid #3f3f46;
border-radius: 0.5rem;
font-family: inherit;
font-size: 1rem;
}
textarea::placeholder {
color: #9ca3af;
}
.button {
display: inline-block;
padding: 0.75rem 1.5rem;
background-color: #f59e0b;
color: #121212;
font-weight: 600;
text-decoration: none;
border: none; /* Remove border */
border-radius: 0.5rem;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.button:hover {
background-color: #d97706;
transform: translateY(-2px);
}
/* Message Container */
.message-container {
margin-bottom: 1.5rem; /* Spacing between product sections */
border-bottom: 1px solid #3f3f46; /* Separator */
padding-bottom: 1.5rem;
}
.message-container h3 {
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 0.75rem;
color: #f59e0b; /* Product ID color */
}
.message {
padding: 0.6rem; /* Slightly less padding */
margin-bottom: 0.4rem; /* Less margin */
border-radius: 0.375rem;
word-wrap: break-word;
}
.user-message {
background-color: #22c55e; /* Green */
color: #121212;
text-align: left;
border: 1px solid #16a34a;
}
.admin-message {
background-color: #3b82f6; /* Blue */
color: #fff;
text-align: right;
border: 1px solid #2563eb;
}
/* Reply Form */
.reply-form {
margin-top: 1rem;
}
.reply-text {
width: 100%;
padding: 0.75rem;
background-color: #27272a;
color: #eee;
border: 1px solid #3f3f46;
border-radius: 0.5rem;
resize: vertical;
font-family: inherit;
font-size: 1rem;
margin-bottom: 0.5rem; /* Spacing between textarea and button */
}
.reply-text::placeholder{
color: #9ca3af;
}
.reply-form .button {
display: block; /* Make reply button full-width */
width: 100%;
}
</style>
</head>
<body>
<div class="container animate-container">
<h1>Admin Panel</h1>
<form id="productForm">
<label for="title">Judul Produk:</label>
<input type="text" id="title" name="title" required><br><br>
<label for="price">Harga:</label>
<input type="number" id="price" name="price" required><br><br>
<label for="image">URL Gambar:</label>
<input type="text" id="image" name="image" required><br><br>
<label for="description">Deskripsi:</label>
<textarea id="description" name="description" rows="4"></textarea><br><br>
<button type="submit" class="button">Tambah Produk</button>
</form>
</div>
<div class="container animate-container">
<h1>Messages</h1>
<div id="messagesContainer">
<!-- Messages will be displayed here -->
</div>
</div>
<script>
const productForm = document.getElementById('productForm');
const messagesContainer = document.getElementById('messagesContainer');
// --- GSAP Animations ---
gsap.from(".animate-container", {
opacity: 0,
y: 50,
duration: 1,
ease: "power2.out",
stagger: 0.3 // Stagger effect for multiple containers
});
// --- Add Product ---
productForm.addEventListener('submit', function(event) {
event.preventDefault();
const title = document.getElementById('title').value;
const price = document.getElementById('price').value;
const image = document.getElementById('image').value;
const description = document.getElementById('description').value;
fetch('/addProduct', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ title, price, image, description }),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
alert(data.message); // Use a more subtle notification (e.g., a toast) in a real app
productForm.reset();
})
.catch(error => {
console.error('Error:', error);
alert('Terjadi kesalahan saat menambahkan produk.'); // Error feedback
});
});
// --- Load and Display Messages ---
function loadMessages() {
fetch('/getAllMessages')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(messagesByProduct => {
messagesContainer.innerHTML = ''; // Clear previous messages
for (const productId in messagesByProduct) {
if (messagesByProduct.hasOwnProperty(productId)) {
const productMessages = messagesByProduct[productId];
const productSection = document.createElement('div');
productSection.classList.add('message-container', 'animate-message-section');
productSection.innerHTML = `<h3>Product ID: ${productId}</h3>`;
const replyForm = document.createElement('form');
replyForm.classList.add('reply-form');
replyForm.innerHTML = `
<textarea class="reply-text" placeholder="Reply..."></textarea>
<button type="submit" class="button">Send Reply</button>
`;
replyForm.addEventListener('submit', function(event) {
event.preventDefault();
const replyText = this.querySelector('.reply-text').value;
if (replyText.trim() !== "") { // Prevent empty replies
sendReply(productId, replyText);
this.querySelector('.reply-text').value = '';
}
});
productMessages.forEach(message => {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
messageDiv.classList.add(message.sender === 'admin' ? 'admin-message' : 'user-message');
messageDiv.textContent = `${message.sender}: ${message.text}`;
productSection.appendChild(messageDiv);
});
productSection.appendChild(replyForm);
messagesContainer.appendChild(productSection);
}
}
// --- GSAP animation for message sections (after adding to DOM) ---
gsap.from(".animate-message-section", {
opacity: 0,
y: 30,
duration: 0.6,
ease: "power2.out",
stagger: 0.2, // Stagger the appearance of each section
});
})
.catch(error => {
console.error('Error loading messages:', error);
messagesContainer.innerHTML = `<p class = "text-red-500">Gagal memuat pesan: ${error.message}</p>`;
});
}
// --- Send Reply ---
function sendReply(productId, replyText) {
fetch('/sendMessage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: replyText, sender: 'admin', productId: productId }),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data.message);
loadMessages(); // Reload messages to show the new reply
})
.catch(error => {
console.error('Error sending reply:', error);
alert('Terjadi kesalahan saat mengirim balasan.'); // User feedback
});
}
// Load messages on page load and periodically
loadMessages();
setInterval(loadMessages, 5000); // Refresh every 5 seconds
</script>
</body>
</html>