/* Copyright (C) 2024 Manuel Bustillo*/ import { Group } from '@/app/lib/definitions'; import { getCsrfToken, getSlug } from '../lib/utils'; export function loadGroups(onLoad?: (groups: Group[]) => void) { fetch(`/api/${getSlug()}/groups`) .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 []; }); } 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)); } 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)); }