65 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-11-17 16:00:30 +00:00
/* Copyright (C) 2024 Manuel Bustillo*/
2024-11-17 16:32:10 +01:00
import { Guest } from '@/app/lib/definitions';
import { getCsrfToken } from '@/app/lib/utils';
export function loadGuests(onLoad?: (guests: Guest[]) => void) {
2024-11-17 17:58:08 +01:00
fetch("/api/guests")
2024-11-17 16:32:10 +01:00
.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,
groupId: record.group.id,
2024-11-17 16:32:10 +01:00
});
}));
}, (error) => {
return [];
});
};
2024-11-17 16:59:16 +01:00
export function updateGuest(guest: Guest) {
2024-11-17 17:50:06 +01:00
return fetch(`/api/guests/${guest.id}`,
2024-11-17 16:32:10 +01:00
{
2024-11-17 16:59:16 +01:00
method: 'PUT',
body: JSON.stringify({ guest: { name: guest.name, status: guest.status, group_id: guest.groupId } }),
2024-11-17 16:32:10 +01:00
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.catch((error) => console.error(error));
}
export function createGuest(guest: Guest, onCreate?: () => void) {
2024-11-17 16:32:10 +01:00
fetch("/api/guests", {
method: 'POST',
body: JSON.stringify({ name: guest.name, group_id: guest.groupId, status: guest.status }),
2024-11-17 16:32:10 +01:00
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then((response) => response.json())
.then((data) => {
onCreate && onCreate();
})
.catch((error) => console.error(error));
2024-11-17 18:29:50 +01:00
}
export function destroyGuest(guest: Guest, onDestroy?: () => void) {
fetch(`/api/guests/${guest.id}`, {
method: 'DELETE',
headers: {
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then((response) => response.json())
.then((data) => {
onDestroy && onDestroy();
})
.catch((error) => console.error(error));
2024-11-17 16:32:10 +01:00
}