fix: restrict delete button to admins
This commit is contained in:
78
src/hooks/usePickupConfig.js
Normal file
78
src/hooks/usePickupConfig.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { sortEntriesByLabel } from '../utils/configUtils';
|
||||
|
||||
const API_URL = '/api/iobroker/pickup-config';
|
||||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const STATIC_CONFIG = [
|
||||
{ id: '63448', active: false, checkProfileId: true, onlyNotify: true, label: 'Penny Baden-Oos' },
|
||||
{ id: '44975', active: false, checkProfileId: true, onlyNotify: false, label: 'Aldi Kuppenheim', desiredWeekday: 'Samstag' },
|
||||
{ id: '44972', active: false, checkProfileId: true, onlyNotify: false, label: 'Aldi Biblisweg', desiredWeekday: 'Dienstag' },
|
||||
{
|
||||
id: '44975',
|
||||
active: false,
|
||||
checkProfileId: true,
|
||||
onlyNotify: false,
|
||||
label: 'Aldi Kuppenheim',
|
||||
desiredDateRange: { start: '2025-05-18', end: '2025-05-18' }
|
||||
},
|
||||
{ id: '33875', active: false, checkProfileId: true, onlyNotify: false, label: 'Cap Markt', desiredWeekday: 'Donnerstag' },
|
||||
{ id: '42322', active: false, checkProfileId: false, onlyNotify: false, label: 'Edeka Haueneberstein' },
|
||||
{ id: '51450', active: false, checkProfileId: true, onlyNotify: false, label: 'Hornbach Grünwinkel' }
|
||||
];
|
||||
|
||||
const usePickupConfig = () => {
|
||||
const [config, setConfig] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [status, setStatus] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const fetchConfig = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
|
||||
try {
|
||||
// In einer echten Implementierung würde hier die API aufgerufen:
|
||||
// const response = await fetch(API_URL);
|
||||
// const data = await response.json();
|
||||
await delay(500);
|
||||
setConfig(sortEntriesByLabel(STATIC_CONFIG));
|
||||
} catch (err) {
|
||||
setError('Fehler beim Laden der Konfiguration: ' + err.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const saveConfig = useCallback(async () => {
|
||||
setStatus('Speichere...');
|
||||
setError('');
|
||||
|
||||
try {
|
||||
// API-Aufruf zum Speichern der Konfiguration:
|
||||
// await fetch(API_URL, { method: 'POST', body: JSON.stringify(config) });
|
||||
await delay(1000);
|
||||
setStatus('Konfiguration erfolgreich gespeichert!');
|
||||
setTimeout(() => setStatus(''), 3000);
|
||||
} catch (err) {
|
||||
setError('Fehler beim Speichern: ' + err.message);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchConfig();
|
||||
}, [fetchConfig]);
|
||||
|
||||
return {
|
||||
API_URL,
|
||||
config,
|
||||
setConfig,
|
||||
loading,
|
||||
status,
|
||||
error,
|
||||
fetchConfig,
|
||||
saveConfig
|
||||
};
|
||||
};
|
||||
|
||||
export default usePickupConfig;
|
||||
Reference in New Issue
Block a user