Skip to content

Commit

Permalink
Solve the item edit and the shortest graph problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbrandB committed Jun 22, 2024
1 parent 686e95a commit 15409b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
4 changes: 3 additions & 1 deletion resources/js/Pages/Items/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ const submit = () => {
<option v-for="selectableItem in items"
:value="selectableItem.id"
:key="selectableItem.id"
:selected="selectableItem.id.toString()===selectedItemId">
:selected="
// @ts-ignore
selectableItem.id===selectedItemId||selectableItem.id.toString()===selectedItemId">
{{ selectableItem.title }}
</option>
</select>
Expand Down
47 changes: 30 additions & 17 deletions resources/js/Pages/Items/Graph.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ForceEdgeDatum,
} from "v-network-graph/lib/force-layout"
import Card from "@/Components/Card.vue";
import {Item, PageProps} from "@/types";
import {Item} from "@/types";
import SelectedItemDropdown from "@/CustomComponents/SelectedItemDropdown.vue";
import PrimaryButton from "@/Components/PrimaryButton.vue";
import axios from "axios";
Expand All @@ -30,6 +30,23 @@ const props = defineProps<{
initialSelectedItems: Item[]
}>();
const selectedItems = ref(new Set<Item>(props.initialSelectedItems ?? []))
onMounted(() => {
watch(selectedItems.value, (selected) => {
axios.post(route('graph.syncSelected'), {
selected: Array.from(selected).map((item: Item) => item.id)
}).then(()=>{
router.reload({
only: ['items', 'nodes', 'initialSelectedItems'],
onSuccess: (e) => {
calculate(e);
}
})
})
})
});
const page = usePage();
const nodes = ref({})
const edges = ref({})
Expand All @@ -56,12 +73,24 @@ const calculate = (pageProps: any) =>{
});
//filter out the myEdges where source is target and target is source from another item
let sameEdges = new Map<string, number>();
Array.from(myEdges).forEach((edge: Edge, index: number) => {
//@ts-ignore
sameEdges.set(`${edge.source}-${edge.target}`, sameEdges.get(`${edge.source}-${edge.target}`) + 1 || 1)
if (!myEdges.has({source: edge.target, target: edge.source})) {
//@ts-ignore
edges.value[`edge${index}`] = edge
}
});
//if the node is not needed, because it is referenced by all selected nodes and therefore there is a shorter path, delete it.
//@ts-ignore
sameEdges.entries().forEach(([key, value]) => {
if (value >= selectedItems.value.size && selectedItems.value.size > 0) {
const [_, target] = key.split('-')
//@ts-ignore
delete nodes.value[target]
}
})
}
calculate(page);
Expand Down Expand Up @@ -130,22 +159,6 @@ const configs = reactive(
},
})
)
const selectedItems = ref(new Set<Item>(props.initialSelectedItems ?? []))
onMounted(() => {
watch(selectedItems.value, (selected) => {
axios.post(route('graph.syncSelected'), {
selected: Array.from(selected).map((item: Item) => item.id)
}).then(()=>{
router.reload({
only: ['items', 'nodes', 'initialSelectedItems'],
onSuccess: (e) => {
calculate(e);
}
})
})
})
});
</script>

<template>
Expand Down

0 comments on commit 15409b5

Please sign in to comment.