Not sure what happened here...
This commit is contained in:
+36
-60
@@ -1,63 +1,56 @@
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
||||
export function localToUTC(localValue) {
|
||||
return localValue;
|
||||
return new Date(localValue).toISOString();
|
||||
}
|
||||
async function fetchStats(endpoint, from_date, to_date, extraParams = {}) {
|
||||
const fromDT = new Date(`${from_date}T00:00:00`).toISOString();
|
||||
const toDT = new Date(`${to_date}T23:59:59`).toISOString();
|
||||
const params = new URLSearchParams({ from_date: fromDT, to_date: toDT, ...extraParams });
|
||||
const response = await fetch(`${BASE_URL}/api/stats/${endpoint}?${params.toString()}`);
|
||||
if (!response.ok)
|
||||
throw new Error(`Failed to fetch ${endpoint}`);
|
||||
return response.json();
|
||||
}
|
||||
export async function fetchDailySummary(from_date, to_date, day_start, day_end) {
|
||||
return fetchStats('daily-summary', from_date, to_date, { day_start, day_end });
|
||||
}
|
||||
export async function fetchTimetable(from_date, to_date, day_start, day_end) {
|
||||
return fetchStats('timetable', from_date, to_date, { day_start, day_end });
|
||||
}
|
||||
export async function fetchWakeSleepTimes(from_date, to_date, wakeup_ref, bedtime_ref) {
|
||||
return fetchStats('wake-sleep-times', from_date, to_date, { wakeup_ref, bedtime_ref });
|
||||
}
|
||||
// Session API functions
|
||||
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;
|
||||
return null;
|
||||
return response.json();
|
||||
}
|
||||
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()}`);
|
||||
export async function fetchSessions() {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions`);
|
||||
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 }),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.detail || 'Failed to create session');
|
||||
}
|
||||
if (!response.ok)
|
||||
throw new Error('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');
|
||||
}
|
||||
if (!response.ok)
|
||||
throw new Error('Failed to end session');
|
||||
return response.json();
|
||||
}
|
||||
export async function deleteSession(sessionId) {
|
||||
@@ -67,30 +60,13 @@ export async function deleteSession(sessionId) {
|
||||
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()}`);
|
||||
export async function updateSession(sessionId, payload) {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
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');
|
||||
throw new Error('Failed to update session');
|
||||
return response.json();
|
||||
}
|
||||
|
||||
@@ -26,3 +26,53 @@ export async function fetchTimetable(from_date: string, to_date: string, day_sta
|
||||
export async function fetchWakeSleepTimes(from_date: string, to_date: string, wakeup_ref: string, bedtime_ref: string): Promise<any> {
|
||||
return fetchStats('wake-sleep-times', from_date, to_date, { wakeup_ref, bedtime_ref });
|
||||
}
|
||||
|
||||
// Session API functions
|
||||
export async function fetchActiveSession(): Promise<SleepSessionRead | null> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions/active`);
|
||||
if (!response.ok) return null;
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function fetchSessions(): Promise<SessionListResponse> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions`);
|
||||
if (!response.ok) throw new Error('Failed to fetch sessions');
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function createSession(payload: SleepSessionCreate): Promise<SleepSessionRead> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to create session');
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function endSession(sessionId: number, payload: SleepSessionUpdate): Promise<SleepSessionRead> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}/end`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to end session');
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteSession(sessionId: number): Promise<void> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to delete session');
|
||||
}
|
||||
|
||||
export async function updateSession(sessionId: number, payload: SleepSessionUpdate): Promise<SleepSessionRead> {
|
||||
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to update session');
|
||||
return response.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user