-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.html
404 lines (338 loc) · 10.6 KB
/
posts.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Posts with Images</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
/>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
nav {
background-color: #333;
color: white;
padding: 10px;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.nav-links {
display: flex;
align-items: center;
}
.nav-links a {
color: white;
text-decoration: none;
margin: 0 10px;
}
#home {
padding: 8px 16px;
text-decoration: none;
cursor: pointer;
position: absolute;
top: 15px;
font-size: 20px;
font-weight: 600;
}
#home:hover {
color: #4caf50;
}
#postButton {
position: absolute;
top: 15px;
right: 310px;
}
#galleryButton {
position: absolute;
top: 15px;
right: 210px;
}
#messageButton {
padding: 8px 16px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
position: absolute;
top: 15px;
right: 100px;
}
#logoutButton {
padding: 8px 16px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
position: absolute;
top: 15px;
right: 10px;
}
form {
max-width: 500px;
margin: auto;
margin-top: 50px;
}
label {
display: block;
margin-top: 10px;
}
input,
textarea {
width: 100%;
padding: 8px;
margin-top: 6px;
margin-bottom: 16px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.posts {
margin-top: 30px;
}
.post {
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
position: relative; /* Added position relative */
}
.buttons-container {
position: absolute;
top: 0;
right: 0;
margin: 10px;
}
.post img {
max-width: 100%;
height: auto;
margin-top: 10px;
}
.post img.medium-image {
max-width: 50vw;
height: 150px;
margin-top: 10px;
margin-right: 10px;
}
.buttons-container button {
background-color: #4caf50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-right: 10px; /* Add a margin-right for the gap */
}
.buttons-container button:last-child {
margin-right: 0; /* Remove margin-right for the last button to avoid unnecessary space */
}
buttons-container button,
.delete-image-button {
display: none;
}
.image-container {
position: relative;
display: inline-block;
}
.delete-image-button {
position: absolute;
top: 5px;
right: 5px;
background-color: #e74c3c;
}
.image-container:hover .delete-image-button {
display: block;
}
h2 {
margin-top: 45px;
}
</style>
</head>
<body>
<!-- Logout button on the right side -->
<a id="home" href="/dashboard.html">VWF</a>
<div class="nav-links">
<a href="/admingallery.html"
><button id="galleryButton">Gallery</button></a
>
<a href="/posts.html"><button id="postButton">Post</button></a>
<button id="logoutButton">Logout</button>
<a href="/message.html"><button id="messageButton">Message</button></a>
</div>
<form id="postForm" enctype="multipart/form-data">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required />
<label for="content">Content:</label>
<textarea id="content" name="content" rows="4" required></textarea>
<label for="images">Images:</label>
<input type="file" id="images" name="images" accept="image/*" multiple />
<button type="button" onclick="addPost()">Add Post</button>
</form>
<h2>Posts</h2>
<div class="posts" id="postsContainer">
<!-- Posts will be displayed here -->
</div>
<script src="./config.js"></script>
<script>
const backendUrl = window.config.backendUrl;
const frontendUrl = window.config.frontendUrl;
async function addPost() {
const form = document.getElementById('postForm');
const formData = new FormData(form);
try {
const response = await fetch(`${backendUrl}/api/v1/posts`, {
method: 'POST',
body: formData,
});
if (response.ok) {
console.log('Post added successfully');
alert('Post added successfully');
// Reset the form
form.reset();
// Reload posts after adding a new one
loadPosts();
} else {
console.error('Failed to add post');
}
} catch (error) {
console.error('Error:', error.message);
}
}
async function loadPosts() {
const postsContainer = document.getElementById('postsContainer');
try {
const response = await fetch(`${backendUrl}/api/v1/posts`);
if (response.ok) {
const posts = await response.json();
renderPosts(posts);
} else {
console.error('Failed to fetch posts');
}
} catch (error) {
console.error('Error:', error.message);
}
}
function renderPosts(posts) {
const postsContainer = document.getElementById('postsContainer');
postsContainer.innerHTML = '';
posts.forEach((post) => {
const postElement = document.createElement('div');
postElement.classList.add('post');
const buttonsContainer = document.createElement('div');
buttonsContainer.classList.add('buttons-container');
const editButton = document.createElement('button');
editButton.innerHTML = '<i class="fas fa-edit"></i>';
editButton.addEventListener('click', () => editPost(post._id));
const deleteButton = document.createElement('button');
deleteButton.innerHTML = '<i class="fas fa-trash-alt"></i>';
deleteButton.addEventListener('click', () => deletePost(post._id));
buttonsContainer.appendChild(editButton);
buttonsContainer.appendChild(deleteButton);
postElement.appendChild(buttonsContainer);
const titleElement = document.createElement('h3');
titleElement.textContent = post.title;
const contentElement = document.createElement('p');
contentElement.textContent = post.content;
postElement.appendChild(titleElement);
postElement.appendChild(contentElement);
// Assuming images property is an array of image URLs
if (post.images && post.images.length > 0) {
post.images.forEach((image, index) => {
const imageContainer = document.createElement('div');
imageContainer.classList.add('image-container');
const imgElement = document.createElement('img');
imgElement.src =
'https://res.cloudinary.com/drtm8uqcp/image/upload/' +
image.img_name;
imgElement.classList.add('medium-image');
const deleteImageButton = document.createElement('button');
deleteImageButton.innerHTML = '<i class="fas fa-trash-alt"></i>';
deleteImageButton.classList.add('delete-image-button');
deleteImageButton.addEventListener('click', () =>
deleteImage(post._id, image.asset_id)
);
imageContainer.appendChild(imgElement);
imageContainer.appendChild(deleteImageButton);
postElement.appendChild(imageContainer);
});
}
postsContainer.appendChild(postElement);
});
}
async function deletePost(postId) {
try {
const response = await fetch(`${backendUrl}/api/v1/posts/${postId}`, {
method: 'DELETE',
});
if (response.ok) {
console.log('Post deleted successfully');
alert('Post deleted successfully');
loadPosts();
} else {
console.error('Failed to delete post');
}
} catch (error) {
console.error('Error:', error.message);
}
}
async function deleteImage(postId, imageId) {
try {
const response = await fetch(
`${backendUrl}/api/v1/posts/${postId}/images/${imageId}`,
{
method: 'DELETE',
}
);
if (response.ok) {
console.log('Image deleted successfully');
alert('Image deleted successfully');
// Reload posts after deleting an image
loadPosts();
} else {
console.error('Failed to delete image');
}
} catch (error) {
console.error('Error:', error.message);
}
}
function editPost(postId) {
// Redirect to the edit page with the post ID
window.location.href = `editpost.html?q=${postId}`;
}
async function logout() {
try {
const response = await fetch(`${backendUrl}/logout`, {
method: 'GET',
credentials: 'include',
});
if (response.ok) {
// Redirect to the login page after successful logout
localStorage.setItem('token', '');
window.location.href = `${frontendUrl}/admin.html`;
} else {
console.error('Error logging out:', response.statusText);
}
} catch (error) {
console.error('Error logging out:', error);
}
}
// Add click event listener to the logout button
const logoutButton = document.getElementById('logoutButton');
logoutButton.addEventListener('click', logout);
// Load posts on page load
loadPosts();
</script>
</body>
</html>