diff --git a/client/src/Pages/Settings/index.tsx b/client/src/Pages/Settings/index.tsx index 979c013ff..d0ad5a6e0 100644 --- a/client/src/Pages/Settings/index.tsx +++ b/client/src/Pages/Settings/index.tsx @@ -7,13 +7,13 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useTranslation } from "react-i18next"; import { useSelector, useDispatch } from "react-redux"; import DummyChart from "@/Pages/Settings/DummyChart"; -import { useGet } from "@/Hooks/UseApi"; +import { useGet, usePatch } from "@/Hooks/UseApi"; import { useSettingsForm } from "@/Hooks/useSettingsForm"; import { useIsAdmin } from "@/Hooks/useIsAdmin.js"; import type { SettingsFormData } from "@/Validation/settings"; import { useState } from "react"; 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 { @@ -46,6 +46,8 @@ export const SettingsPage = () => { // Fetch settings data from API const { data: fetchedSettings } = useGet("/settings"); + // Form submission + const { patch, loading: isSaving } = usePatch(); // Local state for API key reset const [isApiKeySet, setIsApiKeySet] = useState( @@ -111,10 +113,49 @@ export const SettingsPage = () => { 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); + + 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 || {}); return ( - + { {isApiKeySet === true && apiKeyHasBeenReset === false && ( - - {t("pages.settings.pageSpeedSettings.labelApiKeySet")} - + + {t("pages.settings.form.pagespeed.option.apiKey.labelSet")} + - + {t("common.buttons.save")} + ); diff --git a/client/src/locales/en.json b/client/src/locales/en.json index 8bae5dcd0..75b4eafc3 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -944,6 +944,7 @@ "option": { "apiKey": { "label": "PageSpeed API key", + "labelSet": "API key is set. Click Reset to change it.", "placeholder": "Enter your Google PageSpeed API key" } }