add a temp api key field to separate control state form visual state

This commit is contained in:
Alex Holliday committed 2025-05-08 13:44:59 -07:00
1 parent 7a855ef47a
commit 57d87114de
1 file changed
+36 -42
+36 -42
View File
@@ -1,5 +1,5 @@
// Components // Components
import { Box, Stack, Typography, Button, Switch } from "@mui/material"; import { Box, Stack, Typography, Button } from "@mui/material";
import TextInput from "../../Components/Inputs/TextInput"; import TextInput from "../../Components/Inputs/TextInput";
import Link from "../../Components/Link"; import Link from "../../Components/Link";
import Select from "../../Components/Inputs/Select"; import Select from "../../Components/Inputs/Select";
@@ -25,12 +25,7 @@ import {
} from "../../Features/UptimeMonitors/uptimeMonitorsSlice"; } from "../../Features/UptimeMonitors/uptimeMonitorsSlice";
import { update } from "../../Features/Auth/authSlice"; import { update } from "../../Features/Auth/authSlice";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { import { setTimezone, setMode, setLanguage } from "../../Features/UI/uiSlice";
setTimezone,
setMode,
setDistributedUptimeEnabled,
setLanguage,
} from "../../Features/UI/uiSlice";
import timezones from "../../Utils/timezones.json"; import timezones from "../../Utils/timezones.json";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { networkService } from "../../main"; import { networkService } from "../../main";
@@ -52,15 +47,18 @@ const Settings = () => {
const { isLoading: authIsLoading } = useSelector((state) => state.auth); const { isLoading: authIsLoading } = useSelector((state) => state.auth);
const { timezone, distributedUptimeEnabled } = useSelector((state) => state.ui); const { timezone, distributedUptimeEnabled } = useSelector((state) => state.ui);
const { mode } = useSelector((state) => state.ui); const { mode } = useSelector((state) => state.ui);
const { pagespeedApiKey } = useSelector((state) => state.settings);
const [checksIsLoading, setChecksIsLoading] = useState(false); const [checksIsLoading, setChecksIsLoading] = useState(false);
const [form, setForm] = useState({ const [form, setForm] = useState({
enableDistributedUptime: distributedUptimeEnabled, enableDistributedUptime: distributedUptimeEnabled,
ttl: checkTTL ? (checkTTL / SECONDS_PER_DAY).toString() : 0, ttl: checkTTL ? (checkTTL / SECONDS_PER_DAY).toString() : 0,
pagespeedApiKey: "", pagespeedApiKey: pagespeedApiKey,
}); });
const [version, setVersion] = useState("unknown"); const [version, setVersion] = useState("unknown");
const [apiKeyFieldType, setApiKeyFieldType] = useState("password"); const [apiKeyFieldType, setApiKeyFieldType] = useState("password");
const [isApiKeySet, setIsApiKeySet] = useState(false); const [isApiKeySet, setIsApiKeySet] = useState(pagespeedApiKey ? true : false);
const [tempPSKey, setTempPSKey] = useState("");
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const deleteStatsMonitorsInitState = { deleteMonitors: false, deleteStats: false }; const deleteStatsMonitorsInitState = { deleteMonitors: false, deleteStats: false };
const [isOpen, setIsOpen] = useState(deleteStatsMonitorsInitState); const [isOpen, setIsOpen] = useState(deleteStatsMonitorsInitState);
@@ -87,25 +85,7 @@ const Settings = () => {
useEffect(() => { useEffect(() => {
dispatch(getAppSettings()); dispatch(getAppSettings());
}, []); }, [dispatch]);
const { pagespeedApiKey } = useSelector((state) => state.settings);
useEffect(() => {
if (pagespeedApiKey) {
setIsApiKeySet(true);
setForm((prev) => ({
...prev,
pagespeedApiKey: t("maskedPageSpeedKeyPlaceholder"),
}));
} else {
setIsApiKeySet(false);
setForm((prev) => ({
...prev,
pagespeedApiKey: "",
}));
}
}, [pagespeedApiKey]);
const handleChange = (event) => { const handleChange = (event) => {
const { type, checked, value, id } = event.target; const { type, checked, value, id } = event.target;
@@ -118,16 +98,18 @@ const Settings = () => {
return; return;
} }
if (id === "pagespeedApiKey") {
setTempPSKey(value);
return;
}
let inputValue = value; let inputValue = value;
if (id === "ttl") { if (id === "ttl") {
inputValue = value.replace(/[^0-9]/g, ""); inputValue = value.replace(/[^0-9]/g, "");
} }
const updatedForm = { ...form, [id]: inputValue }; const updatedForm = { ...form, [id]: inputValue };
const { error } = settingsValidation.validate( const { error } = settingsValidation.validate(updatedForm, { abortEarly: false });
updatedForm,
{ abortEarly: false }
);
if (!error || error.details.length === 0) { if (!error || error.details.length === 0) {
setErrors({}); setErrors({});
@@ -139,7 +121,7 @@ const Settings = () => {
setErrors(newErrors); setErrors(newErrors);
logger.error("Validation errors:", error.details); logger.error("Validation errors:", error.details);
} }
setForm(updatedForm); setForm(updatedForm);
}; };
@@ -153,7 +135,14 @@ const Settings = () => {
const updatedUser = { ...user, checkTTL: form.ttl }; const updatedUser = { ...user, checkTTL: form.ttl };
const [userAction, settingsAction] = await Promise.all([ const [userAction, settingsAction] = await Promise.all([
dispatch(update({ localData: updatedUser })), dispatch(update({ localData: updatedUser })),
dispatch(updateAppSettings({ settings: { language: language, pagespeedApiKey: form.pagespeedApiKey } })), dispatch(
updateAppSettings({
settings: {
language: language,
pagespeedApiKey: tempPSKey ? tempPSKey : form.pagespeedApiKey,
},
})
),
]); ]);
if (userAction.payload.success && settingsAction.payload.success) { if (userAction.payload.success && settingsAction.payload.success) {
@@ -221,7 +210,7 @@ const Settings = () => {
...prev, ...prev,
pagespeedApiKey: "", pagespeedApiKey: "",
})); }));
}; };
const languages = Object.keys(i18n.options.resources || {}); const languages = Object.keys(i18n.options.resources || {});
@@ -324,7 +313,7 @@ const Settings = () => {
<TextInput <TextInput
id="pagespeedApiKey" id="pagespeedApiKey"
label={t("pageSpeedApiKeyFieldLabel")} label={t("pageSpeedApiKeyFieldLabel")}
value={form.pagespeedApiKey} value={form.pagespeedApiKey ? form.pagespeedApiKey : tempPSKey}
type={apiKeyFieldType} type={apiKeyFieldType}
onChange={handleChange} onChange={handleChange}
disabled={isApiKeySet} disabled={isApiKeySet}
@@ -341,12 +330,17 @@ const Settings = () => {
} }
/> />
{isApiKeySet && ( {isApiKeySet && (
<Box> <Box>
<Typography>{t("pageSpeedApiKeyFieldResetLabel")}</Typography> <Typography>{t("pageSpeedApiKeyFieldResetLabel")}</Typography>
<Button onClick={handleResetApiKey} variant="contained" color="error" sx={{ mt: theme.spacing(4) }}> <Button
{t("reset")} onClick={handleResetApiKey}
</Button> variant="contained"
</Box> color="error"
sx={{ mt: theme.spacing(4) }}
>
{t("reset")}
</Button>
</Box>
)} )}
</Stack> </Stack>
</ConfigBox> </ConfigBox>