Simplified frontend setting creation.

Added ability to reset UserAgent
This commit is contained in:
glax 2024-10-23 02:50:26 +02:00
parent 24417ae180
commit f426a77f25
4 changed files with 52 additions and 32 deletions

View File

@ -4,14 +4,14 @@ import Search from "./modules/Search";
import Header from "./modules/Header"; import Header from "./modules/Header";
import MonitorJobsList from "./modules/MonitorJobsList"; import MonitorJobsList from "./modules/MonitorJobsList";
import './styles/index.css' import './styles/index.css'
import IFrontendSettings, {FrontendSettingsWith} from "./modules/interfaces/IFrontendSettings"; import IFrontendSettings, {LoadFrontendSettings} from "./modules/interfaces/IFrontendSettings";
import {useCookies} from "react-cookie"; import {useCookies} from "react-cookie";
export default function App(){ export default function App(){
const [, setCookie] = useCookies(['apiUri', 'jobInterval']); const [, setCookie] = useCookies(['apiUri', 'jobInterval']);
const [connected, setConnected] = React.useState(false); const [connected, setConnected] = React.useState(false);
const [showSearch, setShowSearch] = React.useState(false); const [showSearch, setShowSearch] = React.useState(false);
const [frontendSettings, setFrontendSettings] = useState<IFrontendSettings>(FrontendSettingsWith(undefined, undefined, undefined)); const [frontendSettings, setFrontendSettings] = useState<IFrontendSettings>(LoadFrontendSettings());
const [updateInterval, setUpdateInterval] = React.useState<number>(); const [updateInterval, setUpdateInterval] = React.useState<number>();
const [updateMonitorList, setUpdateMonitorList] = React.useState<Date>(new Date()); const [updateMonitorList, setUpdateMonitorList] = React.useState<Date>(new Date());

View File

@ -1,8 +1,8 @@
import React, {KeyboardEventHandler, useEffect, useState} from 'react'; import React, {KeyboardEventHandler, MouseEventHandler, useEffect, useState} from 'react';
import IFrontendSettings, {FrontendSettingsWith} from "./interfaces/IFrontendSettings"; import IFrontendSettings from "./interfaces/IFrontendSettings";
import '../styles/settings.css'; import '../styles/settings.css';
import IBackendSettings from "./interfaces/IBackendSettings"; import IBackendSettings from "./interfaces/IBackendSettings";
import {getData} from "../App"; import {getData, postData} from "../App";
import LibraryConnector, {Kavita, Komga} from "./LibraryConnector"; import LibraryConnector, {Kavita, Komga} from "./LibraryConnector";
import NotificationConnector, {Gotify, Lunasea, Ntfy} from "./NotificationConnector"; import NotificationConnector, {Gotify, Lunasea, Ntfy} from "./NotificationConnector";
import ILibraryConnector from "./interfaces/ILibraryConnector"; import ILibraryConnector from "./interfaces/ILibraryConnector";
@ -24,7 +24,7 @@ export default function Settings({backendConnected, apiUri, settings, changeSett
console.debug(`${showSettings ? "Showing" : "Not showing"} settings.`); console.debug(`${showSettings ? "Showing" : "Not showing"} settings.`);
if(!showSettings || !backendConnected) if(!showSettings || !backendConnected)
return; return;
GetSettings(apiUri).then(setBackendSettings).catch(console.error); UpdateBackendSettings();
LibraryConnector.GetLibraryConnectors(apiUri).then(setLibraryConnectors).catch(console.error); LibraryConnector.GetLibraryConnectors(apiUri).then(setLibraryConnectors).catch(console.error);
NotificationConnector.GetNotificationConnectors(apiUri).then(setNotificationConnectors).catch(console.error); NotificationConnector.GetNotificationConnectors(apiUri).then(setNotificationConnectors).catch(console.error);
}, [showSettings]); }, [showSettings]);
@ -45,6 +45,10 @@ export default function Settings({backendConnected, apiUri, settings, changeSett
.catch(Promise.reject); .catch(Promise.reject);
} }
function UpdateBackendSettings() {
GetSettings(apiUri).then(setBackendSettings).catch(console.error);
}
const GetKomga = () : ILibraryConnector | undefined => const GetKomga = () : ILibraryConnector | undefined =>
libraryConnectors?.find(con => con.libraryType == 0); libraryConnectors?.find(con => con.libraryType == 0);
@ -73,13 +77,36 @@ export default function Settings({backendConnected, apiUri, settings, changeSett
const SubmitApiUri : KeyboardEventHandler<HTMLInputElement> = (e) => { const SubmitApiUri : KeyboardEventHandler<HTMLInputElement> = (e) => {
if(e.currentTarget.value.length < 1) if(e.currentTarget.value.length < 1)
return; return;
const newSettings = FrontendSettingsWith(frontendSettings, undefined, e.currentTarget.value);
if(e.key == "Enter"){ if(e.key == "Enter"){
setFrontendSettings(newSettings); setFrontendSettings({...frontendSettings, apiUri: e.currentTarget.value});
RefreshInputs(); RefreshInputs();
} }
} }
1
const SubmitUserAgent: KeyboardEventHandler<HTMLInputElement> = (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<HTMLSpanElement> = () => {
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(){ function RefreshInputs(){
alert("Saved."); alert("Saved.");
setShowSettings(false); setShowSettings(false);
@ -103,7 +130,8 @@ export default function Settings({backendConnected, apiUri, settings, changeSett
<label htmlFor="settingApiUri">API URI:</label> <label htmlFor="settingApiUri">API URI:</label>
<input placeholder={frontendSettings.apiUri} type="text" id="settingApiUri" onKeyDown={SubmitApiUri} /> <input placeholder={frontendSettings.apiUri} type="text" id="settingApiUri" onKeyDown={SubmitApiUri} />
<label htmlFor="userAgent">User Agent:</label> <label htmlFor="userAgent">User Agent:</label>
<input id="userAgent" type="text" placeholder={backendSettings != undefined ? backendSettings.userAgent : "UserAgent"} /> <input id="userAgent" type="text" placeholder={backendSettings != undefined ? backendSettings.userAgent : "UserAgent"} onKeyDown={SubmitUserAgent} />
<span id="resetUserAgent" onClick={ResetUserAgent}>Reset</span>
</div> </div>
<div className="section-item"> <div className="section-item">
<span className="settings-section-title">Rate Limits</span> <span className="settings-section-title">Rate Limits</span>

View File

@ -5,27 +5,14 @@ export default interface IFrontendSettings {
apiUri: string; apiUri: string;
} }
export function FrontendSettingsWith(settings: IFrontendSettings | undefined, jobInterval: Date | undefined, apiUri: string | undefined): IFrontendSettings { export function LoadFrontendSettings(): IFrontendSettings {
const cookies = new Cookies(); const cookies = new Cookies();
let transform : IFrontendSettings; return {
if(settings === undefined) { jobInterval: cookies.get('jobInterval') === undefined
transform = { ? new Date(0,0,0,3)
apiUri: apiUri === undefined : cookies.get('jobInterval'),
? cookies.get('apiUri') === undefined apiUri: cookies.get('apiUri') === undefined
? `${window.location.protocol}//${window.location.host}/api` ? `${window.location.protocol}//${window.location.host}/api`
: cookies.get('apiUri') : 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 transform;
} }

View File

@ -83,10 +83,15 @@
width: calc(100% - 20px); width: calc(100% - 20px);
} }
.section-actions > span { .section-actions > span, #resetUserAgent {
border: 1px solid lightgray; border: 1px solid lightgray;
padding: 3px 5px 2px; padding: 3px 5px 2px;
width: 10ch; width: 10ch;
text-align: center; text-align: center;
border-radius: 3px; border-radius: 3px;
} }
#resetUserAgent{
align-self: flex-end;
margin-top: 3px;
}