feat: full backend (untested)
This commit is contained in:
78
frontend/src/api/client.ts
Normal file
78
frontend/src/api/client.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';
|
||||
|
||||
export interface ParticipantAPI {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
ics_url: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface TimeSlotAPI {
|
||||
day: string;
|
||||
hour: number;
|
||||
availability: 'full' | 'partial' | 'none';
|
||||
availableParticipants: string[];
|
||||
}
|
||||
|
||||
export interface CreateParticipantRequest {
|
||||
name: string;
|
||||
email: string;
|
||||
ics_url: string;
|
||||
}
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ detail: 'Request failed' }));
|
||||
throw new Error(error.detail || 'Request failed');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function fetchParticipants(): Promise<ParticipantAPI[]> {
|
||||
const response = await fetch(`${API_URL}/api/participants`);
|
||||
return handleResponse<ParticipantAPI[]>(response);
|
||||
}
|
||||
|
||||
export async function createParticipant(data: CreateParticipantRequest): Promise<ParticipantAPI> {
|
||||
const response = await fetch(`${API_URL}/api/participants`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<ParticipantAPI>(response);
|
||||
}
|
||||
|
||||
export async function deleteParticipant(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/api/participants/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete participant');
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchAvailability(participantIds: string[]): Promise<TimeSlotAPI[]> {
|
||||
const response = await fetch(`${API_URL}/api/availability`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ participant_ids: participantIds }),
|
||||
});
|
||||
const data = await handleResponse<{ slots: TimeSlotAPI[] }>(response);
|
||||
return data.slots;
|
||||
}
|
||||
|
||||
export async function syncCalendars(): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/api/sync`, { method: 'POST' });
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to sync calendars');
|
||||
}
|
||||
}
|
||||
|
||||
export async function syncParticipant(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/api/sync/${id}`, { method: 'POST' });
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to sync participant calendar');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user