From 9c2c84a45b2d5a6a85073f69bd6850ca33972080 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 11 Aug 2024 17:26:16 +0200 Subject: [PATCH] Display the hierarchy of groups received from the backend --- app/ui/guests/affinity-groups-tree.tsx | 112 ++++++++----------------- app/ui/guests/table.tsx | 2 +- 2 files changed, 37 insertions(+), 77 deletions(-) diff --git a/app/ui/guests/affinity-groups-tree.tsx b/app/ui/guests/affinity-groups-tree.tsx index 1cfeb9c..95863da 100644 --- a/app/ui/guests/affinity-groups-tree.tsx +++ b/app/ui/guests/affinity-groups-tree.tsx @@ -1,89 +1,49 @@ 'use client' -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, Suspense } from 'react'; import { Tree } from 'primereact/tree'; import { PrimeIcons } from 'primereact/api'; +import { debug } from 'console'; export default function AffinityGroupsTree() { - const [nodes, setNodes] = useState( - [ - { - key: 1, - label: "Lilly's guests", - icon: PrimeIcons.HEART, - draggable: false, - children: [ - { - key: 11, - label: "Family", - icon: PrimeIcons.HOME, - leaf: true, - className: "px-4", - }, - { - key: 12, - label: "Country club", - icon: PrimeIcons.SUN, - className: "px-4", + 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)); + }); - }, - { - key: 13, - label: "Work", - icon: PrimeIcons.DESKTOP, - className: "px-4", - }, - ] - }, - { - key: 2, - label: "Dave's guests", - icon: PrimeIcons.HEART_FILL, - draggable: false, - children: [ - { - key: 21, - label: "Family", - icon: PrimeIcons.HOME, - leaf: true, - className: "px-4", - }, - { - key: 22, - label: "College friends", - icon: PrimeIcons.GRADUATION_CAP, - leaf: true, - className: "px-4", - }, - ] - }, - { - key: 3, - label: "Common guests", - icon: PrimeIcons.USERS, - draggable: false, - children: [ - { - key: 31, - label: "Common workplace", - icon: PrimeIcons.BRIEFCASE, - leaf: true, - className: "px-4", - }, - { - key: 32, - label: "Lifetime friends", - icon: PrimeIcons.STAR, - leaf: true, - className: "px-4", - }, - ] - } - ]); + 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)); + })) + }); + }); return (
- setNodes(e.value)} className="w-full md:w-30rem" /> + + setNodes(e.value)} className="w-full md:w-30rem" /> +
) } \ No newline at end of file diff --git a/app/ui/guests/table.tsx b/app/ui/guests/table.tsx index 3d90381..6340b5c 100644 --- a/app/ui/guests/table.tsx +++ b/app/ui/guests/table.tsx @@ -42,7 +42,7 @@ export default function guestsTable() { - + {guests.map((guest) => (