Manuel Bustillo 07b88aa85b
Some checks failed
Check usage of free licenses / build-static-assets (pull_request) Successful in 27s
Add copyright notice / copyright_notice (pull_request) Successful in 43s
Playwright Tests / test (pull_request) Failing after 4m9s
Refactor guests API
2024-11-17 16:59:16 +01:00

48 lines
1.3 KiB
TypeScript

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));
}