refactor to use new settings return type
This commit is contained in:
1 parent
2f93b70d7f
commit
fc655e7711
5 files changed
+57
-49
No files matched your search
@@ -3,7 +3,7 @@ import { networkService } from "../main";
|
|||||||
import { createToast } from "../Utils/toastUtils";
|
import { createToast } from "../Utils/toastUtils";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
const useFetchSettings = ({ setSettings }) => {
|
const useFetchSettings = ({ setSettingsData }) => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [error, setError] = useState(undefined);
|
const [error, setError] = useState(undefined);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -11,7 +11,7 @@ const useFetchSettings = ({ setSettings }) => {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await networkService.getAppSettings();
|
const response = await networkService.getAppSettings();
|
||||||
setSettings(response?.data?.data);
|
setSettingsData(response?.data?.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({ body: "Failed to fetch settings" });
|
createToast({ body: "Failed to fetch settings" });
|
||||||
setError(error);
|
setError(error);
|
||||||
|
|||||||
@@ -8,12 +8,8 @@ import { PropTypes } from "prop-types";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import Dialog from "../../Components/Dialog";
|
import Dialog from "../../Components/Dialog";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
const SettingsDemoMonitors = ({
|
|
||||||
isLoading,
|
const SettingsDemoMonitors = ({ HEADER_SX, handleChange, isLoading }) => {
|
||||||
authIsLoading,
|
|
||||||
checksIsLoading,
|
|
||||||
handleChange,
|
|
||||||
}) => {
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
// Local state
|
// Local state
|
||||||
@@ -23,16 +19,14 @@ const SettingsDemoMonitors = ({
|
|||||||
<ConfigBox>
|
<ConfigBox>
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="h1">{t("settingsDemoMonitors")}</Typography>
|
<Typography component="h1">{t("settingsDemoMonitors")}</Typography>
|
||||||
<Typography sx={{ mt: theme.spacing(2) }}>
|
<Typography sx={HEADER_SX}>{t("settingsDemoMonitorsDescription")}</Typography>
|
||||||
{t("settingsDemoMonitorsDescription")}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Typography>{t("settingsAddDemoMonitors")}</Typography>
|
<Typography>{t("settingsAddDemoMonitors")}</Typography>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="accent"
|
color="accent"
|
||||||
loading={isLoading || authIsLoading || checksIsLoading}
|
loading={isLoading}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const syntheticEvent = {
|
const syntheticEvent = {
|
||||||
target: {
|
target: {
|
||||||
@@ -59,7 +53,7 @@ const SettingsDemoMonitors = ({
|
|||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="error"
|
color="error"
|
||||||
loading={isLoading || authIsLoading || checksIsLoading}
|
loading={isLoading}
|
||||||
onClick={() => setIsOpen(true)}
|
onClick={() => setIsOpen(true)}
|
||||||
sx={{ mt: theme.spacing(4) }}
|
sx={{ mt: theme.spacing(4) }}
|
||||||
>
|
>
|
||||||
@@ -81,7 +75,7 @@ const SettingsDemoMonitors = ({
|
|||||||
handleChange(syntheticEvent);
|
handleChange(syntheticEvent);
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}}
|
}}
|
||||||
isLoading={isLoading || authIsLoading || checksIsLoading}
|
isLoading={isLoading}
|
||||||
/>
|
/>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
</>
|
</>
|
||||||
@@ -89,10 +83,8 @@ const SettingsDemoMonitors = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
SettingsDemoMonitors.propTypes = {
|
SettingsDemoMonitors.propTypes = {
|
||||||
isLoading: PropTypes.bool,
|
|
||||||
authIsLoading: PropTypes.bool,
|
|
||||||
checksIsLoading: PropTypes.bool,
|
|
||||||
handleChange: PropTypes.func,
|
handleChange: PropTypes.func,
|
||||||
|
HEADER_SX: PropTypes.object,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SettingsDemoMonitors;
|
export default SettingsDemoMonitors;
|
||||||
@@ -11,26 +11,28 @@ import { PropTypes } from "prop-types";
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
const SettingsPagespeed = ({ HEADING_SX, settings, setSettings }) => {
|
const SettingsPagespeed = ({
|
||||||
|
HEADING_SX,
|
||||||
|
settingsData,
|
||||||
|
setSettingsData,
|
||||||
|
isApiKeySet,
|
||||||
|
}) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
// Local state
|
// Local state
|
||||||
const [apiKey, setApiKey] = useState("");
|
const [apiKey, setApiKey] = useState("");
|
||||||
const [isApiKeySet, setIsApiKeySet] = useState(false);
|
|
||||||
const [hasBeenReset, setHasBeenReset] = useState(false);
|
const [hasBeenReset, setHasBeenReset] = useState(false);
|
||||||
|
|
||||||
// Handler
|
// Handler
|
||||||
const handleChange = (e) => {
|
const handleChange = (e) => {
|
||||||
setApiKey(e.target.value);
|
setApiKey(e.target.value);
|
||||||
setSettings({ ...settings, pagespeedApiKey: e.target.value });
|
setSettingsData({
|
||||||
|
...settingsData,
|
||||||
|
settings: { ...settingsData.settings, pagespeedApiKey: e.target.value },
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
console.log(settings);
|
|
||||||
setIsApiKeySet(Boolean(settings.pagespeedApiKey) && apiKey === "");
|
|
||||||
}, [settings, apiKey]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ConfigBox>
|
<ConfigBox>
|
||||||
<Box>
|
<Box>
|
||||||
@@ -56,7 +58,10 @@ const SettingsPagespeed = ({ HEADING_SX, settings, setSettings }) => {
|
|||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setApiKey("");
|
setApiKey("");
|
||||||
setSettings({ ...settings, pagespeedApiKey: "" });
|
setSettingsData({
|
||||||
|
...settingsData,
|
||||||
|
settings: { ...settingsData.settings, pagespeedApiKey: "" },
|
||||||
|
});
|
||||||
setHasBeenReset(true);
|
setHasBeenReset(true);
|
||||||
}}
|
}}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@@ -74,8 +79,9 @@ const SettingsPagespeed = ({ HEADING_SX, settings, setSettings }) => {
|
|||||||
|
|
||||||
SettingsPagespeed.propTypes = {
|
SettingsPagespeed.propTypes = {
|
||||||
HEADING_SX: PropTypes.object,
|
HEADING_SX: PropTypes.object,
|
||||||
settings: PropTypes.object,
|
settingsData: PropTypes.object,
|
||||||
setSettings: PropTypes.func,
|
setSettingsData: PropTypes.func,
|
||||||
|
isApiKeySet: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SettingsPagespeed;
|
export default SettingsPagespeed;
|
||||||
@@ -12,7 +12,7 @@ import { PropTypes } from "prop-types";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
const SettingsStats = ({ HEADING_SX, handleChange, settings, errors }) => {
|
const SettingsStats = ({ HEADING_SX, handleChange, settingsData, errors }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
@@ -34,7 +34,7 @@ const SettingsStats = ({ HEADING_SX, handleChange, settings, errors }) => {
|
|||||||
name="checkTTL"
|
name="checkTTL"
|
||||||
label={t("settingsTTLLabel")}
|
label={t("settingsTTLLabel")}
|
||||||
optionalLabel={t("settingsTTLOptionalLabel")}
|
optionalLabel={t("settingsTTLOptionalLabel")}
|
||||||
value={settings?.checkTTL ?? ""}
|
value={settingsData?.settings?.checkTTL ?? ""}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
type="number"
|
type="number"
|
||||||
error={errors.checkTTL ? true : false}
|
error={errors.checkTTL ? true : false}
|
||||||
@@ -77,7 +77,7 @@ const SettingsStats = ({ HEADING_SX, handleChange, settings, errors }) => {
|
|||||||
SettingsStats.propTypes = {
|
SettingsStats.propTypes = {
|
||||||
HEADING_SX: PropTypes.object,
|
HEADING_SX: PropTypes.object,
|
||||||
handleChange: PropTypes.func,
|
handleChange: PropTypes.func,
|
||||||
settings: PropTypes.object,
|
settingsData: PropTypes.object,
|
||||||
errors: PropTypes.object,
|
errors: PropTypes.object,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import SettingsUI from "./SettingsUI";
|
|||||||
import SettingsPagespeed from "./SettingsPagespeed";
|
import SettingsPagespeed from "./SettingsPagespeed";
|
||||||
import SettingsDemoMonitors from "./SettingsDemoMonitors";
|
import SettingsDemoMonitors from "./SettingsDemoMonitors";
|
||||||
import SettingsAbout from "./SettingsAbout";
|
import SettingsAbout from "./SettingsAbout";
|
||||||
|
import SettingsEmail from "./SettingsEmail";
|
||||||
import Button from "@mui/material/Button";
|
import Button from "@mui/material/Button";
|
||||||
// Utils
|
// Utils
|
||||||
import { settingsValidation } from "../../Validation/validation";
|
import { settingsValidation } from "../../Validation/validation";
|
||||||
@@ -15,7 +16,6 @@ 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";
|
||||||
import { setTimezone, setMode, setLanguage } from "../../Features/UI/uiSlice";
|
import { setTimezone, setMode, setLanguage } from "../../Features/UI/uiSlice";
|
||||||
import { getAppSettings } from "../../Features/Settings/settingsSlice";
|
|
||||||
import SettingsStats from "./SettingsStats";
|
import SettingsStats from "./SettingsStats";
|
||||||
import {
|
import {
|
||||||
deleteMonitorChecksByTeamId,
|
deleteMonitorChecksByTeamId,
|
||||||
@@ -34,17 +34,14 @@ const Settings = () => {
|
|||||||
const { user } = useSelector((state) => state.auth);
|
const { user } = useSelector((state) => state.auth);
|
||||||
|
|
||||||
// Local state
|
// Local state
|
||||||
const [settings, setSettings] = useState({});
|
const [settingsData, setSettingsData] = useState({});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
// Network
|
// Network
|
||||||
const [isSettingsLoading, settingsError] = useFetchSettings({
|
const [isSettingsLoading, settingsError] = useFetchSettings({
|
||||||
settings,
|
setSettingsData,
|
||||||
setSettings,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const [isSaving, saveError, saveSettings] = useSaveSettings();
|
const [isSaving, saveError, saveSettings] = useSaveSettings();
|
||||||
|
|
||||||
const [deleteMonitorStats, isDeletingMonitorStats] = UseDeleteMonitorStats();
|
const [deleteMonitorStats, isDeletingMonitorStats] = UseDeleteMonitorStats();
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
@@ -58,10 +55,15 @@ const Settings = () => {
|
|||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
|
|
||||||
// Build next state early
|
// Build next state early
|
||||||
const newSettings = { ...settings, [name]: value };
|
const newSettingsData = {
|
||||||
|
...settingsData,
|
||||||
|
settings: { ...settingsData.settings, [name]: value },
|
||||||
|
};
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
const { error } = settingsValidation.validate(newSettings, { abortEarly: false });
|
const { error } = settingsValidation.validate(newSettingsData.settings, {
|
||||||
|
abortEarly: false,
|
||||||
|
});
|
||||||
if (!error || error.details.length === 0) {
|
if (!error || error.details.length === 0) {
|
||||||
setErrors({});
|
setErrors({});
|
||||||
} else {
|
} else {
|
||||||
@@ -118,11 +120,13 @@ const Settings = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setSettings(newSettings);
|
setSettingsData(newSettingsData);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
const { error } = settingsValidation.validate(settings, { abortEarly: false });
|
const { error } = settingsValidation.validate(settingsData.settings, {
|
||||||
|
abortEarly: false,
|
||||||
|
});
|
||||||
if (!error || error.details.length === 0) {
|
if (!error || error.details.length === 0) {
|
||||||
setErrors({});
|
setErrors({});
|
||||||
} else {
|
} else {
|
||||||
@@ -132,7 +136,7 @@ const Settings = () => {
|
|||||||
});
|
});
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
}
|
}
|
||||||
saveSettings(settings);
|
saveSettings(settingsData?.settings);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -152,21 +156,27 @@ const Settings = () => {
|
|||||||
/>
|
/>
|
||||||
<SettingsPagespeed
|
<SettingsPagespeed
|
||||||
HEADING_SX={HEADING_SX}
|
HEADING_SX={HEADING_SX}
|
||||||
settings={settings}
|
settingsData={settingsData}
|
||||||
setSettings={setSettings}
|
setSettingsData={setSettingsData}
|
||||||
|
isApiKeySet={settingsData?.pagespeedKeySet ?? false}
|
||||||
/>
|
/>
|
||||||
<SettingsStats
|
<SettingsStats
|
||||||
HEADING_SX={HEADING_SX}
|
HEADING_SX={HEADING_SX}
|
||||||
settings={settings}
|
settingsData={settingsData}
|
||||||
setSettings={setSettings}
|
|
||||||
handleChange={handleChange}
|
handleChange={handleChange}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
/>
|
/>
|
||||||
<SettingsDemoMonitors
|
<SettingsDemoMonitors
|
||||||
isLoading={false}
|
HEADER_SX={HEADING_SX}
|
||||||
authIsLoading={false}
|
|
||||||
checksIsLoading={false}
|
|
||||||
handleChange={handleChange}
|
handleChange={handleChange}
|
||||||
|
isLoading={isSettingsLoading || isSaving || isDeletingMonitorStats}
|
||||||
|
/>
|
||||||
|
<SettingsEmail
|
||||||
|
HEADER_SX={HEADING_SX}
|
||||||
|
handleChange={handleChange}
|
||||||
|
settingsData={settingsData}
|
||||||
|
setSettingsData={setSettingsData}
|
||||||
|
isPasswordSet={settingsData?.emailPasswordSet ?? false}
|
||||||
/>
|
/>
|
||||||
<SettingsAbout />
|
<SettingsAbout />
|
||||||
<Stack
|
<Stack
|
||||||
|
|||||||
Reference in new issue
Block a user