From f426a77f2581b2919238d6a3cee43b23bac5076e Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 23 Oct 2024 02:50:26 +0200 Subject: [PATCH] Simplified frontend setting creation. Added ability to reset UserAgent --- Website/App.tsx | 4 +- Website/modules/Settings.tsx | 44 +++++++++++++++---- .../modules/interfaces/IFrontendSettings.tsx | 29 ++++-------- Website/styles/settings.css | 7 ++- 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/Website/App.tsx b/Website/App.tsx index ac54501..1788095 100644 --- a/Website/App.tsx +++ b/Website/App.tsx @@ -4,14 +4,14 @@ import Search from "./modules/Search"; import Header from "./modules/Header"; import MonitorJobsList from "./modules/MonitorJobsList"; import './styles/index.css' -import IFrontendSettings, {FrontendSettingsWith} from "./modules/interfaces/IFrontendSettings"; +import IFrontendSettings, {LoadFrontendSettings} from "./modules/interfaces/IFrontendSettings"; import {useCookies} from "react-cookie"; export default function App(){ const [, setCookie] = useCookies(['apiUri', 'jobInterval']); const [connected, setConnected] = React.useState(false); const [showSearch, setShowSearch] = React.useState(false); - const [frontendSettings, setFrontendSettings] = useState(FrontendSettingsWith(undefined, undefined, undefined)); + const [frontendSettings, setFrontendSettings] = useState(LoadFrontendSettings()); const [updateInterval, setUpdateInterval] = React.useState(); const [updateMonitorList, setUpdateMonitorList] = React.useState(new Date()); diff --git a/Website/modules/Settings.tsx b/Website/modules/Settings.tsx index 0eb3f5b..a3ea4a0 100644 --- a/Website/modules/Settings.tsx +++ b/Website/modules/Settings.tsx @@ -1,8 +1,8 @@ -import React, {KeyboardEventHandler, useEffect, useState} from 'react'; -import IFrontendSettings, {FrontendSettingsWith} from "./interfaces/IFrontendSettings"; +import React, {KeyboardEventHandler, MouseEventHandler, useEffect, useState} from 'react'; +import IFrontendSettings from "./interfaces/IFrontendSettings"; import '../styles/settings.css'; import IBackendSettings from "./interfaces/IBackendSettings"; -import {getData} from "../App"; +import {getData, postData} from "../App"; import LibraryConnector, {Kavita, Komga} from "./LibraryConnector"; import NotificationConnector, {Gotify, Lunasea, Ntfy} from "./NotificationConnector"; import ILibraryConnector from "./interfaces/ILibraryConnector"; @@ -24,7 +24,7 @@ export default function Settings({backendConnected, apiUri, settings, changeSett console.debug(`${showSettings ? "Showing" : "Not showing"} settings.`); if(!showSettings || !backendConnected) return; - GetSettings(apiUri).then(setBackendSettings).catch(console.error); + UpdateBackendSettings(); LibraryConnector.GetLibraryConnectors(apiUri).then(setLibraryConnectors).catch(console.error); NotificationConnector.GetNotificationConnectors(apiUri).then(setNotificationConnectors).catch(console.error); }, [showSettings]); @@ -45,6 +45,10 @@ export default function Settings({backendConnected, apiUri, settings, changeSett .catch(Promise.reject); } + function UpdateBackendSettings() { + GetSettings(apiUri).then(setBackendSettings).catch(console.error); + } + const GetKomga = () : ILibraryConnector | undefined => libraryConnectors?.find(con => con.libraryType == 0); @@ -73,13 +77,36 @@ export default function Settings({backendConnected, apiUri, settings, changeSett const SubmitApiUri : KeyboardEventHandler = (e) => { if(e.currentTarget.value.length < 1) return; - const newSettings = FrontendSettingsWith(frontendSettings, undefined, e.currentTarget.value); if(e.key == "Enter"){ - setFrontendSettings(newSettings); + setFrontendSettings({...frontendSettings, apiUri: e.currentTarget.value}); RefreshInputs(); } } -1 + + const SubmitUserAgent: KeyboardEventHandler = (e) => { + if(e.currentTarget.value.length < 1 || backendSettings === undefined) + return; + if(e.key == "Enter"){ + //console.info(`Updating Useragent ${e.currentTarget.value}`); + postData(`${apiUri}/v2/Settings/UserAgent`, {value: e.currentTarget.value}) + .then((json) => { + //console.info(`Successfully updated Useragent ${e.currentTarget.value}`); + UpdateBackendSettings(); + RefreshInputs(); + }) + .catch(() => alert("Failed to update Useragent.")); + } + } + const ResetUserAgent: MouseEventHandler = () => { + postData(`${apiUri}/v2/Settings/UserAgent`, {value: undefined}) + .then((json) => { + //console.info(`Successfully updated Useragent ${e.currentTarget.value}`); + UpdateBackendSettings(); + RefreshInputs(); + }) + .catch(() => alert("Failed to update Useragent.")); + } + function RefreshInputs(){ alert("Saved."); setShowSettings(false); @@ -103,7 +130,8 @@ export default function Settings({backendConnected, apiUri, settings, changeSett - + + Reset
Rate Limits diff --git a/Website/modules/interfaces/IFrontendSettings.tsx b/Website/modules/interfaces/IFrontendSettings.tsx index 1ac2b0e..1b56ab4 100644 --- a/Website/modules/interfaces/IFrontendSettings.tsx +++ b/Website/modules/interfaces/IFrontendSettings.tsx @@ -5,27 +5,14 @@ export default interface IFrontendSettings { apiUri: string; } -export function FrontendSettingsWith(settings: IFrontendSettings | undefined, jobInterval: Date | undefined, apiUri: string | undefined): IFrontendSettings { +export function LoadFrontendSettings(): IFrontendSettings { const cookies = new Cookies(); - let transform : IFrontendSettings; - if(settings === undefined) { - transform = { - apiUri: apiUri === undefined - ? cookies.get('apiUri') === undefined - ? `${window.location.protocol}//${window.location.host}/api` - : cookies.get('apiUri') - : apiUri, - jobInterval: jobInterval === undefined - ? cookies.get('jobInterval') === undefined - ? new Date(0,0,0,3) - : cookies.get('jobInterval') - : jobInterval - } - }else { - transform = { - apiUri: apiUri === undefined ? settings.apiUri : apiUri, - jobInterval: jobInterval === undefined ? settings.jobInterval : jobInterval, - } + return { + jobInterval: cookies.get('jobInterval') === undefined + ? new Date(0,0,0,3) + : cookies.get('jobInterval'), + apiUri: cookies.get('apiUri') === undefined + ? `${window.location.protocol}//${window.location.host}/api` + : cookies.get('apiUri') } - return transform; } \ No newline at end of file diff --git a/Website/styles/settings.css b/Website/styles/settings.css index ef9abbc..df781c7 100644 --- a/Website/styles/settings.css +++ b/Website/styles/settings.css @@ -83,10 +83,15 @@ width: calc(100% - 20px); } -.section-actions > span { +.section-actions > span, #resetUserAgent { border: 1px solid lightgray; padding: 3px 5px 2px; width: 10ch; text-align: center; border-radius: 3px; +} + +#resetUserAgent{ + align-self: flex-end; + margin-top: 3px; } \ No newline at end of file