Extract groups API

This commit is contained in:
Manuel Bustillo 2024-11-17 16:34:58 +01:00 committed by bustikiller
parent 470366b8f6
commit 53c4938e92
2 changed files with 35 additions and 27 deletions

25
app/api/groups.tsx Normal file
View File

@ -0,0 +1,25 @@
import { Group } from '@/app/lib/definitions';
export function loadGroups(onLoad?: (groups: Group[]) => void) {
fetch("/api/groups.json")
.then((response) => response.json())
.then((data) => {
onLoad && onLoad(data.map((record: any) => {
return ({
id: record.id,
name: record.name,
color: record.color,
attendance: {
considered: record.considered,
invited: record.invited,
confirmed: record.confirmed,
tentative: record.tentative,
declined: record.declined,
total: record.total,
}
});
}));
}, (error) => {
return [];
});
}

View File

@ -11,33 +11,9 @@ import GuestsTable from '@/app/ui/guests/table';
import { TabPanel, TabView } from 'primereact/tabview'; import { TabPanel, TabView } from 'primereact/tabview';
import { Suspense, useState } from 'react'; import { Suspense, useState } from 'react';
import { loadGuests, updateGuestStatus } from '@/app/api/guests'; import { loadGuests, updateGuestStatus } from '@/app/api/guests';
import { loadGroups } from '@/app/api/groups';
export default function Page() { export default function Page() {
function loadGroups() {
fetch("/api/groups")
.then((response) => response.json())
.then((data) => {
setGroups(data.map((record: any) => {
return ({
id: record.id,
name: record.name,
color: record.color,
attendance: {
considered: record.considered,
invited: record.invited,
confirmed: record.confirmed,
tentative: record.tentative,
declined: record.declined,
total: record.total,
}
});
}));
setGroupsLoaded(true);
}, (error) => {
return [];
});
}
function refreshGuests() { function refreshGuests() {
loadGuests((guests) => { loadGuests((guests) => {
setGuests(guests); setGuests(guests);
@ -45,13 +21,20 @@ export default function Page() {
}); });
} }
function refreshGroups() {
loadGroups((groups) => {
setGroups(groups);
setGroupsLoaded(true);
});
}
const [groupsLoaded, setGroupsLoaded] = useState(false); const [groupsLoaded, setGroupsLoaded] = useState(false);
const [groups, setGroups] = useState<Array<Group>>([]); const [groups, setGroups] = useState<Array<Group>>([]);
const [guestsLoaded, setGuestsLoaded] = useState(false); const [guestsLoaded, setGuestsLoaded] = useState(false);
const [guests, setGuests] = useState<Array<Guest>>([]); const [guests, setGuests] = useState<Array<Guest>>([]);
!groupsLoaded && loadGroups(); !groupsLoaded && refreshGroups();
!guestsLoaded && refreshGuests(); !guestsLoaded && refreshGuests();
return ( return (