2024-08-11 16:14:12 +02:00
|
|
|
'use client'
|
|
|
|
|
2024-08-11 17:26:16 +02:00
|
|
|
import React, { useState, useEffect, Suspense } from 'react';
|
2024-08-11 16:14:12 +02:00
|
|
|
import { Tree } from 'primereact/tree';
|
|
|
|
import { PrimeIcons } from 'primereact/api';
|
2024-08-11 17:26:16 +02:00
|
|
|
import { debug } from 'console';
|
2024-08-11 16:14:12 +02:00
|
|
|
|
|
|
|
export default function AffinityGroupsTree() {
|
2024-08-11 17:26:16 +02:00
|
|
|
const [nodes, setNodes] = useState([]);
|
|
|
|
const parseNode = (record: any, included: any[]) => {
|
|
|
|
if (!record.attributes) {
|
|
|
|
record = included.find((a) => a.id === record.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
const children = (record?.relationships?.children?.data || []).map((child: any) => {
|
|
|
|
return (parseNode(child, included));
|
|
|
|
});
|
2024-08-11 16:14:12 +02:00
|
|
|
|
2024-08-11 17:26:16 +02:00
|
|
|
return ({
|
|
|
|
key: record.id,
|
|
|
|
label: record.attributes.name,
|
|
|
|
icon: record.attributes.icon,
|
|
|
|
children: children,
|
|
|
|
className: "px-4",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (nodes.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetch("http://localhost:3001/groups.json")
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
setNodes(data.data.map((record: any) => {
|
|
|
|
return (parseNode(record, data.included));
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
});
|
2024-08-11 16:14:12 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="card flex justify-content-center">
|
2024-08-11 17:26:16 +02:00
|
|
|
<Suspense>
|
|
|
|
<Tree value={nodes} dragdropScope="affinity-groups" onDragDrop={(e) => setNodes(e.value)} className="w-full md:w-30rem" />
|
|
|
|
</Suspense>
|
2024-08-11 16:14:12 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|