feat: Update left sidebar and fetch queries

This commit is contained in:
2025-11-12 03:14:03 +03:00
parent 0a6192c7da
commit 6f32c6e671
5 changed files with 192 additions and 234 deletions

View File

@@ -118,6 +118,10 @@ const LinkedSightsContentsInner = <
const parentResource = "station";
const childResource = "sight";
const buildPayload = (ids: number[]) => ({
[`${childResource}_ids`]: ids,
});
const availableItems = allItems
.filter((item) => !linkedItems.some((linked) => linked.id === item.id))
.filter((item) => {
@@ -160,9 +164,7 @@ const LinkedSightsContentsInner = <
const linkItem = () => {
if (selectedItemId !== null) {
setError(null);
const requestData = {
sight_id: selectedItemId,
};
const requestData = buildPayload([selectedItemId]);
setIsLinkingSingle(true);
authInstance
@@ -194,7 +196,7 @@ const LinkedSightsContentsInner = <
});
authInstance
.delete(`/${parentResource}/${parentId}/${childResource}`, {
data: { [`${childResource}_id`]: itemId },
data: buildPayload([itemId]),
})
.then(() => {
setLinkedItems(linkedItems.filter((item) => item.id !== itemId));
@@ -229,49 +231,28 @@ const LinkedSightsContentsInner = <
setIsLinkingBulk(true);
const idsToLink = Array.from(selectedItems);
const linkedIds: number[] = [];
const failedIds: number[] = [];
for (const id of idsToLink) {
try {
await authInstance.post(
`/${parentResource}/${parentId}/${childResource}`,
{
sight_id: id,
}
);
linkedIds.push(id);
} catch (error) {
console.error("Error linking sight:", error);
failedIds.push(id);
}
}
try {
await authInstance.post(
`/${parentResource}/${parentId}/${childResource}`,
buildPayload(idsToLink)
);
if (linkedIds.length > 0) {
const newItems = allItems.filter((item) => linkedIds.includes(item.id));
const newItems = allItems.filter((item) => idsToLink.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];
});
setSelectedItems((prev) => {
const remaining = new Set(prev);
idsToLink.forEach((id) => remaining.delete(id));
return remaining;
});
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 sights"
: "Some sights failed to link"
);
} catch (error) {
console.error("Error linking sights:", error);
setError("Failed to link sights");
}
setIsLinkingBulk(false);
@@ -307,42 +288,26 @@ const LinkedSightsContentsInner = <
return next;
});
const detachedIds: number[] = [];
const failedIds: number[] = [];
try {
await authInstance.delete(
`/${parentResource}/${parentId}/${childResource}`,
{
data: buildPayload(idsToDetach),
}
);
for (const itemId of idsToDetach) {
try {
await authInstance.delete(
`/${parentResource}/${parentId}/${childResource}`,
{
data: { [`${childResource}_id`]: itemId },
}
);
detachedIds.push(itemId);
} catch (error) {
console.error("Error deleting sight:", error);
failedIds.push(itemId);
}
}
if (detachedIds.length > 0) {
setLinkedItems((prev) =>
prev.filter((item) => !detachedIds.includes(item.id))
prev.filter((item) => !idsToDetach.includes(item.id))
);
setSelectedToDetach((prev) => {
const remaining = new Set(prev);
detachedIds.forEach((id) => remaining.delete(id));
return failedIds.length > 0 ? remaining : new Set();
idsToDetach.forEach((id) => remaining.delete(id));
return remaining;
});
onUpdate?.();
}
if (failedIds.length > 0) {
setError(
failedIds.length === idsToDetach.length
? "Failed to delete sights"
: "Some sights failed to delete"
);
} catch (error) {
console.error("Error deleting sights:", error);
setError("Failed to delete sights");
}
setDetachingIds((prev) => {