Merge pull request #2217 from bluewave-labs/feat/add-pagespeedapikey-settings
Feature - Add/Reset PageSpeed API key in Settings
This commit is contained in:
5 files changed
+98
-13
No files matched your search
@@ -104,6 +104,7 @@ const TextInput = forwardRef(
|
|||||||
fontSize={"var(--env-var-font-size-medium)"}
|
fontSize={"var(--env-var-font-size-medium)"}
|
||||||
color={theme.palette.primary.contrastTextSecondary}
|
color={theme.palette.primary.contrastTextSecondary}
|
||||||
fontWeight={500}
|
fontWeight={500}
|
||||||
|
mb={theme.spacing(2)}
|
||||||
>
|
>
|
||||||
{label}
|
{label}
|
||||||
{isRequired && <Required />}
|
{isRequired && <Required />}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const initialState = {
|
|||||||
apiBaseUrl: "",
|
apiBaseUrl: "",
|
||||||
logLevel: "debug",
|
logLevel: "debug",
|
||||||
language: "gb",
|
language: "gb",
|
||||||
|
pagespeedApiKey: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAppSettings = createAsyncThunk(
|
export const getAppSettings = createAsyncThunk(
|
||||||
@@ -33,6 +34,7 @@ export const updateAppSettings = createAsyncThunk(
|
|||||||
try {
|
try {
|
||||||
const parsedSettings = {
|
const parsedSettings = {
|
||||||
language: settings.language,
|
language: settings.language,
|
||||||
|
pagespeedApiKey: settings.pagespeedApiKey,
|
||||||
};
|
};
|
||||||
const res = await networkService.updateAppSettings({ settings: parsedSettings });
|
const res = await networkService.updateAppSettings({ settings: parsedSettings });
|
||||||
return res.data;
|
return res.data;
|
||||||
@@ -56,6 +58,7 @@ const handleGetSettingsFulfilled = (state, action) => {
|
|||||||
state.apiBaseUrl = action.payload.data.apiBaseUrl;
|
state.apiBaseUrl = action.payload.data.apiBaseUrl;
|
||||||
state.logLevel = action.payload.data.logLevel;
|
state.logLevel = action.payload.data.logLevel;
|
||||||
state.language = action.payload.data.language;
|
state.language = action.payload.data.language;
|
||||||
|
state.pagespeedApiKey = action.payload.data.pagespeedApiKey;
|
||||||
};
|
};
|
||||||
const handleGetSettingsRejected = (state, action) => {
|
const handleGetSettingsRejected = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import Select from "../../Components/Inputs/Select";
|
|||||||
import { useIsAdmin } from "../../Hooks/useIsAdmin";
|
import { useIsAdmin } from "../../Hooks/useIsAdmin";
|
||||||
import Dialog from "../../Components/Dialog";
|
import Dialog from "../../Components/Dialog";
|
||||||
import ConfigBox from "../../Components/ConfigBox";
|
import ConfigBox from "../../Components/ConfigBox";
|
||||||
|
import { PasswordEndAdornment } from "../../Components/Inputs/TextInput/Adornments";
|
||||||
|
import { getAppSettings } from "../../Features/Settings/settingsSlice";
|
||||||
// import {
|
// import {
|
||||||
// WalletMultiButton,
|
// WalletMultiButton,
|
||||||
// WalletDisconnectButton,
|
// WalletDisconnectButton,
|
||||||
@@ -54,8 +56,11 @@ const Settings = () => {
|
|||||||
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: "",
|
||||||
});
|
});
|
||||||
const [version, setVersion] = useState("unknown");
|
const [version, setVersion] = useState("unknown");
|
||||||
|
const [apiKeyFieldType, setApiKeyFieldType] = useState("password");
|
||||||
|
const [isApiKeySet, setIsApiKeySet] = useState(false);
|
||||||
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);
|
||||||
@@ -80,6 +85,28 @@ const Settings = () => {
|
|||||||
fetchLatestVersion();
|
fetchLatestVersion();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(getAppSettings());
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
@@ -91,12 +118,17 @@ const Settings = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let inputValue = value;
|
||||||
|
if (id === "ttl") {
|
||||||
|
inputValue = value.replace(/[^0-9]/g, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedForm = { ...form, [id]: inputValue };
|
||||||
const { error } = settingsValidation.validate(
|
const { error } = settingsValidation.validate(
|
||||||
{ [id]: value },
|
updatedForm,
|
||||||
{
|
{ abortEarly: false }
|
||||||
abortEarly: false,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!error || error.details.length === 0) {
|
if (!error || error.details.length === 0) {
|
||||||
setErrors({});
|
setErrors({});
|
||||||
} else {
|
} else {
|
||||||
@@ -107,12 +139,8 @@ const Settings = () => {
|
|||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
logger.error("Validation errors:", error.details);
|
logger.error("Validation errors:", error.details);
|
||||||
}
|
}
|
||||||
let inputValue = value;
|
|
||||||
id === "ttl" && (inputValue = value.replace(/[^0-9]/g, ""));
|
setForm(updatedForm);
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
[id]: inputValue,
|
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO Handle saving
|
// TODO Handle saving
|
||||||
@@ -125,7 +153,7 @@ 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 } })),
|
dispatch(updateAppSettings({ settings: { language: language, pagespeedApiKey: form.pagespeedApiKey } })),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (userAction.payload.success && settingsAction.payload.success) {
|
if (userAction.payload.success && settingsAction.payload.success) {
|
||||||
@@ -187,6 +215,14 @@ const Settings = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleResetApiKey = () => {
|
||||||
|
setIsApiKeySet(false);
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
pagespeedApiKey: "",
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
const languages = Object.keys(i18n.options.resources || {});
|
const languages = Object.keys(i18n.options.resources || {});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -277,6 +313,43 @@ const Settings = () => {
|
|||||||
</Box>
|
</Box>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
)} */}
|
)} */}
|
||||||
|
<ConfigBox>
|
||||||
|
<Box>
|
||||||
|
<Typography component="h1">{t("pageSpeedApiKeyFieldTitle")}</Typography>
|
||||||
|
<Typography sx={{ mt: theme.spacing(2), mb: theme.spacing(2) }}>
|
||||||
|
{t("pageSpeedApiKeyFieldDescription")}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Stack gap={theme.spacing(20)}>
|
||||||
|
<TextInput
|
||||||
|
id="pagespeedApiKey"
|
||||||
|
label={t("pageSpeedApiKeyFieldLabel")}
|
||||||
|
value={form.pagespeedApiKey}
|
||||||
|
type={apiKeyFieldType}
|
||||||
|
onChange={handleChange}
|
||||||
|
disabled={isApiKeySet}
|
||||||
|
optionalLabel="(Optional)"
|
||||||
|
error={!!errors.pagespeedApiKey}
|
||||||
|
helperText={errors.pagespeedApiKey}
|
||||||
|
endAdornment={
|
||||||
|
!isApiKeySet && (
|
||||||
|
<PasswordEndAdornment
|
||||||
|
fieldType={apiKeyFieldType}
|
||||||
|
setFieldType={setApiKeyFieldType}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{isApiKeySet && (
|
||||||
|
<Box>
|
||||||
|
<Typography>{t("pageSpeedApiKeyFieldResetLabel")}</Typography>
|
||||||
|
<Button onClick={handleResetApiKey} variant="contained" color="error" sx={{ mt: theme.spacing(4) }}>
|
||||||
|
{t("reset")}
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
</ConfigBox>
|
||||||
{/* {isAdmin && (
|
{/* {isAdmin && (
|
||||||
<ConfigBox>
|
<ConfigBox>
|
||||||
<Box>
|
<Box>
|
||||||
|
|||||||
@@ -257,7 +257,9 @@ const settingsValidation = joi.object({
|
|||||||
"number.base": "Please enter a valid number",
|
"number.base": "Please enter a valid number",
|
||||||
"any.required": "Please enter a value"
|
"any.required": "Please enter a value"
|
||||||
}),
|
}),
|
||||||
});
|
pagespeedApiKey: joi.string().allow("").optional(),
|
||||||
|
})
|
||||||
|
.unknown(true);
|
||||||
|
|
||||||
const dayjsValidator = (value, helpers) => {
|
const dayjsValidator = (value, helpers) => {
|
||||||
if (!dayjs(value).isValid()) {
|
if (!dayjs(value).isValid()) {
|
||||||
|
|||||||
@@ -430,6 +430,12 @@
|
|||||||
"noFileSelected": "No file selected",
|
"noFileSelected": "No file selected",
|
||||||
"fallbackPage": "Import a file to upload a list of servers in bulk"
|
"fallbackPage": "Import a file to upload a list of servers in bulk"
|
||||||
},
|
},
|
||||||
"publicLink": "Public link"
|
"publicLink": "Public link",
|
||||||
|
"maskedPageSpeedKeyPlaceholder": "*************************************",
|
||||||
|
"pageSpeedApiKeyFieldTitle": "Google PageSpeed API key",
|
||||||
|
"pageSpeedApiKeyFieldLabel": "PageSpeed API key",
|
||||||
|
"pageSpeedApiKeyFieldDescription": "Enter your Google PageSpeed API key to enable pagespeed monitoring. Click Reset to update the key.",
|
||||||
|
"pageSpeedApiKeyFieldResetLabel": "API key is set. Click Reset to change it.",
|
||||||
|
"reset": "Reset"
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user