Upload folder using huggingface_hub
Browse files- client/src/pages/Slides.tsx +13 -49
client/src/pages/Slides.tsx
CHANGED
|
@@ -34,12 +34,9 @@ const Slides: React.FC = () => {
|
|
| 34 |
try { const parsed = u ? JSON.parse(u) : null; setIsAdmin(parsed?.role === 'admin'); } catch {}
|
| 35 |
try {
|
| 36 |
setLoading(true); setError('');
|
| 37 |
-
const
|
| 38 |
-
const resp = await fetch(`${base}/api/slides`, { cache: 'no-cache' });
|
| 39 |
-
if (!resp.ok) throw new Error('Failed to load slides');
|
| 40 |
-
const data = await resp.json();
|
| 41 |
if (aborted) return;
|
| 42 |
-
const list = Array.isArray(data?.slides) ? data.slides : [];
|
| 43 |
setItems(list as any);
|
| 44 |
} catch (e: any) {
|
| 45 |
if (!aborted) setError('Unable to load slides.');
|
|
@@ -57,43 +54,13 @@ const Slides: React.FC = () => {
|
|
| 57 |
|
| 58 |
setDeleting(slideId);
|
| 59 |
try {
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
console.
|
| 66 |
-
|
| 67 |
-
console.log('Token:', token ? 'Present' : 'Missing');
|
| 68 |
-
|
| 69 |
-
const resp = await fetch(url, {
|
| 70 |
-
method: 'DELETE',
|
| 71 |
-
headers: {
|
| 72 |
-
'Authorization': `Bearer ${token}`,
|
| 73 |
-
'user-role': 'admin',
|
| 74 |
-
'user-info': localStorage.getItem('user') || ''
|
| 75 |
-
}
|
| 76 |
-
});
|
| 77 |
-
|
| 78 |
-
console.log('Delete response status:', resp.status);
|
| 79 |
-
|
| 80 |
-
if (resp.ok) {
|
| 81 |
-
// Remove the slide from the local state
|
| 82 |
-
setItems(items.filter(item => (item as any)._id !== slideId));
|
| 83 |
-
} else {
|
| 84 |
-
let errorMessage = 'Unknown error';
|
| 85 |
-
try {
|
| 86 |
-
const errorData = await resp.json();
|
| 87 |
-
errorMessage = errorData.error || errorMessage;
|
| 88 |
-
} catch (e) {
|
| 89 |
-
errorMessage = `Server error: ${resp.status} ${resp.statusText}`;
|
| 90 |
-
}
|
| 91 |
-
console.error('Delete error:', errorMessage);
|
| 92 |
-
window.alert(`Failed to delete slide: ${errorMessage}`);
|
| 93 |
-
}
|
| 94 |
-
} catch (e) {
|
| 95 |
-
console.error('Delete exception:', e);
|
| 96 |
-
window.alert('Failed to delete slide. Please try again.');
|
| 97 |
} finally {
|
| 98 |
setDeleting(null);
|
| 99 |
}
|
|
@@ -174,15 +141,12 @@ const Slides: React.FC = () => {
|
|
| 174 |
<button className="px-3 py-2 rounded-md bg-gray-100" onClick={() => setEditing({ title: '', date: '', file: '', order: 0 })}>New</button>
|
| 175 |
<button className="px-3 py-2 rounded-md bg-indigo-600 text-white" onClick={async () => {
|
| 176 |
if (!editing || !editing.title || !editing.file) return;
|
| 177 |
-
const
|
| 178 |
-
|
| 179 |
-
const resp = await fetch(`${base}/api/slides`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, 'user-role': 'admin', 'user-info': localStorage.getItem('user') || '' }, body: JSON.stringify(editing) });
|
| 180 |
-
const data = await resp.json();
|
| 181 |
-
if (resp.ok) {
|
| 182 |
setEditing(null);
|
| 183 |
// reload list
|
| 184 |
-
const listResp = await
|
| 185 |
-
const listData =
|
| 186 |
setItems(Array.isArray(listData?.slides) ? listData.slides : []);
|
| 187 |
}
|
| 188 |
}}>Save</button>
|
|
|
|
| 34 |
try { const parsed = u ? JSON.parse(u) : null; setIsAdmin(parsed?.role === 'admin'); } catch {}
|
| 35 |
try {
|
| 36 |
setLoading(true); setError('');
|
| 37 |
+
const resp = await api.get('/api/slides', { headers: { 'Cache-Control': 'no-cache' } });
|
|
|
|
|
|
|
|
|
|
| 38 |
if (aborted) return;
|
| 39 |
+
const list = Array.isArray((resp.data as any)?.slides) ? (resp.data as any).slides : [];
|
| 40 |
setItems(list as any);
|
| 41 |
} catch (e: any) {
|
| 42 |
if (!aborted) setError('Unable to load slides.');
|
|
|
|
| 54 |
|
| 55 |
setDeleting(slideId);
|
| 56 |
try {
|
| 57 |
+
await api.delete(`/api/slides/${encodeURIComponent(slideId)}`);
|
| 58 |
+
setItems(items.filter(item => (item as any)._id !== slideId));
|
| 59 |
+
} catch (e: any) {
|
| 60 |
+
const status = e?.response?.status;
|
| 61 |
+
const msg = e?.response?.data?.error || e?.message || 'Unknown error';
|
| 62 |
+
console.error('Delete error:', status, msg);
|
| 63 |
+
window.alert(`Failed to delete slide: ${status ? status + ' ' : ''}${msg}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
} finally {
|
| 65 |
setDeleting(null);
|
| 66 |
}
|
|
|
|
| 141 |
<button className="px-3 py-2 rounded-md bg-gray-100" onClick={() => setEditing({ title: '', date: '', file: '', order: 0 })}>New</button>
|
| 142 |
<button className="px-3 py-2 rounded-md bg-indigo-600 text-white" onClick={async () => {
|
| 143 |
if (!editing || !editing.title || !editing.file) return;
|
| 144 |
+
const resp = await api.post('/api/slides', editing);
|
| 145 |
+
if (resp.status >= 200 && resp.status < 300) {
|
|
|
|
|
|
|
|
|
|
| 146 |
setEditing(null);
|
| 147 |
// reload list
|
| 148 |
+
const listResp = await api.get('/api/slides');
|
| 149 |
+
const listData = listResp.data;
|
| 150 |
setItems(Array.isArray(listData?.slides) ? listData.slides : []);
|
| 151 |
}
|
| 152 |
}}>Save</button>
|