-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia.js
More file actions
391 lines (345 loc) · 14.3 KB
/
media.js
File metadata and controls
391 lines (345 loc) · 14.3 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
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
// Media management functions
// Load user's media
async function loadMedia(type, userId) {
if (!authToken) return;
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[type.toUpperCase()]}/records?filter=(user="${userId}")`, {
headers: {
'Authorization': `Bearer ${authToken}`
}
});
const data = await response.json();
if (response.ok) {
currentMediaData = data.items || [];
applyFilters();
} else {
throw new Error(data.message || `Failed to load ${type}`);
}
} catch (error) {
document.getElementById('mediaList').innerHTML =
`<div class="error">Error loading ${type}: ${error.message}</div>`;
}
}
// Update user visibility
async function updateVisibility(visible) {
if (!currentUser || !authToken) {
showMessage('Please login first', 'error');
return;
}
const formData = new FormData();
formData.append('visible', visible);
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/users/records/${currentUser.id}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${authToken}`
},
body: formData
});
if (response.ok) {
currentUser.visible = visible;
showMessage(`Visibility ${visible ? 'enabled' : 'disabled'} successfully!`);
} else {
const data = await response.json();
throw new Error(data.message || 'Failed to update visibility');
}
} catch (error) {
showMessage(`Error updating visibility: ${error.message}`, 'error');
document.getElementById('visibilityToggle').checked = currentUser.visible || false;
}
}
// Add a new media item
async function addMedia() {
if (!currentUser || !authToken) {
showMessage('Please login first', 'error');
return;
}
if (viewedUser) {
showMessage('Cannot add media to another user\'s list', 'error');
return;
}
const title = document.getElementById('title').value.trim();
const image = document.getElementById('image').files[0];
const status = document.getElementById('status').value;
const rating = document.getElementById('rating').value ? parseInt(document.getElementById('rating').value) : null;
const startdate = document.getElementById('startdate').value;
const enddate = document.getElementById('enddate').value;
const currentep = document.getElementById('currentep').value ? parseInt(document.getElementById('currentep').value) : null;
const totalep = document.getElementById('totalep').value ? parseInt(document.getElementById('totalep').value) : null;
const comment = document.getElementById('comment').value;
if (!title || !status) {
showMessage('Please provide a title and status', 'error');
return;
}
if (rating !== null && (isNaN(rating) || rating < 1 || rating > 5)) {
showMessage('Rating must be between 1 and 5', 'error');
return;
}
if (['animes', 'shows'].includes(currentMediaType)) {
if (currentep !== null && (isNaN(currentep) || currentep < 0)) {
showMessage('Current episode must be 0 or greater', 'error');
return;
}
if (totalep !== null && (isNaN(totalep) || totalep < 0)) {
showMessage('Total episodes must be 0 or greater', 'error');
return;
}
if (currentep !== null && totalep !== null && currentep > totalep) {
showMessage('Current episode cannot exceed total episodes', 'error');
return;
}
}
const formData = new FormData();
formData.append('title', title);
formData.append('status', status);
formData.append('user', currentUser.id);
if (rating !== null) formData.append('rating', rating);
if (image) formData.append('image', image);
if (startdate) formData.append('startdate', startdate);
if (enddate) formData.append('enddate', enddate);
if (['animes', 'shows'].includes(currentMediaType) && currentep !== null) formData.append('currentep', currentep);
if (['animes', 'shows'].includes(currentMediaType) && totalep !== null) formData.append('totalep', totalep);
if (comment) formData.append('comment', comment);
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[currentMediaType.toUpperCase()]}/records`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${authToken}`
},
body: formData
});
const data = await response.json();
if (response.ok) {
showMessage(`${currentMediaType.slice(0, -1)} added successfully!`);
document.getElementById('addForm').reset();
resetStarRating('add-rating');
hideAddMediaPopup();
loadMedia(currentMediaType, currentUser.id);
} else {
throw new Error(data.message || `Failed to add ${currentMediaType.slice(0, -1)}`);
}
} catch (error) {
showMessage(`Error adding ${currentMediaType.slice(0, -1)}: ${error.message}`, 'error');
}
}
// Edit a media item
function editMedia(id, type) {
if (viewedUser) {
showMessage('Cannot edit another user\'s list', 'error');
return;
}
const mediaItem = document.querySelector(`[data-id="${id}"]`);
const info = mediaItem.querySelector('.media-info');
const actions = mediaItem.querySelector('.media-actions');
const editForm = mediaItem.querySelector('.edit-form');
info.style.display = 'none';
actions.style.display = 'none';
editForm.classList.add('active');
}
// Cancel edit
function cancelEdit(id) {
const mediaItem = document.querySelector(`[data-id="${id}"]`);
const info = mediaItem.querySelector('.media-info');
const actions = mediaItem.querySelector('.media-actions');
const editForm = mediaItem.querySelector('.edit-form');
info.style.display = 'block';
actions.style.display = 'flex';
editForm.classList.remove('active');
}
// Save media changes
async function saveMedia(id, type, imageOnly = false) {
if (!authToken) {
showMessage('Please login first', 'error');
return;
}
if (viewedUser) {
showMessage('Cannot edit another user\'s list', 'error');
return;
}
const formData = new FormData();
if (imageOnly) {
const image = document.getElementById(`edit-image-${id}`).files[0];
if (!image) {
showMessage('Please select an image', 'error');
cancelEdit(id);
return;
}
formData.append('image', image);
} else {
const title = document.getElementById(`edit-title-${id}`).value.trim();
const image = document.getElementById(`edit-image-${id}`).files[0];
const status = document.getElementById(`edit-status-${id}`).value;
const rating = document.getElementById(`edit-rating-value-${id}`).value ? parseInt(document.getElementById(`edit-rating-value-${id}`).value) : null;
const startdate = document.getElementById(`edit-startdate-${id}`)?.value;
const enddate = document.getElementById(`edit-enddate-${id}`).value;
const currentep = document.getElementById(`edit-currentep-${id}`)?.value ? parseInt(document.getElementById(`edit-currentep-${id}`).value) : null;
const totalep = document.getElementById(`edit-totalep-${id}`)?.value ? parseInt(document.getElementById(`edit-totalep-${id}`).value) : null;
const comment = document.getElementById(`edit-comment-${id}`).value;
if (!title || !status) {
showMessage('Please provide a title and status', 'error');
return;
}
if (rating !== null && (isNaN(rating) || rating < 1 || rating > 5)) {
showMessage('Rating must be between 1 and 5', 'error');
return;
}
if (['animes', 'shows'].includes(type)) {
if (currentep !== null && (isNaN(currentep) || currentep < 0)) {
showMessage('Current episode must be 0 or greater', 'error');
return;
}
if (totalep !== null && (isNaN(totalep) || totalep < 0)) {
showMessage('Total episodes must be 0 or greater', 'error');
return;
}
if (currentep !== null && totalep !== null && currentep > totalep) {
showMessage('Current episode cannot exceed total episodes', 'error');
return;
}
}
formData.append('title', title);
formData.append('status', status);
if (rating !== null) formData.append('rating', rating);
if (image) formData.append('image', image);
if (type !== 'movies' && startdate) formData.append('startdate', startdate);
if (enddate) formData.append('enddate', enddate);
if (['animes', 'shows'].includes(type) && currentep !== null) formData.append('currentep', currentep);
if (['animes', 'shows'].includes(type) && totalep !== null) formData.append('totalep', totalep);
if (comment) formData.append('comment', comment);
}
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[type.toUpperCase()]}/records/${id}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${authToken}`
},
body: formData
});
const data = await response.json();
if (response.ok) {
showMessage(imageOnly ? 'Image updated successfully!' : `${type.slice(0, -1)} updated successfully!`);
cancelEdit(id); // Close edit form
loadMedia(type, currentUser.id);
} else {
throw new Error(data.message || `Failed to update ${imageOnly ? 'image' : type.slice(0, -1)}`);
}
} catch (error) {
showMessage(`Error updating ${imageOnly ? 'image' : type.slice(0, -1)}: ${error.message}`, 'error');
cancelEdit(id);
}
}
// Update rating
async function updateRating(id, type, rating) {
if (!authToken) {
showMessage('Please login first', 'error');
return;
}
if (viewedUser) {
showMessage('Cannot edit another user\'s list', 'error');
return;
}
if (rating !== null && (isNaN(rating) || rating < 1 || rating > 5)) {
showMessage('Rating must be between 1 and 5', 'error');
return;
}
const formData = new FormData();
if (rating === null) {
formData.append('rating', ''); // Clear the rating
} else {
formData.append('rating', rating);
}
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[type.toUpperCase()]}/records/${id}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${authToken}`
},
body: formData
});
if (response.ok) {
showMessage(rating === null ? 'Rating cleared!' : 'Rating updated successfully!');
document.getElementById(`display-rating-value-${id}`).value = rating || '';
const stars = document.getElementById(`display-rating-${id}`).querySelectorAll('.star');
stars.forEach(star => {
star.classList.toggle('selected', rating !== null && parseInt(star.dataset.value) <= rating);
});
loadMedia(type, currentUser.id);
} else {
const data = await response.json();
throw new Error(data.message || 'Failed to update rating');
}
} catch (error) {
showMessage(`Error updating rating: ${error.message}`, 'error');
}
}
// Update episode count
async function updateEpisode(id, type, delta) {
if (!authToken) {
showMessage('Please login first', 'error');
return;
}
if (viewedUser) {
showMessage('Cannot edit another user\'s list', 'error');
return;
}
const currentepInput = document.getElementById(`edit-currentep-${id}`);
const totalepInput = document.getElementById(`edit-totalep-${id}`);
const currentep = currentepInput ? parseInt(currentepInput.value) || 0 : 0;
const totalep = totalepInput ? parseInt(totalepInput.value) || null : null;
const newCurrentep = Math.max(0, currentep + delta);
if (totalep !== null && newCurrentep > totalep) {
showMessage('Current episode cannot exceed total episodes', 'error');
return;
}
const formData = new FormData();
formData.append('currentep', newCurrentep);
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[type.toUpperCase()]}/records/${id}`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${authToken}`
},
body: formData
});
if (response.ok) {
showMessage(`Episode count updated!`);
loadMedia(type, currentUser.id);
} else {
const data = await response.json();
throw new Error(data.message || `Failed to update episode count`);
}
} catch (error) {
showMessage(`Error updating episode count: ${error.message}`, 'error');
}
}
// Delete a media item
async function deleteMedia(id, type) {
if (!authToken) {
showMessage('Please login first', 'error');
return;
}
if (viewedUser) {
showMessage('Cannot delete from another user\'s list', 'error');
return;
}
if (!confirm(`Are you sure you want to delete this ${type.slice(0, -1)}?`)) {
return;
}
try {
const response = await fetch(`${CONFIG.API_BASE}/api/collections/${CONFIG.COLLECTIONS[type.toUpperCase()]}/records/${id}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${authToken}`
}
});
if (response.ok) {
showMessage(`${type.slice(0, -1)} deleted successfully!`);
loadMedia(type, currentUser.id);
} else {
const data = await response.json();
throw new Error(data.message || `Failed to delete ${type.slice(0, -1)}`);
}
} catch (error) {
showMessage(`Error deleting ${type.slice(0, -1)}: ${error.message}`, 'error');
}
}