diff --git a/app/api/tableSimulations.tsx b/app/api/tableSimulations.tsx new file mode 100644 index 0000000..51e1fff --- /dev/null +++ b/app/api/tableSimulations.tsx @@ -0,0 +1,17 @@ +import { TableArrangement } from '@/app/lib/definitions'; + +export function loadTableSimulations(onLoad?: (tableSimulations: TableArrangement[]) => void) { + fetch('/api/tables_arrangements') + .then((response) => response.json()) + .then((data) => { + onLoad && onLoad(data.map((record: any) => { + return ({ + id: record.id, + name: record.name, + discomfort: record.discomfort, + }); + })); + }, (error) => { + return []; + }); +} \ No newline at end of file diff --git a/app/ui/arrangements/arrangements-table.tsx b/app/ui/arrangements/arrangements-table.tsx index 66e41d4..57dd67d 100644 --- a/app/ui/arrangements/arrangements-table.tsx +++ b/app/ui/arrangements/arrangements-table.tsx @@ -6,31 +6,24 @@ import React, { useState } from "react" import { TableArrangement } from '@/app/lib/definitions'; import { classNames } from "../components/button"; import TableOfContents from "../components/table-of-contents"; +import { loadTableSimulations } from "@/app/api/tableSimulations"; export default function ArrangementsTable ({onArrangementSelected}: {onArrangementSelected: (arrangementId: string) => void}) { const [arrangements, setArrangements] = useState>([]); + const [arrangementsLoaded, setArrangementsLoaded] = useState(false); - function loadArrangements() { - fetch("/api/tables_arrangements") - .then((response) => response.json()) - .then((data) => { - setArrangements(data.map((record: any) => { - return ({ - id: record.id, - name: record.name, - discomfort: record.discomfort - }); - })); - }, (error) => { - return []; - }); + function refreshSimulations() { + loadTableSimulations((arrangements) => { + setArrangements(arrangements); + setArrangementsLoaded(true); + }); } function arrangementClicked(e: React.MouseEvent) { onArrangementSelected(e.currentTarget.getAttribute('data-arrangement-id') || ''); } - arrangements.length === 0 && loadArrangements(); + !arrangementsLoaded && refreshSimulations(); return(