Prevent self-referencing locations and items as parents (#773)

* prevent current location and descendants from being selected as parent

* prevent an item from showing up in the parent items drop-down for itself

* pass location object to filter function to allow for more flexible filtering

* align exclude prop and fix type comparison, change item filter to array of ItemsObjects to allow for descendant filtering in future

* fix linting prop reference
This commit is contained in:
Nikolai Oakfield
2025-06-28 18:58:46 -04:00
committed by GitHub
parent c1c8eb649c
commit c9d055fe03
5 changed files with 45 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
import type { Ref } from "vue";
import type { TreeItem } from "~~/lib/api/types/data-contracts";
import type { TreeItem, LocationSummary } from "~~/lib/api/types/data-contracts";
export interface FlatTreeItem {
id: string;
@@ -35,9 +35,29 @@ function flatTree(tree: TreeItem[]): FlatTreeItem[] {
return v;
}
export function useFlatLocations(): Ref<FlatTreeItem[]> {
const locations = useLocationStore();
function filterOutSubtree(tree: TreeItem[], excludeId: string): TreeItem[] {
// Recursively filters out a subtree starting from excludeId
const result: TreeItem[] = [];
for (const item of tree) {
if (item.id === excludeId) {
continue;
}
const newItem = { ...item };
if (item.children) {
newItem.children = filterOutSubtree(item.children, excludeId);
}
result.push(newItem);
}
return result;
}
export function useFlatLocations(excludeSubtreeForLocation?: LocationSummary): Ref<FlatTreeItem[]> {
const locations = useLocationStore();
if (locations.tree === null) {
locations.refreshTree();
}
@@ -47,6 +67,10 @@ export function useFlatLocations(): Ref<FlatTreeItem[]> {
return [];
}
return flatTree(locations.tree);
const filteredTree = excludeSubtreeForLocation
? filterOutSubtree(locations.tree, excludeSubtreeForLocation.id)
: locations.tree;
return flatTree(filteredTree);
});
}