|
|
|
@@ -9,7 +9,8 @@ import type {
|
|
|
|
|
SummaryResponse
|
|
|
|
|
} from '@/types';
|
|
|
|
|
|
|
|
|
|
const BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
|
|
|
|
const API_BASE = import.meta.env.BASE_URL.replace(/\/$/, '')
|
|
|
|
|
export const apiUrl = (path: string) => `${API_BASE}${path}`
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a local HH:MM time string to its UTC equivalent.
|
|
|
|
@@ -33,7 +34,7 @@ export function localToUTC(localValue: string): string {
|
|
|
|
|
|
|
|
|
|
async function fetchStats<T>(endpoint: string, from_date: string, to_date: string, extraParams: Record<string, string> = {}): Promise<T> {
|
|
|
|
|
const params = new URLSearchParams({ from_date, to_date, ...extraParams });
|
|
|
|
|
const response = await fetch(`${BASE_URL}/api/stats/${endpoint}?${params.toString()}`);
|
|
|
|
|
const response = await fetch(apiUrl(`/api/stats/${endpoint}?${params.toString()}`));
|
|
|
|
|
if (!response.ok) throw new Error(`Failed to fetch ${endpoint}`);
|
|
|
|
|
return response.json();
|
|
|
|
|
}
|
|
|
|
@@ -60,19 +61,19 @@ export async function fetchWakeSleepTimes(from_date: string, to_date: string, wa
|
|
|
|
|
|
|
|
|
|
// Session API functions
|
|
|
|
|
export async function fetchActiveSession(): Promise<SleepSessionRead | null> {
|
|
|
|
|
const response = await fetch(`${BASE_URL}/api/sessions/active`);
|
|
|
|
|
const response = await fetch(apiUrl('/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`);
|
|
|
|
|
const response = await fetch(apiUrl('/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`, {
|
|
|
|
|
const response = await fetch(apiUrl('/api/sessions'), {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
@@ -82,7 +83,7 @@ export async function createSession(payload: SleepSessionCreate): Promise<SleepS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function endSession(sessionId: number, payload: SleepSessionUpdate): Promise<SleepSessionRead> {
|
|
|
|
|
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}/end`, {
|
|
|
|
|
const response = await fetch(apiUrl(`/api/sessions/${sessionId}/end`), {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
@@ -92,14 +93,14 @@ export async function endSession(sessionId: number, payload: SleepSessionUpdate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteSession(sessionId: number): Promise<void> {
|
|
|
|
|
const response = await fetch(`${BASE_URL}/api/sessions/${sessionId}`, {
|
|
|
|
|
const response = await fetch(apiUrl(`/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}`, {
|
|
|
|
|
const response = await fetch(apiUrl(`/api/sessions/${sessionId}`), {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
|