remove settings hooks
This commit is contained in:
1 parent
2091d8dcd7
commit
b817a16459
3 files changed
+36
-96
No files matched your search
@@ -2,19 +2,19 @@ import { useEffect, useState } from "react";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import { logger } from "../../../Utils/Logger.js";
|
import { logger } from "../../../Utils/Logger.js";
|
||||||
import { networkService } from "../../../main.jsx";
|
import { useLazyGet } from "@/Hooks/UseApi";
|
||||||
|
|
||||||
const withAdminCheck = (WrappedComponent) => {
|
const withAdminCheck = (WrappedComponent) => {
|
||||||
const WithAdminCheck = (props) => {
|
const WithAdminCheck = (props) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [isChecking, setIsChecking] = useState(true);
|
|
||||||
const [superAdminExists, setSuperAdminExists] = useState(false);
|
const [superAdminExists, setSuperAdminExists] = useState(false);
|
||||||
|
const [hasChecked, setHasChecked] = useState(false);
|
||||||
|
const { get: checkSuperAdmin, loading: isChecking } = useLazyGet();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
networkService
|
checkSuperAdmin("/auth/users/superadmin")
|
||||||
.doesSuperAdminExist()
|
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response?.data?.data === true) {
|
if (response?.data === true) {
|
||||||
navigate("/login");
|
navigate("/login");
|
||||||
} else {
|
} else {
|
||||||
setSuperAdminExists(false);
|
setSuperAdminExists(false);
|
||||||
@@ -24,11 +24,11 @@ const withAdminCheck = (WrappedComponent) => {
|
|||||||
logger.error(error);
|
logger.error(error);
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
setIsChecking(false);
|
setHasChecked(true);
|
||||||
});
|
});
|
||||||
}, [navigate]);
|
}, [navigate, checkSuperAdmin]);
|
||||||
|
|
||||||
if (isChecking) {
|
if (!hasChecked || isChecking) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
import { useState, useEffect } from "react";
|
|
||||||
import { networkService } from "../main.jsx";
|
|
||||||
import { createToast } from "../Utils/toastUtils.jsx";
|
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
|
|
||||||
const useFetchSettings = ({ setSettingsData, setIsApiKeySet, setIsEmailPasswordSet }) => {
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [error, setError] = useState(undefined);
|
|
||||||
useEffect(() => {
|
|
||||||
const fetchSettings = async () => {
|
|
||||||
setIsLoading(true);
|
|
||||||
try {
|
|
||||||
const response = await networkService.getAppSettings();
|
|
||||||
setSettingsData(response?.data?.data);
|
|
||||||
setIsApiKeySet(response?.data?.data?.pagespeedKeySet);
|
|
||||||
setIsEmailPasswordSet(response?.data?.data?.emailPasswordSet);
|
|
||||||
} catch (error) {
|
|
||||||
createToast({ body: "Failed to fetch settings" });
|
|
||||||
setError(error);
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
fetchSettings();
|
|
||||||
}, [setSettingsData]);
|
|
||||||
|
|
||||||
return [isLoading, error];
|
|
||||||
};
|
|
||||||
|
|
||||||
const useSaveSettings = ({
|
|
||||||
setSettingsData,
|
|
||||||
setIsApiKeySet,
|
|
||||||
setApiKeyHasBeenReset,
|
|
||||||
setIsEmailPasswordSet,
|
|
||||||
setEmailPasswordHasBeenReset,
|
|
||||||
}) => {
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [error, setError] = useState(undefined);
|
|
||||||
const { t } = useTranslation();
|
|
||||||
|
|
||||||
const saveSettings = async (settings) => {
|
|
||||||
setIsLoading(true);
|
|
||||||
try {
|
|
||||||
const settingsResponse = await networkService.updateAppSettings({ settings });
|
|
||||||
// if (settings.checkTTL) {
|
|
||||||
// await networkService.updateChecksTTL({
|
|
||||||
// ttl: settings.checkTTL,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
setIsApiKeySet(settingsResponse.data.data.pagespeedKeySet);
|
|
||||||
setIsEmailPasswordSet(settingsResponse.data.data.emailPasswordSet);
|
|
||||||
if (settingsResponse.data.data.pagespeedKeySet === true) {
|
|
||||||
setApiKeyHasBeenReset(false);
|
|
||||||
}
|
|
||||||
if (settingsResponse.data.data.emailPasswordSet === true) {
|
|
||||||
setEmailPasswordHasBeenReset(false);
|
|
||||||
}
|
|
||||||
setSettingsData(settingsResponse.data.data);
|
|
||||||
|
|
||||||
createToast({ body: t("settingsSuccessSaved") });
|
|
||||||
} catch (error) {
|
|
||||||
createToast({ body: t("settingsFailedToSave") });
|
|
||||||
|
|
||||||
setError(error);
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return [isLoading, error, saveSettings];
|
|
||||||
};
|
|
||||||
|
|
||||||
export { useFetchSettings, useSaveSettings };
|
|
||||||
@@ -13,7 +13,7 @@ import SettingsExport from "./SettingsExport.jsx";
|
|||||||
import Button from "@mui/material/Button";
|
import Button from "@mui/material/Button";
|
||||||
// Utils
|
// Utils
|
||||||
import { settingsValidation } from "@/Validation/validation.js";
|
import { settingsValidation } from "@/Validation/validation.js";
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
@@ -26,9 +26,8 @@ import {
|
|||||||
} from "@/Features/UI/uiSlice.js";
|
} from "@/Features/UI/uiSlice.js";
|
||||||
import SettingsStats from "./SettingsStats.jsx";
|
import SettingsStats from "./SettingsStats.jsx";
|
||||||
|
|
||||||
import { useFetchSettings, useSaveSettings } from "@/Hooks/settingsHooks";
|
|
||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||||
import { usePost, useDelete, useLazyGet } from "@/Hooks/UseApi";
|
import { useGet, usePost, useDelete, useLazyGet, usePut } from "@/Hooks/UseApi";
|
||||||
// Constants
|
// Constants
|
||||||
const BREADCRUMBS = [{ name: `Settings`, path: "/settings" }];
|
const BREADCRUMBS = [{ name: `Settings`, path: "/settings" }];
|
||||||
|
|
||||||
@@ -53,19 +52,33 @@ const Settings = () => {
|
|||||||
const [emailPasswordHasBeenReset, setEmailPasswordHasBeenReset] = useState(false);
|
const [emailPasswordHasBeenReset, setEmailPasswordHasBeenReset] = useState(false);
|
||||||
|
|
||||||
// Network
|
// Network
|
||||||
const [isSettingsLoading, settingsError] = useFetchSettings({
|
const { data: fetchedSettings, isLoading: isSettingsLoading } = useGet("/settings");
|
||||||
setSettingsData,
|
|
||||||
setIsApiKeySet,
|
|
||||||
setIsEmailPasswordSet,
|
|
||||||
});
|
|
||||||
|
|
||||||
const [isSaving, saveError, saveSettings] = useSaveSettings({
|
useEffect(() => {
|
||||||
setSettingsData,
|
if (fetchedSettings) {
|
||||||
setIsApiKeySet,
|
setSettingsData(fetchedSettings);
|
||||||
setApiKeyHasBeenReset,
|
setIsApiKeySet(fetchedSettings?.pagespeedKeySet);
|
||||||
setIsEmailPasswordSet,
|
setIsEmailPasswordSet(fetchedSettings?.emailPasswordSet);
|
||||||
setEmailPasswordHasBeenReset,
|
}
|
||||||
});
|
}, [fetchedSettings]);
|
||||||
|
|
||||||
|
const { put: saveSettingsFn, loading: isSaving } = usePut();
|
||||||
|
|
||||||
|
const saveSettings = async (settings) => {
|
||||||
|
const response = await saveSettingsFn("/settings", { settings });
|
||||||
|
if (response?.data) {
|
||||||
|
const data = response.data;
|
||||||
|
setIsApiKeySet(data.pagespeedKeySet);
|
||||||
|
setIsEmailPasswordSet(data.emailPasswordSet);
|
||||||
|
if (data.pagespeedKeySet === true) {
|
||||||
|
setApiKeyHasBeenReset(false);
|
||||||
|
}
|
||||||
|
if (data.emailPasswordSet === true) {
|
||||||
|
setEmailPasswordHasBeenReset(false);
|
||||||
|
}
|
||||||
|
setSettingsData(data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// New API hooks to replace monitorHooks
|
// New API hooks to replace monitorHooks
|
||||||
const { post: postDemoMonitors, loading: isAddingDemoMonitors } = usePost();
|
const { post: postDemoMonitors, loading: isAddingDemoMonitors } = usePost();
|
||||||
|
|||||||
Reference in new issue
Block a user