2024-11-17 16:00:30 +00:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
2024-11-17 16:34:58 +01:00
|
|
|
import { Group } from '@/app/lib/definitions';
|
2024-12-08 12:45:15 +01:00
|
|
|
import { getCsrfToken, getSlug } from '../lib/utils';
|
2024-11-17 16:34:58 +01:00
|
|
|
|
|
|
|
export function loadGroups(onLoad?: (groups: Group[]) => void) {
|
2024-12-01 18:02:25 +01:00
|
|
|
fetch(`/api/${getSlug()}/groups`)
|
2024-11-17 16:34:58 +01:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
onLoad && onLoad(data.map((record: any) => {
|
|
|
|
return ({
|
|
|
|
id: record.id,
|
|
|
|
name: record.name,
|
|
|
|
color: record.color,
|
|
|
|
attendance: {
|
|
|
|
considered: record.considered,
|
|
|
|
invited: record.invited,
|
|
|
|
confirmed: record.confirmed,
|
|
|
|
tentative: record.tentative,
|
|
|
|
declined: record.declined,
|
|
|
|
total: record.total,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}, (error) => {
|
|
|
|
return [];
|
|
|
|
});
|
2024-12-08 12:45:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateGroup(group: Group) {
|
|
|
|
return fetch(`/api/${getSlug()}/groups/${group.id}`,
|
|
|
|
{
|
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify({ group: {
|
|
|
|
name: group.name,
|
|
|
|
color: group.color,
|
|
|
|
icon: group.icon,
|
|
|
|
parent_id: group.parentId
|
|
|
|
} }),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => console.error(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createGroup(group: Group, onCreate?: () => void) {
|
|
|
|
fetch(`/api/${getSlug()}/groups`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: group.name,
|
|
|
|
color: group.color,
|
|
|
|
icon: group.icon,
|
|
|
|
parent_id: group.parentId
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
onCreate && onCreate();
|
|
|
|
})
|
|
|
|
.catch((error) => console.error(error));
|
2024-12-08 12:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function destroyGroup(group: Group, onDestroy?: () => void) {
|
|
|
|
fetch(`/api/${getSlug()}/groups/${group.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:34:58 +01:00
|
|
|
}
|