Files
Checkmate/client/src/Hooks/useFetchSettings.js
T

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-05-09 13:29:05 -07:00
import { useState, useEffect } from "react";
import { networkService } from "../main";
import { createToast } from "../Utils/toastUtils";
import { useTranslation } from "react-i18next";
2025-05-09 14:54:28 -07:00
const useFetchSettings = ({ setSettingsData }) => {
2025-05-09 13:29:05 -07:00
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(undefined);
useEffect(() => {
const fetchSettings = async () => {
setIsLoading(true);
try {
const response = await networkService.getAppSettings();
2025-05-09 14:54:28 -07:00
setSettingsData(response?.data?.data);
2025-05-09 13:29:05 -07:00
} catch (error) {
createToast({ body: "Failed to fetch settings" });
setError(error);
} finally {
setIsLoading(false);
}
};
fetchSettings();
}, []);
return [isLoading, error];
};
const useSaveSettings = () => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(undefined);
const { t } = useTranslation();
const saveSettings = async (settings) => {
setIsLoading(true);
try {
2025-06-13 09:16:07 +08:00
const settingsResponse = await networkService.updateAppSettings({ settings });
2025-05-09 13:29:05 -07:00
if (settings.checkTTL) {
await networkService.updateChecksTTL({
ttl: settings.checkTTL,
});
}
2025-06-13 09:16:07 +08:00
console.log({ settingsResponse });
2025-05-09 13:29:05 -07:00
createToast({ body: t("settingsSuccessSaved") });
} catch (error) {
createToast({ body: t("settingsFailedToSave") });
setError(error);
} finally {
setIsLoading(false);
}
};
return [isLoading, error, saveSettings];
};
export { useFetchSettings, useSaveSettings };