mirror of
https://github.com/C9Glax/tranga-website.git
synced 2025-05-22 14:13:02 +02:00
Fix UserAgent Setting
This commit is contained in:
parent
4e648bd04e
commit
c00c80503c
@ -7,17 +7,18 @@ import {
|
|||||||
ColorPaletteProp,
|
ColorPaletteProp,
|
||||||
Input
|
Input
|
||||||
} from "@mui/joy";
|
} from "@mui/joy";
|
||||||
import {KeyboardEventHandler, useCallback, useContext, useState} from "react";
|
import {KeyboardEventHandler, useCallback, useContext, useEffect, useState} from "react";
|
||||||
import {ApiUriContext} from "../../api/fetchApi.tsx";
|
import {ApiUriContext} from "../../api/fetchApi.tsx";
|
||||||
import {ResetUserAgent, UpdateUserAgent} from "../../api/BackendSettings.tsx";
|
import {GetUserAgent, ResetUserAgent, UpdateUserAgent} from "../../api/BackendSettings.tsx";
|
||||||
|
|
||||||
export default function UserAgent({backendSettings}: {backendSettings?: IBackendSettings}) {
|
export default function UserAgent({backendSettings}: {backendSettings?: IBackendSettings}) {
|
||||||
const apiUri = useContext(ApiUriContext);
|
const apiUri = useContext(ApiUriContext);
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [value, setValue] = useState<string>("");
|
const [value, setValue] = useState<string|undefined>(backendSettings?.userAgent);
|
||||||
const [color, setColor] = useState<ColorPaletteProp>("neutral");
|
const [color, setColor] = useState<ColorPaletteProp>("neutral");
|
||||||
|
|
||||||
const keyDown : KeyboardEventHandler<HTMLInputElement> = useCallback((e) => {
|
const keyDown : KeyboardEventHandler<HTMLInputElement> = useCallback((e) => {
|
||||||
|
if(value === undefined) return;
|
||||||
if(e.key === "Enter") {
|
if(e.key === "Enter") {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
UpdateUserAgent(apiUri, value)
|
UpdateUserAgent(apiUri, value)
|
||||||
@ -25,26 +26,32 @@ export default function UserAgent({backendSettings}: {backendSettings?: IBackend
|
|||||||
.catch(() => setColor("danger"))
|
.catch(() => setColor("danger"))
|
||||||
.finally(() => setLoading(false));
|
.finally(() => setLoading(false));
|
||||||
}
|
}
|
||||||
}, [apiUri])
|
}, [apiUri, value])
|
||||||
|
|
||||||
const Reset = useCallback(() => {
|
const Reset = useCallback(() => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
ResetUserAgent(apiUri)
|
ResetUserAgent(apiUri)
|
||||||
|
.then(() => GetUserAgent(apiUri))
|
||||||
|
.then((val) => setValue(val))
|
||||||
.then(() => setColor("success"))
|
.then(() => setColor("success"))
|
||||||
.catch(() => setColor("danger"))
|
.catch(() => setColor("danger"))
|
||||||
.finally(() => setLoading(false));
|
.finally(() => setLoading(false));
|
||||||
}, [apiUri]);
|
}, [apiUri]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setValue(backendSettings?.userAgent);
|
||||||
|
}, [backendSettings]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Accordion>
|
<Accordion>
|
||||||
<AccordionSummary>UserAgent</AccordionSummary>
|
<AccordionSummary>UserAgent</AccordionSummary>
|
||||||
<AccordionDetails>
|
<AccordionDetails>
|
||||||
<Input disabled={backendSettings === undefined || loading}
|
<Input disabled={backendSettings === undefined || loading}
|
||||||
placeholder={"UserAgent"}
|
placeholder={"UserAgent"}
|
||||||
defaultValue={backendSettings?.userAgent}
|
value={value}
|
||||||
onKeyDown={keyDown}
|
onKeyDown={keyDown}
|
||||||
onChange={e => setValue(e.target.value)}
|
onChange={e => setValue(e.target.value)}
|
||||||
color={color}
|
color={color}
|
||||||
endDecorator={<Button onClick={Reset} loading={loading}>Reset</Button>}
|
endDecorator={<Button onClick={Reset} loading={loading}>Reset</Button>}
|
||||||
/>
|
/>
|
||||||
</AccordionDetails>
|
</AccordionDetails>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user