1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Move submission to action

This commit is contained in:
viown 2024-12-17 20:31:03 +03:00
parent 2e4848ade9
commit 0e54b11c61
3 changed files with 39 additions and 26 deletions

View file

@ -1,4 +1,4 @@
import React, { ChangeEvent, FormEvent, useCallback, useEffect, useState } from 'react'; import React, { ChangeEvent, useCallback, useEffect, useState } from 'react';
import type { LogFile } from '@jellyfin/sdk/lib/generated-client/models/log-file'; import type { LogFile } from '@jellyfin/sdk/lib/generated-client/models/log-file';
import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api'; import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api';
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api'; import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api';
@ -8,16 +8,44 @@ import Page from 'components/Page';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import globalize from 'lib/globalize'; import globalize from 'lib/globalize';
import { Alert, Box, Button, FormControlLabel, Stack, Switch, TextField, Typography } from '@mui/material'; import { Alert, Box, Button, FormControlLabel, Stack, Switch, TextField, Typography } from '@mui/material';
import { Form } from 'react-router-dom'; import { type ActionFunctionArgs, type LoaderFunctionArgs, Form, useActionData } from 'react-router-dom';
import ServerConnections from 'components/ServerConnections';
const Logs = () => { interface ActionData {
isSaved: boolean;
}
export const action = async ({ request }: ActionFunctionArgs) => {
const api = ServerConnections.getCurrentApi();
if (!api) throw new Error('No Api instance available');
const formData = await request.formData();
const { data: config } = await getConfigurationApi(api).getConfiguration();
config.EnableSlowResponseWarning = formData.get('EnableWarningMessage') === 'on';
const responseTime = formData.get('SlowResponseTime');
if (responseTime) {
config.SlowResponseThresholdMs = parseInt(responseTime.toString(), 10);
}
await getConfigurationApi(api)
.updateConfiguration({ serverConfiguration: config });
return {
isSaved: true
};
};
export const Logs = () => {
const actionData = useActionData() as ActionData | undefined;
const { api } = useApi(); const { api } = useApi();
const [ logs, setLogs ] = useState<LogFile[]>([]); const [ logs, setLogs ] = useState<LogFile[]>([]);
const [ logsLoading, setLogsLoading ] = useState<boolean>(true); const [ logsLoading, setLogsLoading ] = useState<boolean>(true);
const [ configLoading, setConfigLoading ] = useState<boolean>(true); const [ configLoading, setConfigLoading ] = useState<boolean>(true);
const [ logWarningMessageChecked, setLogWarningMessageChecked ] = useState<boolean>(false); const [ logWarningMessageChecked, setLogWarningMessageChecked ] = useState<boolean>(false);
const [ slowResponseTime, setSlowResponseTime ] = useState<string>(''); const [ slowResponseTime, setSlowResponseTime ] = useState<string>('');
const [ submitted, setSubmitted ] = useState<boolean>(false); const [ isSubmitting, setIsSubmitting ] = useState(false);
const setLogWarningMessage = useCallback((_: ChangeEvent<HTMLInputElement>, checked: boolean) => { const setLogWarningMessage = useCallback((_: ChangeEvent<HTMLInputElement>, checked: boolean) => {
setLogWarningMessageChecked(checked); setLogWarningMessageChecked(checked);
@ -37,26 +65,9 @@ const Logs = () => {
}); });
}, [api]); }, [api]);
const onSubmit = useCallback((e: FormEvent<HTMLFormElement>) => { const onSubmit = useCallback(() => {
e.preventDefault(); setIsSubmitting(true);
if (!api) return; }, []);
getConfigurationApi(api)
.getConfiguration()
.then(({ data: config }) => {
config.EnableSlowResponseWarning = logWarningMessageChecked;
config.SlowResponseThresholdMs = parseInt(slowResponseTime, 10);
getConfigurationApi(api)
.updateConfiguration({ serverConfiguration: config })
.then(() => setSubmitted(true))
.catch(err => {
console.error('[logs] failed to update configuration data', err);
});
})
.catch(err => {
console.error('[logs] failed to get configuration data', err);
});
}, [api, logWarningMessageChecked, slowResponseTime]);
useEffect(() => { useEffect(() => {
if (!api) return; if (!api) return;
@ -94,13 +105,13 @@ const Logs = () => {
className='mainAnimatedPage type-interior' className='mainAnimatedPage type-interior'
> >
<Box className='content-primary'> <Box className='content-primary'>
<Form className='logsForm' method='POST' onSubmit={onSubmit}> <Form method='POST' onSubmit={onSubmit}>
<Stack spacing={3}> <Stack spacing={3}>
<Typography variant='h1'> <Typography variant='h1'>
{globalize.translate('TabLogs')} {globalize.translate('TabLogs')}
</Typography> </Typography>
{submitted && ( {isSubmitting && actionData?.isSaved && (
<Alert severity='success'> <Alert severity='success'>
{globalize.translate('SettingsSaved')} {globalize.translate('SettingsSaved')}
</Alert> </Alert>
@ -111,6 +122,7 @@ const Logs = () => {
<Switch <Switch
checked={logWarningMessageChecked} checked={logWarningMessageChecked}
onChange={setLogWarningMessage} onChange={setLogWarningMessage}
name={'EnableWarningMessage'}
/> />
} }
label={globalize.translate('LabelSlowResponseEnabled')} label={globalize.translate('LabelSlowResponseEnabled')}
@ -119,6 +131,7 @@ const Logs = () => {
<TextField <TextField
fullWidth fullWidth
type='number' type='number'
name={'SlowResponseTime'}
label={globalize.translate('LabelSlowResponseTime')} label={globalize.translate('LabelSlowResponseTime')}
value={slowResponseTime} value={slowResponseTime}
onChange={onResponseTimeChange} onChange={onResponseTimeChange}