pagespeed
This commit is contained in:
1 parent
d8e7f9fc0f
commit
a59f7fd830
2 files changed
+74
-18
No files matched your search
@@ -7,13 +7,13 @@ import { zodResolver } from "@hookform/resolvers/zod";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import DummyChart from "@/Pages/Settings/DummyChart";
|
import DummyChart from "@/Pages/Settings/DummyChart";
|
||||||
import { useGet } from "@/Hooks/UseApi";
|
import { useGet, usePatch } from "@/Hooks/UseApi";
|
||||||
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormData } from "@/Validation/settings";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Controller } from "react-hook-form";
|
import { Controller } from "react-hook-form";
|
||||||
import { TextField, Button } from "@/Components/v2/inputs";
|
import { TextField, FieldLabel, Button } from "@/Components/v2/inputs";
|
||||||
import { Typography, Box } from "@mui/material";
|
import { Typography, Box } from "@mui/material";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -46,6 +46,8 @@ export const SettingsPage = () => {
|
|||||||
|
|
||||||
// Fetch settings data from API
|
// Fetch settings data from API
|
||||||
const { data: fetchedSettings } = useGet<SettingsResponse>("/settings");
|
const { data: fetchedSettings } = useGet<SettingsResponse>("/settings");
|
||||||
|
// Form submission
|
||||||
|
const { patch, loading: isSaving } = usePatch<SettingsFormData, SettingsResponse>();
|
||||||
|
|
||||||
// Local state for API key reset
|
// Local state for API key reset
|
||||||
const [isApiKeySet, setIsApiKeySet] = useState(
|
const [isApiKeySet, setIsApiKeySet] = useState(
|
||||||
@@ -111,10 +113,49 @@ export const SettingsPage = () => {
|
|||||||
setApiKeyHasBeenReset(true);
|
setApiKeyHasBeenReset(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onSubmit = async (data: SettingsFormData) => {
|
||||||
|
// Convert undefined to empty string for backend unsetting
|
||||||
|
const processedData = Object.entries(data).reduce((acc, [key, value]) => {
|
||||||
|
const typedKey = key as keyof SettingsFormData;
|
||||||
|
if (value === undefined) {
|
||||||
|
(acc as any)[typedKey] = "";
|
||||||
|
} else if (typeof value === "object" && value !== null) {
|
||||||
|
// Handle nested objects like globalThresholds
|
||||||
|
(acc as any)[typedKey] = Object.entries(value).reduce(
|
||||||
|
(nested, [nestedKey, nestedValue]) => ({
|
||||||
|
...nested,
|
||||||
|
[nestedKey]: nestedValue === undefined ? "" : nestedValue,
|
||||||
|
}),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
(acc as any)[typedKey] = value;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, {} as Partial<SettingsFormData>);
|
||||||
|
|
||||||
|
const result = await patch("/settings", processedData as SettingsFormData);
|
||||||
|
|
||||||
|
if (result?.success) {
|
||||||
|
// Update API key state from response
|
||||||
|
if (result.data) {
|
||||||
|
setIsApiKeySet(result.data.pagespeedKeySet);
|
||||||
|
setApiKeyHasBeenReset(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onError = (errors: unknown) => {
|
||||||
|
console.log("Form validation errors:", errors);
|
||||||
|
};
|
||||||
|
|
||||||
const languages = Object.keys(i18n.options.resources || {});
|
const languages = Object.keys(i18n.options.resources || {});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BasePage component="form">
|
<BasePage
|
||||||
|
component="form"
|
||||||
|
onSubmit={form.handleSubmit(onSubmit, onError)}
|
||||||
|
>
|
||||||
<Stack gap={theme.spacing(8)}>
|
<Stack gap={theme.spacing(8)}>
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.settings.form.timezone.title")}
|
title={t("pages.settings.form.timezone.title")}
|
||||||
@@ -211,9 +252,9 @@ export const SettingsPage = () => {
|
|||||||
|
|
||||||
{isApiKeySet === true && apiKeyHasBeenReset === false && (
|
{isApiKeySet === true && apiKeyHasBeenReset === false && (
|
||||||
<Box>
|
<Box>
|
||||||
<Typography sx={{ mb: theme.spacing(4) }}>
|
<FieldLabel>
|
||||||
{t("pages.settings.pageSpeedSettings.labelApiKeySet")}
|
{t("pages.settings.form.pagespeed.option.apiKey.labelSet")}
|
||||||
</Typography>
|
</FieldLabel>
|
||||||
<Button
|
<Button
|
||||||
onClick={handleResetApiKey}
|
onClick={handleResetApiKey}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
@@ -227,19 +268,33 @@ export const SettingsPage = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<Stack
|
</Stack>
|
||||||
direction="row"
|
|
||||||
justifyContent="flex-end"
|
{/* Sticky Save Button */}
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
justifyContent="flex-end"
|
||||||
|
sx={{
|
||||||
|
position: "sticky",
|
||||||
|
bottom: 0,
|
||||||
|
backgroundColor: theme.palette.background.paper,
|
||||||
|
borderTop: `1px solid ${theme.palette.divider}`,
|
||||||
|
padding: theme.spacing(8),
|
||||||
|
marginLeft: theme.spacing(-8),
|
||||||
|
marginRight: theme.spacing(-8),
|
||||||
|
marginBottom: theme.spacing(-8),
|
||||||
|
zIndex: 1000,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
loading={isSaving}
|
||||||
|
type="submit"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
disabled={!form.formState.isValid}
|
||||||
>
|
>
|
||||||
<Button
|
{t("common.buttons.save")}
|
||||||
loading={false}
|
</Button>
|
||||||
type="submit"
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
>
|
|
||||||
{t("common.buttons.save")}
|
|
||||||
</Button>
|
|
||||||
</Stack>
|
|
||||||
</Stack>
|
</Stack>
|
||||||
</BasePage>
|
</BasePage>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -944,6 +944,7 @@
|
|||||||
"option": {
|
"option": {
|
||||||
"apiKey": {
|
"apiKey": {
|
||||||
"label": "PageSpeed API key",
|
"label": "PageSpeed API key",
|
||||||
|
"labelSet": "API key is set. Click Reset to change it.",
|
||||||
"placeholder": "Enter your Google PageSpeed API key"
|
"placeholder": "Enter your Google PageSpeed API key"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user