refactor: extract shared applyReorder helper in CarManagement
Agent-Logs-Url: https://github.com/pdf114514/CarReservation/sessions/c5f8f1a2-8a8b-4951-8442-76ce37d906ae Co-authored-by: pdf114514 <57948770+pdf114514@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
3b49844d0b
commit
7a04012b60
@@ -118,11 +118,7 @@ export default function CarManagement({ reloadKey = 0 }) {
|
||||
}
|
||||
};
|
||||
|
||||
const handleReorder = async (index, direction) => {
|
||||
const newCars = [...cars];
|
||||
const swapIndex = index + direction;
|
||||
if (swapIndex < 0 || swapIndex >= newCars.length) return;
|
||||
[newCars[index], newCars[swapIndex]] = [newCars[swapIndex], newCars[index]];
|
||||
const applyReorder = async (newCars) => {
|
||||
setCars(newCars);
|
||||
try {
|
||||
await api.reorderCars(newCars.map((c) => c.id));
|
||||
@@ -132,6 +128,14 @@ export default function CarManagement({ reloadKey = 0 }) {
|
||||
}
|
||||
};
|
||||
|
||||
const handleReorder = async (index, direction) => {
|
||||
const swapIndex = index + direction;
|
||||
if (swapIndex < 0 || swapIndex >= cars.length) return;
|
||||
const newCars = [...cars];
|
||||
[newCars[index], newCars[swapIndex]] = [newCars[swapIndex], newCars[index]];
|
||||
await applyReorder(newCars);
|
||||
};
|
||||
|
||||
const handleDragStart = (index) => {
|
||||
dragSrcIdx.current = index;
|
||||
};
|
||||
@@ -155,13 +159,7 @@ export default function CarManagement({ reloadKey = 0 }) {
|
||||
const newCars = [...cars];
|
||||
const [moved] = newCars.splice(srcIndex, 1);
|
||||
newCars.splice(dropIndex, 0, moved);
|
||||
setCars(newCars);
|
||||
try {
|
||||
await api.reorderCars(newCars.map((c) => c.id));
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
await loadCars();
|
||||
}
|
||||
await applyReorder(newCars);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user