Extract APIs to their own files #108

Merged
bustikiller merged 8 commits from extract-apis into main 2024-11-17 17:02:08 +00:00
2 changed files with 25 additions and 15 deletions
Showing only changes of commit 28abb23a77 - Show all commits

View File

@ -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 [];
});
}

View File

@ -6,23 +6,16 @@ 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<Array<TableArrangement>>([]);
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);
});
}
@ -30,7 +23,7 @@ export default function ArrangementsTable ({onArrangementSelected}: {onArrangeme
onArrangementSelected(e.currentTarget.getAttribute('data-arrangement-id') || '');
}
arrangements.length === 0 && loadArrangements();
!arrangementsLoaded && refreshSimulations();
return(
<TableOfContents