fix children not displayed if first occurence of a node is the leave of the tree

This commit is contained in:
axolotle
2021-03-01 17:00:23 +01:00
parent 24903f986a
commit f00c250b0e
+23 -16
View File
@@ -43,33 +43,40 @@ export function toManyManyData (rawData) {
h.each(node => { h.each(node => {
if (node.parent && node.children) { if (node.parent && node.children) {
const id = node.parent.data.id const id = node.parent.data.id
// Remove reference of parent in child's children.
node.children = node.children.filter(child => child.data.id !== id) node.children = node.children.filter(child => child.data.id !== id)
} }
}) })
const ids = []
const nodes = []
const links = [] const links = []
flatten(h.descendants()).filter(node => { const nodes = flatten(h.descendants()).reduce((nodes, node) => {
if (!ids.includes(node.data.id)) { const sameNode = nodes.find(n => node.data.id === n.data.id)
ids.push(node.data.id) if (sameNode) {
return true if (!node.children) return nodes
node.children.forEach(child => {
if (!sameNode.children.find(c => child.data.id === c.data.id)) {
sameNode.children.push(child)
}
})
} else {
if (!node.children) node.children = []
nodes.push(node)
} }
return false return nodes
}).forEach(({ data, children, depth }) => { }, []).map(({ data, children, depth }) => {
nodes.push({ id: data.id, data })
if (children) { if (children) {
children.forEach(child => { children.forEach(child => {
links.push({ source: data.id, target: child.data.id }) links.push({ source: data.id, target: child.data.id })
}) })
} }
}) return {
id: data.id,
nodes.forEach(node => { data: {
Object.assign(node.data, { ...data,
type: node.data.__typename.toLowerCase(), type: data.__typename.toLowerCase(),
class: 'family-' + node.data.familles[0].id class: 'family-' + data.familles[0].id
}) }
}
}) })
return { nodes, links } return { nodes, links }
} }