97 lines
3.9 KiB
JavaScript
97 lines
3.9 KiB
JavaScript
const BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
|
export function localToUTC(localValue) {
|
|
return localValue;
|
|
}
|
|
export async function fetchActiveSession() {
|
|
const response = await fetch(`${BASE_URL}/api/sessions/active`);
|
|
if (!response.ok)
|
|
throw new Error('Failed to fetch active session');
|
|
const data = await response.json();
|
|
return data.session;
|
|
}
|
|
export async function fetchSessions(from_date, to_date, limit = 200) {
|
|
const params = new URLSearchParams();
|
|
if (from_date)
|
|
params.append('from_date', from_date);
|
|
if (to_date)
|
|
params.append('to_date', to_date);
|
|
if (limit)
|
|
params.append('limit', limit.toString());
|
|
const response = await fetch(`${BASE_URL}/api/sessions?${params.toString()}`);
|
|
if (!response.ok)
|
|
throw new Error('Failed to fetch sessions');
|
|
return response.json();
|
|
}
|
|
export async function createSession(payload) {
|
|
const utcTime = localToUTC(payload.start_time);
|
|
const response = await fetch(`${BASE_URL}/api/sessions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ start_time: utcTime }),
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.detail || 'Failed to create session');
|
|
}
|
|
return response.json();
|
|
}
|
|
export async function endSession(sessionId, payload) {
|
|
const utcTime = payload.end_time ? localToUTC(payload.end_time) : undefined;
|
|
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}/end`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ end_time: utcTime }),
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.detail || 'Failed to end session');
|
|
}
|
|
return response.json();
|
|
}
|
|
export async function updateSession(sessionId, payload) {
|
|
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.detail || 'Failed to update session');
|
|
}
|
|
return response.json();
|
|
}
|
|
export async function deleteSession(sessionId) {
|
|
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
|
method: 'DELETE',
|
|
});
|
|
if (!response.ok)
|
|
throw new Error('Failed to delete session');
|
|
}
|
|
export async function fetchDailySummary(from_date, to_date, day_start, day_end) {
|
|
const fromDT = from_date + 'T00:00:00';
|
|
const toDT = to_date + 'T23:59:59';
|
|
const params = new URLSearchParams({ from_date: fromDT, to_date: toDT, day_start, day_end });
|
|
const response = await fetch(`${BASE_URL}/api/stats/daily-summary?${params.toString()}`);
|
|
if (!response.ok)
|
|
throw new Error('Failed to fetch daily summary');
|
|
return response.json();
|
|
}
|
|
export async function fetchTimetable(from_date, to_date, day_start, day_end) {
|
|
const fromDT = from_date + 'T00:00:00';
|
|
const toDT = to_date + 'T23:59:59';
|
|
const params = new URLSearchParams({ from_date: fromDT, to_date: toDT, day_start, day_end });
|
|
const response = await fetch(`${BASE_URL}/api/stats/timetable?${params.toString()}`);
|
|
if (!response.ok)
|
|
throw new Error('Failed to fetch timetable');
|
|
return response.json();
|
|
}
|
|
export async function fetchWakeSleepTimes(from_date, to_date, wakeup_ref, bedtime_ref) {
|
|
const fromDT = from_date + 'T00:00:00';
|
|
const toDT = to_date + 'T23:59:59';
|
|
const params = new URLSearchParams({ from_date: fromDT, to_date: toDT, wakeup_ref, bedtime_ref });
|
|
const response = await fetch(`${BASE_URL}/api/stats/wake-sleep-times?${params.toString()}`);
|
|
if (!response.ok)
|
|
throw new Error('Failed to fetch wake/sleep times');
|
|
return response.json();
|
|
}
|