WIP load guests from API

This commit is contained in:
Manuel Bustillo 2024-08-11 14:38:49 +02:00
parent 8c6b19a96e
commit 231e33f76c

View File

@ -1,32 +1,29 @@
import Image from 'next/image';
import { UpdateInvoice, DeleteInvoice } from '@/app/ui/guests/buttons';
import gueststatus from '@/app/ui/guests/status';
import { formatDateToLocal, formatCurrency } from '@/app/lib/utils';
import { Guest } from '@/app/lib/definitions';
'use client'
export default async function guestsTable() {
const guests: Guest[] = [
{
id: '1',
name: 'John Doe',
email: 'foo@bar.com'
},
{
id: '2',
name: 'Jane Doe',
email: 'jane@bar.com',
},
{
id: '3',
name: 'John Doe',
email: 'foo@bar.com'
},
{
id: '4',
name: 'Jane Doe',
email: 'jane@bar.com',
},
];
import { Guest } from '@/app/lib/definitions';
import { useState, Suspense } from 'react';
export default function guestsTable() {
const url = "http://localhost:3001/guests.json";
const getData = (): Guest[] => {
var guests: Guest[] = [];
fetch(url)
.then((response) => response.json())
.then((data) => {
data.data.map((record: any) => {
guests.push({
id: record.id,
name: record.attributes.name,
email: record.attributes.email,
});
});
});
return guests;
}
const [guests, setGuests] = useState<Guest[]>(getData());
return (
<div className="w-full relative overflow-x-auto shadow-md sm:rounded-lg">
@ -45,6 +42,7 @@ export default async function guestsTable() {
</tr>
</thead>
<tbody>
<Suspense fallback="<tr><td colspan=3> Loading data</td></tr>">
{guests.map((guest) => (
<tr className="bg-white border-b odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800">
<th scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@ -58,6 +56,7 @@ export default async function guestsTable() {
</td>
</tr>
))}
</Suspense>
</tbody>
</table>
</div>