File size: 10,850 Bytes
1e7308f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect, useCallback } from 'react';
import { API, APIHandler, Validators } from './api.js';

const AlbumManager = () => {
    const [albums, setAlbums] = useState([]);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);
    const [selectedAlbums, setSelectedAlbums] = useState(new Set());
    const [editingAlbum, setEditingAlbum] = useState(null);

    // Daten laden
    const fetchAlbums = useCallback(async () => {
        try {
            setLoading(true);
            const data = await APIHandler.get(API.albums.list);
            setAlbums(data);
            setError(null);
        } catch (err) {
            setError('Fehler beim Laden der Alben: ' + err.message);
            console.error('Fetch error:', err);
        } finally {
            setLoading(false);
        }
    }, []);

    useEffect(() => {
        fetchAlbums();
    }, [fetchAlbums]);


    // Album erstellen
    const handleCreate = async (name, description = '') => {
        try {
            //const validationErrors = Validators.album({ name });
            //if (Object.keys(validationErrors).length > 0) {
            //    throw new Error(Object.values(validationErrors).join(', '));
            //}

            await APIHandler.post(API.albums.create, { name, description });
            await fetchAlbums();
            window.showToast('Erfolg', 'Album wurde erstellt', 'success');
        } catch (err) {
            window.showToast('Fehler', err.message, 'danger');
        }
    };

    // Album aktualisieren
    const handleUpdate = async (id, updates) => {
        try {
            //const validationErrors = Validators.album(updates);
            //if (Object.keys(validationErrors).length > 0) {
            //    throw new Error(Object.values(validationErrors).join(', '));
            //}

            await APIHandler.put(API.albums.update(id), updates);
            await fetchAlbums();
            setEditingAlbum(null);
            window.showToast('Erfolg', 'Album wurde aktualisiert', 'success');
        } catch (err) {
            window.showToast('Fehler', err.message, 'danger');
        }
    };


    // Album aktualisieren
    const handleUpdate = async (id, updates) => {
        try {
            const validationErrors = Validators.album(updates);
            if (Object.keys(validationErrors).length > 0) {
                throw new Error(Object.values(validationErrors).join(', '));
            }

            await APIHandler.put(API.albums.update(id), updates);
            await fetchAlbums();
            setEditingAlbum(null);
            window.showToast('Erfolg', 'Album wurde aktualisiert', 'success');
        } catch (err) {
            window.showToast('Fehler', err.message, 'danger');
        }
    };

    // Massenbearbeitung
    const handleBulkDelete = async () => {
        if (selectedAlbums.size === 0) return;
        if (!window.confirm(`Möchten Sie ${selectedAlbums.size} Alben wirklich löschen?`)) return;

        try {
            await Promise.all(
                Array.from(selectedAlbums).map(id => 
                    APIHandler.delete(API.albums.delete(id))
                )
            );
            setSelectedAlbums(new Set());
            await fetchAlbums();
            window.showToast('Erfolg', 'Ausgewählte Alben wurden gelöscht', 'success');
        } catch (err) {
            window.showToast('Fehler', err.message, 'danger');
        }
    };

    const handleSelectAll = (event) => {
        if (event.target.checked) {
            setSelectedAlbums(new Set(albums.map(album => album.id)));
        } else {
            setSelectedAlbums(new Set());
        }
    };

    if (loading) {
        return (
            <div className="d-flex justify-content-center p-5">
                <div className="spinner-border text-primary" role="status">
                    <span className="visually-hidden">Laden...</span>
                </div>
            </div>
        );
    }

    if (error) {
        return (
            <div className="alert alert-danger m-3" role="alert">
                <h4 className="alert-heading">Fehler</h4>
                <p>{error}</p>
                <button 
                    className="btn btn-outline-danger"
                    onClick={fetchAlbums}
                >
                    Erneut versuchen
                </button>
            </div>
        );
    }

    return (
        <div className="card-body">
            {/* Toolbar */}
            <div className="d-flex justify-content-between mb-3">
                <button 
                    className="btn btn-primary"
                    onClick={() => setEditingAlbum({ name: '', description: '' })}
                >
                    <i className="bi bi-plus-lg"></i> Neues Album
                </button>
                {selectedAlbums.size > 0 && (
                    <button 
                        className="btn btn-danger"
                        onClick={handleBulkDelete}
                    >
                        <i className="bi bi-trash"></i> 
                        {selectedAlbums.size} Alben löschen
                    </button>
                )}
            </div>

            {/* Album Liste */}
            <div className="table-responsive">
                <table className="table table-hover">
                    <thead>
                        <tr>
                            <th>
                                <input 
                                    type="checkbox"
                                    className="form-check-input"
                                    onChange={handleSelectAll}
                                    checked={selectedAlbums.size === albums.length}
                                />
                            </th>
                            <th>Name</th>
                            <th>Bilder</th>
                            <th>Erstellt</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        {albums.map(album => (
                            <tr key={album.id}>
                                <td>
                                    <input 
                                        type="checkbox"
                                        className="form-check-input"
                                        checked={selectedAlbums.has(album.id)}
                                        onChange={(e) => {
                                            const newSelected = new Set(selectedAlbums);
                                            if (e.target.checked) {
                                                newSelected.add(album.id);
                                            } else {
                                                newSelected.delete(album.id);
                                            }
                                            setSelectedAlbums(newSelected);
                                        }}
                                    />
                                </td>
                                <td>
                                    {editingAlbum?.id === album.id ? (
                                        <input 
                                            type="text"
                                            className="form-control"
                                            value={editingAlbum.name}
                                            onChange={(e) => setEditingAlbum({
                                                ...editingAlbum,
                                                name: e.target.value
                                            })}
                                        />
                                    ) : album.name}
                                </td>
                                <td>{album.imageCount}</td>
                                <td>{new Date(album.createdAt).toLocaleDateString()}</td>
                                <td>
                                    <div className="btn-group">
                                        {editingAlbum?.id === album.id ? (
                                            <>
                                                <button 
                                                    className="btn btn-sm btn-success"
                                                    onClick={() => handleUpdate(album.id, editingAlbum)}
                                                >
                                                    <i className="bi bi-check"></i>
                                                </button>
                                                <button 
                                                    className="btn btn-sm btn-secondary"
                                                    onClick={() => setEditingAlbum(null)}
                                                >
                                                    <i className="bi bi-x"></i>
                                                </button>
                                            </>
                                        ) : (
                                            <>
                                                <button 
                                                    className="btn btn-sm btn-outline-primary"
                                                    onClick={() => setEditingAlbum(album)}
                                                >
                                                    <i className="bi bi-pencil"></i>
                                                </button>
                                                <button 
                                                    className="btn btn-sm btn-outline-danger"
                                                    onClick={() => handleDelete(album.id)}
                                                >
                                                    <i className="bi bi-trash"></i>
                                                </button>
                                            </>
                                        )}
                                    </div>
                                </td>
                            </tr>
                        ))}
                    </tbody>
                </table>
            </div>

            {albums.length === 0 && (
                <div className="text-center text-muted p-5">
                    <i className="bi bi-folder-x display-4"></i>
                    <p className="mt-3">Keine Alben vorhanden</p>
                </div>
            )}
        </div>
    );
};

export default AlbumManager;
window.AlbumManager = AlbumManager; // <-- Diese Zeile ans Ende setzen