50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
|
|
'use client';
|
||
|
|
|
||
|
|
import { useCallback, useEffect, useState } from 'react';
|
||
|
|
import { getLocalDayIsoRange } from '@/components/appointments/appointmentTime';
|
||
|
|
import { todayApi } from '@/lib/api/today';
|
||
|
|
import type { TodaySummaryData } from '@/types/today';
|
||
|
|
import type { ApiError } from '@/types/api';
|
||
|
|
|
||
|
|
interface UseTodaySummaryResult {
|
||
|
|
data: TodaySummaryData | null;
|
||
|
|
loading: boolean;
|
||
|
|
error: ApiError | null;
|
||
|
|
reload: () => Promise<void>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useTodaySummary(enabled = true): UseTodaySummaryResult {
|
||
|
|
const [data, setData] = useState<TodaySummaryData | null>(null);
|
||
|
|
const [loading, setLoading] = useState(enabled);
|
||
|
|
const [error, setError] = useState<ApiError | null>(null);
|
||
|
|
|
||
|
|
const reload = useCallback(async () => {
|
||
|
|
if (!enabled) {
|
||
|
|
setData(null);
|
||
|
|
setLoading(false);
|
||
|
|
setError(null);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
setLoading(true);
|
||
|
|
setError(null);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const range = getLocalDayIsoRange(new Date());
|
||
|
|
const response = await todayApi.summary(range);
|
||
|
|
setData(response.data);
|
||
|
|
} catch (err) {
|
||
|
|
setData(null);
|
||
|
|
setError(err as ApiError);
|
||
|
|
} finally {
|
||
|
|
setLoading(false);
|
||
|
|
}
|
||
|
|
}, [enabled]);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
void reload();
|
||
|
|
}, [reload]);
|
||
|
|
|
||
|
|
return { data, loading, error, reload };
|
||
|
|
}
|