Private
Public Access
añadido crud de grupos
This commit is contained in:
1 parent
ed3b51668b
commit
e3a2b1a47f
8 files changed
+418
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import type { Grupo } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function actualizarGrupo(id: number, data: Partial<Grupo>): Promise<Grupo> {
|
||||
const response = await fetch(`${API_URL}/grupos/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Grupo>(response);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { Grupo } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function crearGrupo(data: Partial<Grupo>): Promise<Grupo> {
|
||||
const response = await fetch(`${API_URL}/grupos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Grupo>(response);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function eliminarGrupo(id: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
if (!response.ok) throw new Error('Error al eliminar grupo');
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { Grupo } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.error || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function obtenerGrupos(): Promise<Grupo[]> {
|
||||
const response = await fetch(`${API_URL}/grupos`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
return handleResponse<Grupo[]>(response);
|
||||
}
|
||||
Reference in new issue
Block a user