50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
import { Guest } from '@/app/lib/definitions';
|
|
import { getCsrfToken } from '@/app/lib/utils';
|
|
|
|
export function loadGuests(onLoad?: (guests: Guest[]) => void) {
|
|
fetch("/api/guests.json")
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
onLoad && onLoad(data.map((record: any) => {
|
|
return ({
|
|
id: record.id,
|
|
name: record.name,
|
|
status: record.status,
|
|
group_name: record.group.name,
|
|
});
|
|
}));
|
|
}, (error) => {
|
|
return [];
|
|
});
|
|
};
|
|
|
|
export function updateGuest(guest: Guest) {
|
|
fetch(`/api/guests/${guest.id}`,
|
|
{
|
|
method: 'PUT',
|
|
body: JSON.stringify({ guest: { name: guest.name, status: guest.status } }),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
}
|
|
})
|
|
.catch((error) => console.error(error));
|
|
}
|
|
|
|
export function createGuest(name: string, group_id: string, onCreate?: () => void) {
|
|
fetch("/api/guests", {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name: name, group_id: group_id }),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
}
|
|
})
|
|
.then((response) => response.json())
|
|
.then((data) => {
|
|
onCreate && onCreate();
|
|
})
|
|
.catch((error) => console.error(error));
|
|
} |