refactoring

This commit is contained in:
2025-11-10 09:39:00 +01:00
parent c747d40d3d
commit 083c79d82e
8 changed files with 231 additions and 76 deletions

View File

@@ -1,5 +1,6 @@
import { useCallback, useEffect, useState } from 'react';
import { sortEntriesByLabel } from '../utils/configUtils';
import { formatDateValue } from '../utils/dateUtils';
const API_URL = '/api/iobroker/pickup-config';
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -59,6 +60,73 @@ const usePickupConfig = () => {
}
}, []);
const updateEntryByIndex = useCallback((index, updater) => {
setConfig((prev) => {
if (!prev[index]) {
return prev;
}
const next = [...prev];
next[index] = updater(next[index]);
return next;
});
}, []);
const updateEntryById = useCallback((entryId, updater) => {
setConfig((prev) =>
prev.map((item) => {
if (item.id !== entryId) {
return item;
}
return updater(item);
})
);
}, []);
const toggleActive = useCallback((index) => {
updateEntryByIndex(index, (entry) => ({ ...entry, active: !entry.active }));
}, [updateEntryByIndex]);
const toggleProfileCheck = useCallback((index) => {
updateEntryByIndex(index, (entry) => ({ ...entry, checkProfileId: !entry.checkProfileId }));
}, [updateEntryByIndex]);
const toggleOnlyNotify = useCallback((index) => {
updateEntryByIndex(index, (entry) => ({ ...entry, onlyNotify: !entry.onlyNotify }));
}, [updateEntryByIndex]);
const changeWeekday = useCallback((index, value) => {
updateEntryByIndex(index, (entry) => {
const next = { ...entry, desiredWeekday: value };
if (next.desiredDateRange) {
delete next.desiredDateRange;
}
return next;
});
}, [updateEntryByIndex]);
const updateDateRange = useCallback((entryId, startDate, endDate) => {
const startValue = formatDateValue(startDate);
const endValue = formatDateValue(endDate);
updateEntryById(entryId, (entry) => {
const next = { ...entry };
if (startValue || endValue) {
next.desiredDateRange = {
start: startValue || endValue,
end: endValue || startValue
};
if (next.desiredWeekday) {
delete next.desiredWeekday;
}
} else if (next.desiredDateRange) {
delete next.desiredDateRange;
}
if (next.desiredDate) {
delete next.desiredDate;
}
return next;
});
}, [updateEntryById]);
useEffect(() => {
fetchConfig();
}, [fetchConfig]);
@@ -66,12 +134,16 @@ const usePickupConfig = () => {
return {
API_URL,
config,
setConfig,
loading,
status,
error,
fetchConfig,
saveConfig
saveConfig,
toggleActive,
toggleProfileCheck,
toggleOnlyNotify,
changeWeekday,
updateDateRange
};
};

View File

@@ -0,0 +1,72 @@
import React from 'react';
import { act, render } from '@testing-library/react';
import usePickupConfig from './usePickupConfig';
describe('usePickupConfig', () => {
const advanceTimers = async (ms = 0) => {
await act(async () => {
jest.advanceTimersByTime(ms);
await Promise.resolve();
});
};
const renderHook = () => {
let latest;
const TestHarness = () => {
latest = usePickupConfig();
return null;
};
render(<TestHarness />);
return () => latest;
};
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
test('fetchConfig loads sorted configuration', async () => {
const getHook = renderHook();
await advanceTimers(600);
const hook = getHook();
expect(hook.loading).toBe(false);
expect(hook.config[0].label).toBe('Aldi Biblisweg');
expect(hook.config[hook.config.length - 1].label).toBe('Penny Baden-Oos');
});
test('mutators update entries as expected', async () => {
const getHook = renderHook();
await advanceTimers(600);
await act(async () => {
getHook().toggleActive(0);
getHook().toggleProfileCheck(0);
getHook().toggleOnlyNotify(0);
});
let updated = getHook().config[0];
expect(updated.active).toBe(true);
expect(updated.checkProfileId).toBe(false);
expect(updated.onlyNotify).toBe(true);
await act(async () => {
getHook().changeWeekday(0, 'Montag');
});
updated = getHook().config[0];
expect(updated.desiredWeekday).toBe('Montag');
expect(updated.desiredDateRange).toBeUndefined();
await act(async () => {
getHook().updateDateRange(updated.id, new Date('2025-01-01'), new Date('2025-01-03'));
});
updated = getHook().config[0];
expect(updated.desiredDateRange).toEqual({
start: '2025-01-01',
end: '2025-01-03'
});
expect(updated.desiredWeekday).toBeUndefined();
});
});