From 33626c7e45f98346ace5dbe232c65198e41d0802 Mon Sep 17 00:00:00 2001 From: Meik Date: Mon, 10 Nov 2025 13:49:49 +0100 Subject: [PATCH] refactoring --- src/App.js | 21 ++++ src/PickupConfigEditor.js | 159 ++++++++++++++-------------- src/components/NavigationTabs.js | 1 + src/components/PickupConfigTable.js | 11 +- src/hooks/usePickupConfig.js | 139 ------------------------ src/hooks/usePickupConfig.test.js | 84 --------------- 6 files changed, 106 insertions(+), 309 deletions(-) delete mode 100644 src/hooks/usePickupConfig.js delete mode 100644 src/hooks/usePickupConfig.test.js diff --git a/src/App.js b/src/App.js index ba638e5..bc63650 100644 --- a/src/App.js +++ b/src/App.js @@ -21,6 +21,7 @@ import DirtyNavigationDialog from './components/DirtyNavigationDialog'; import ConfirmationDialog from './components/ConfirmationDialog'; import StoreSyncOverlay from './components/StoreSyncOverlay'; import RangePickerModal from './components/RangePickerModal'; +import PickupConfigEditor from './PickupConfigEditor'; function App() { const [credentials, setCredentials] = useState({ email: '', password: '' }); @@ -694,6 +695,25 @@ function App() { ); + const legacyEditorContent = session?.isAdmin ? ( + fetchConfig()} + onSaveConfig={saveConfig} + /> + ) : ( + + ); + return ( <> @@ -702,6 +722,7 @@ function App() { + } /> diff --git a/src/PickupConfigEditor.js b/src/PickupConfigEditor.js index 94d78f6..9089d3f 100644 --- a/src/PickupConfigEditor.js +++ b/src/PickupConfigEditor.js @@ -1,117 +1,114 @@ -import { useEffect, useState } from 'react'; -import { startOfDay } from 'date-fns'; import PickupConfigTable from './components/PickupConfigTable'; -import RangePickerModal from './components/RangePickerModal'; -import usePickupConfig from './hooks/usePickupConfig'; -import 'react-date-range/dist/styles.css'; -import 'react-date-range/dist/theme/default.css'; -const PickupConfigEditor = () => { - const { - config, - loading, - status, - error, - fetchConfig, - saveConfig, - toggleActive, - toggleProfileCheck, - toggleOnlyNotify, - changeWeekday, - updateDateRange - } = usePickupConfig(); - const [activeRangePicker, setActiveRangePicker] = useState(null); - const minSelectableDate = startOfDay(new Date()); +const PickupConfigEditor = ({ + config, + loading, + status, + error, + weekdays, + onToggleActive, + onToggleProfileCheck, + onToggleOnlyNotify, + onWeekdayChange, + onRangePickerRequest, + onResetConfig, + onSaveConfig +}) => { + const resolvedWeekdays = + weekdays || ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']; - const handleWeekdayChange = (index, value, entryId) => { - changeWeekday(index, value); - if (value && entryId) { - setActiveRangePicker((prev) => (prev === entryId ? null : prev)); + const resolveEntryId = (entryId, index) => { + if (entryId) { + return entryId; + } + if (typeof index === 'number' && config[index]?.id) { + return config[index].id; + } + return null; + }; + + const handleToggleActive = (entryId, index) => { + const resolvedId = resolveEntryId(entryId, index); + if (resolvedId && onToggleActive) { + onToggleActive(resolvedId); } }; - useEffect(() => { - if (!activeRangePicker) { - return; + const handleToggleProfileCheck = (entryId, index) => { + const resolvedId = resolveEntryId(entryId, index); + if (resolvedId && onToggleProfileCheck) { + onToggleProfileCheck(resolvedId); } - const entry = config.find((item) => item.id === activeRangePicker); - if (!entry || entry.desiredWeekday) { - setActiveRangePicker(null); + }; + + const handleToggleOnlyNotify = (entryId, index) => { + const resolvedId = resolveEntryId(entryId, index); + if (resolvedId && onToggleOnlyNotify) { + onToggleOnlyNotify(resolvedId); } - }, [activeRangePicker, config]); + }; - const activeRangeEntry = activeRangePicker - ? config.find((item) => item.id === activeRangePicker) || null - : null; + const handleWeekdayChange = (entryId, value, index) => { + const resolvedId = resolveEntryId(entryId, index); + if (resolvedId && onWeekdayChange) { + onWeekdayChange(resolvedId, value); + } + }; - if (loading) { + const handleRangePickerRequest = (entryId, index) => { + const resolvedId = resolveEntryId(entryId, index); + if (resolvedId && onRangePickerRequest) { + onRangePickerRequest(resolvedId); + } + }; + + if (loading && !config.length) { return
Lade Konfiguration...
; } - const weekdays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']; - return (
-

ioBroker Abholung-Konfiguration

- +

Tabellarischer Editor

+ {error && ( -
- {error} -
+
{error}
)} - + {status && ( -
- {status} -
+
{status}
)} - + - -
- -
- +

Aktuelle JSON-Konfiguration:

-
-          {JSON.stringify(config, null, 2)}
-        
+
{JSON.stringify(config, null, 2)}
- - {activeRangeEntry && ( - - updateDateRange(activeRangeEntry.id, startDate, endDate) - } - onResetRange={() => { - updateDateRange(activeRangeEntry.id, null, null); - setActiveRangePicker(null); - }} - onClose={() => setActiveRangePicker(null)} - /> - )}
); }; diff --git a/src/components/NavigationTabs.js b/src/components/NavigationTabs.js index e603e58..19e908e 100644 --- a/src/components/NavigationTabs.js +++ b/src/components/NavigationTabs.js @@ -10,6 +10,7 @@ const NavigationTabs = ({ isAdmin, onProtectedNavigate }) => { const tabs = [ { to: '/', label: 'Konfiguration' }, + { to: '/legacy', label: 'Tabelleneditor' }, { to: '/admin', label: 'Admin' } ]; diff --git a/src/components/PickupConfigTable.js b/src/components/PickupConfigTable.js index aa9998c..cfd3ebe 100644 --- a/src/components/PickupConfigTable.js +++ b/src/components/PickupConfigTable.js @@ -24,6 +24,7 @@ const PickupConfigTable = ({ {config.map((item, index) => { + const itemId = item?.id; const normalizedRange = item.desiredDateRange ? { ...item.desiredDateRange } : item.desiredDate @@ -39,7 +40,7 @@ const PickupConfigTable = ({ onToggleActive(index)} + onChange={() => onToggleActive(itemId, index)} className="h-5 w-5" /> @@ -50,7 +51,7 @@ const PickupConfigTable = ({ onToggleProfileCheck(index)} + onChange={() => onToggleProfileCheck(itemId, index)} className="h-5 w-5" /> @@ -58,14 +59,14 @@ const PickupConfigTable = ({ onToggleOnlyNotify(index)} + onChange={() => onToggleOnlyNotify(itemId, index)} className="h-5 w-5" />