Split multi-hook frontend modules into single-hook files
This commit is contained in:
44
frontend/src/hooks/useSchedule.ts
Normal file
44
frontend/src/hooks/useSchedule.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* React hook for fetching and updating the blocklist import schedule.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { fetchSchedule, updateSchedule } from "../api/blocklist";
|
||||
import { handleFetchError } from "../utils/fetchError";
|
||||
import type { ScheduleConfig, ScheduleInfo } from "../types/blocklist";
|
||||
|
||||
export interface UseScheduleReturn {
|
||||
info: ScheduleInfo | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
saveSchedule: (config: ScheduleConfig) => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and update the blocklist import schedule.
|
||||
*/
|
||||
export function useSchedule(): UseScheduleReturn {
|
||||
const [info, setInfo] = useState<ScheduleInfo | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
fetchSchedule()
|
||||
.then((data) => {
|
||||
setInfo(data);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
handleFetchError(err, setError, "Failed to load schedule");
|
||||
setLoading(false);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const saveSchedule = useCallback(async (config: ScheduleConfig): Promise<void> => {
|
||||
const updated = await updateSchedule(config);
|
||||
setInfo(updated);
|
||||
}, []);
|
||||
|
||||
return { info, loading, error, saveSchedule };
|
||||
}
|
||||
Reference in New Issue
Block a user