feat: Корректировки 07.11.25
This commit is contained in:
@@ -222,33 +222,55 @@ const LinkedStationsContentsInner = <
|
||||
setSelectedItems(updated);
|
||||
};
|
||||
|
||||
const handleBulkLink = () => {
|
||||
const handleBulkLink = async () => {
|
||||
if (selectedItems.size === 0) return;
|
||||
setError(null);
|
||||
|
||||
setIsLinkingBulk(true);
|
||||
Promise.all(
|
||||
Array.from(selectedItems).map((id) =>
|
||||
authInstance.post(`/${parentResource}/${parentId}/${childResource}`, {
|
||||
const idsToLink = Array.from(selectedItems);
|
||||
const linkedIds: number[] = [];
|
||||
const failedIds: number[] = [];
|
||||
|
||||
for (const id of idsToLink) {
|
||||
try {
|
||||
await authInstance.post(`/${parentResource}/${parentId}/${childResource}`, {
|
||||
station_id: id,
|
||||
})
|
||||
)
|
||||
)
|
||||
.then(() => {
|
||||
const newItems = allItems.filter((item) =>
|
||||
selectedItems.has(item.id)
|
||||
);
|
||||
setLinkedItems([...linkedItems, ...newItems]);
|
||||
setSelectedItems(new Set());
|
||||
onUpdate?.();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error bulk linking stations:", error);
|
||||
setError("Failed to link stations");
|
||||
})
|
||||
.finally(() => {
|
||||
setIsLinkingBulk(false);
|
||||
});
|
||||
linkedIds.push(id);
|
||||
} catch (error) {
|
||||
console.error("Error linking station:", error);
|
||||
failedIds.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
if (linkedIds.length > 0) {
|
||||
const newItems = allItems.filter((item) => linkedIds.includes(item.id));
|
||||
setLinkedItems((prev) => {
|
||||
const existingIds = new Set(prev.map((item) => item.id));
|
||||
const additions = newItems.filter((item) => !existingIds.has(item.id));
|
||||
return [...prev, ...additions];
|
||||
});
|
||||
onUpdate?.();
|
||||
}
|
||||
|
||||
setSelectedItems((prev) => {
|
||||
if (linkedIds.length === 0) {
|
||||
return prev;
|
||||
}
|
||||
const remaining = new Set(prev);
|
||||
linkedIds.forEach((id) => remaining.delete(id));
|
||||
return failedIds.length > 0 ? remaining : new Set();
|
||||
});
|
||||
|
||||
if (failedIds.length > 0) {
|
||||
setError(
|
||||
failedIds.length === idsToLink.length
|
||||
? "Failed to link stations"
|
||||
: "Some stations failed to link"
|
||||
);
|
||||
}
|
||||
|
||||
setIsLinkingBulk(false);
|
||||
};
|
||||
|
||||
const toggleDetachSelection = (itemId: number) => {
|
||||
@@ -269,7 +291,7 @@ const LinkedStationsContentsInner = <
|
||||
setSelectedToDetach(new Set(linkedItems.map((item) => item.id)));
|
||||
};
|
||||
|
||||
const handleBulkDetach = () => {
|
||||
const handleBulkDetach = async () => {
|
||||
const idsToDetach = Array.from(selectedToDetach);
|
||||
if (idsToDetach.length === 0) return;
|
||||
setError(null);
|
||||
@@ -281,32 +303,47 @@ const LinkedStationsContentsInner = <
|
||||
return next;
|
||||
});
|
||||
|
||||
Promise.all(
|
||||
idsToDetach.map((itemId) =>
|
||||
authInstance.delete(`/${parentResource}/${parentId}/${childResource}`, {
|
||||
const detachedIds: number[] = [];
|
||||
const failedIds: number[] = [];
|
||||
|
||||
for (const itemId of idsToDetach) {
|
||||
try {
|
||||
await authInstance.delete(`/${parentResource}/${parentId}/${childResource}`, {
|
||||
data: { [`${childResource}_id`]: itemId },
|
||||
})
|
||||
)
|
||||
)
|
||||
.then(() => {
|
||||
setLinkedItems(
|
||||
linkedItems.filter((item) => !idsToDetach.includes(item.id))
|
||||
);
|
||||
setSelectedToDetach(new Set());
|
||||
onUpdate?.();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error bulk deleting stations:", error);
|
||||
setError("Failed to delete stations");
|
||||
})
|
||||
.finally(() => {
|
||||
setDetachingIds((prev) => {
|
||||
const next = new Set(prev);
|
||||
idsToDetach.forEach((id) => next.delete(id));
|
||||
return next;
|
||||
});
|
||||
setIsBulkDetaching(false);
|
||||
detachedIds.push(itemId);
|
||||
} catch (error) {
|
||||
console.error("Error deleting station:", error);
|
||||
failedIds.push(itemId);
|
||||
}
|
||||
}
|
||||
|
||||
if (detachedIds.length > 0) {
|
||||
setLinkedItems((prev) =>
|
||||
prev.filter((item) => !detachedIds.includes(item.id))
|
||||
);
|
||||
setSelectedToDetach((prev) => {
|
||||
const remaining = new Set(prev);
|
||||
detachedIds.forEach((id) => remaining.delete(id));
|
||||
return failedIds.length > 0 ? remaining : new Set();
|
||||
});
|
||||
onUpdate?.();
|
||||
}
|
||||
|
||||
if (failedIds.length > 0) {
|
||||
setError(
|
||||
failedIds.length === idsToDetach.length
|
||||
? "Failed to delete stations"
|
||||
: "Some stations failed to delete"
|
||||
);
|
||||
}
|
||||
|
||||
setDetachingIds((prev) => {
|
||||
const next = new Set(prev);
|
||||
idsToDetach.forEach((id) => next.delete(id));
|
||||
return next;
|
||||
});
|
||||
setIsBulkDetaching(false);
|
||||
};
|
||||
|
||||
const allSelectedForDetach =
|
||||
|
||||
Reference in New Issue
Block a user