feat: Update left sidebar and fetch queries
This commit is contained in:
@@ -118,6 +118,10 @@ const LinkedStationsContentsInner = <
|
||||
const parentResource = "sight";
|
||||
const childResource = "station";
|
||||
|
||||
const buildPayload = (ids: number[]) => ({
|
||||
[`${childResource}_ids`]: ids,
|
||||
});
|
||||
|
||||
const availableItems = allItems
|
||||
.filter((item) => !linkedItems.some((linked) => linked.id === item.id))
|
||||
.filter((item) => {
|
||||
@@ -159,9 +163,7 @@ const LinkedStationsContentsInner = <
|
||||
const linkItem = () => {
|
||||
if (selectedItemId !== null) {
|
||||
setError(null);
|
||||
const requestData = {
|
||||
station_id: selectedItemId,
|
||||
};
|
||||
const requestData = buildPayload([selectedItemId]);
|
||||
|
||||
setIsLinkingSingle(true);
|
||||
authInstance
|
||||
@@ -193,7 +195,7 @@ const LinkedStationsContentsInner = <
|
||||
});
|
||||
authInstance
|
||||
.delete(`/${parentResource}/${parentId}/${childResource}`, {
|
||||
data: { [`${childResource}_id`]: itemId },
|
||||
data: buildPayload([itemId]),
|
||||
})
|
||||
.then(() => {
|
||||
setLinkedItems(linkedItems.filter((item) => item.id !== itemId));
|
||||
@@ -228,46 +230,28 @@ const LinkedStationsContentsInner = <
|
||||
|
||||
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}`, {
|
||||
station_id: id,
|
||||
});
|
||||
linkedIds.push(id);
|
||||
} catch (error) {
|
||||
console.error("Error linking station:", 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 stations"
|
||||
: "Some stations failed to link"
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error linking stations:", error);
|
||||
setError("Failed to link stations");
|
||||
}
|
||||
|
||||
setIsLinkingBulk(false);
|
||||
@@ -303,39 +287,26 @@ const LinkedStationsContentsInner = <
|
||||
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 station:", 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 stations"
|
||||
: "Some stations failed to delete"
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error deleting stations:", error);
|
||||
setError("Failed to delete stations");
|
||||
}
|
||||
|
||||
setDetachingIds((prev) => {
|
||||
@@ -499,8 +470,9 @@ const LinkedStationsContentsInner = <
|
||||
<Autocomplete
|
||||
fullWidth
|
||||
value={
|
||||
availableItems?.find((item) => item.id === selectedItemId) ||
|
||||
null
|
||||
availableItems?.find(
|
||||
(item) => item.id === selectedItemId
|
||||
) || null
|
||||
}
|
||||
onChange={(_, newValue) =>
|
||||
setSelectedItemId(newValue?.id || null)
|
||||
@@ -508,7 +480,11 @@ const LinkedStationsContentsInner = <
|
||||
options={availableItems}
|
||||
getOptionLabel={(item) => String(item.name)}
|
||||
renderInput={(params) => (
|
||||
<TextField {...params} label="Выберите остановку" fullWidth />
|
||||
<TextField
|
||||
{...params}
|
||||
label="Выберите остановку"
|
||||
fullWidth
|
||||
/>
|
||||
)}
|
||||
isOptionEqualToValue={(option, value) =>
|
||||
option.id === value?.id
|
||||
|
||||
Reference in New Issue
Block a user