Manuel Bustillo 827271efc2
Some checks failed
Check usage of free licenses / build-static-assets (pull_request) Successful in 43s
Add copyright notice / copyright_notice (pull_request) Successful in 54s
Playwright Tests / test (pull_request) Failing after 3m23s
Simplify guest edition by adding status to the dialog
2024-11-17 19:49:39 +01:00

65 lines
1.7 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")
.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,
});
}));
}, (error) => {
return [];
});
};
export function updateGuest(guest: Guest) {
return fetch(`/api/guests/${guest.id}`,
{
method: 'PUT',
body: JSON.stringify({ guest: { name: guest.name, status: guest.status, group_id: guest.groupId } }),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.catch((error) => console.error(error));
}
export function createGuest(guest: Guest, onCreate?: () => void) {
fetch("/api/guests", {
method: 'POST',
body: JSON.stringify({ name: guest.name, group_id: guest.groupId, status: guest.status }),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then((response) => response.json())
.then((data) => {
onCreate && onCreate();
})
.catch((error) => console.error(error));
}
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));
}