diff --git a/client/src/Hooks/useDeleteMonitorStats.js b/client/src/Hooks/useDeleteMonitorStats.js new file mode 100644 index 000000000..64138929f --- /dev/null +++ b/client/src/Hooks/useDeleteMonitorStats.js @@ -0,0 +1,24 @@ +import { useState } from "react"; +import { networkService } from "../main"; +import { createToast } from "../Utils/toastUtils"; +import { useTranslation } from "react-i18next"; + +const UseDeleteMonitorStats = () => { + const { t } = useTranslation(); + const [isLoading, setIsLoading] = useState(false); + const deleteMonitorStats = async ({ teamId }) => { + setIsLoading(true); + try { + const res = await networkService.deleteChecksByTeamId({ teamId }); + createToast({ body: t("settingsStatsCleared") }); + } catch (error) { + createToast({ body: t("settingsFailedToClearStats") }); + } finally { + setIsLoading(false); + } + }; + + return [deleteMonitorStats, isLoading]; +}; + +export { UseDeleteMonitorStats }; diff --git a/client/src/Hooks/useFetchSettings.js b/client/src/Hooks/useFetchSettings.js new file mode 100644 index 000000000..15859795f --- /dev/null +++ b/client/src/Hooks/useFetchSettings.js @@ -0,0 +1,55 @@ +import { useState, useEffect } from "react"; +import { networkService } from "../main"; +import { createToast } from "../Utils/toastUtils"; +import { useTranslation } from "react-i18next"; + +const useFetchSettings = ({ setSettings }) => { + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(undefined); + useEffect(() => { + const fetchSettings = async () => { + setIsLoading(true); + try { + const response = await networkService.getAppSettings(); + setSettings(response?.data?.data); + } 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 { + await networkService.updateAppSettings({ settings }); + if (settings.checkTTL) { + await networkService.updateChecksTTL({ + ttl: settings.checkTTL, + }); + } + createToast({ body: t("settingsSuccessSaved") }); + } catch (error) { + createToast({ body: t("settingsFailedToSave") }); + + setError(error); + } finally { + setIsLoading(false); + } + }; + + return [isLoading, error, saveSettings]; +}; + +export { useFetchSettings, useSaveSettings };