mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
fixed changing log settings - https://github.com/emsesp/EMS-ESP32/pull/1162#issuecomment-1525877550
This commit is contained in:
@@ -2,6 +2,7 @@ import DownloadIcon from '@mui/icons-material/GetApp';
|
|||||||
import WarningIcon from '@mui/icons-material/Warning';
|
import WarningIcon from '@mui/icons-material/Warning';
|
||||||
import { Box, styled, Button, Checkbox, MenuItem, Grid, TextField } from '@mui/material';
|
import { Box, styled, Button, Checkbox, MenuItem, Grid, TextField } from '@mui/material';
|
||||||
import { useState, useEffect, useCallback, useLayoutEffect } from 'react';
|
import { useState, useEffect, useCallback, useLayoutEffect } from 'react';
|
||||||
|
import { toast } from 'react-toastify';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
|
|
||||||
import type { LogSettings, LogEntry, LogEntries } from 'types';
|
import type { LogSettings, LogEntry, LogEntries } from 'types';
|
||||||
@@ -9,7 +10,7 @@ import { addAccessTokenParameter } from 'api/authentication';
|
|||||||
import { EVENT_SOURCE_ROOT } from 'api/endpoints';
|
import { EVENT_SOURCE_ROOT } from 'api/endpoints';
|
||||||
import * as SystemApi from 'api/system';
|
import * as SystemApi from 'api/system';
|
||||||
|
|
||||||
import { SectionContent, FormLoader, BlockFormControlLabel, ValidatedTextField, ButtonRow } from 'components';
|
import { SectionContent, FormLoader, BlockFormControlLabel, BlockNavigation } from 'components';
|
||||||
|
|
||||||
import { useI18nContext } from 'i18n/i18n-react';
|
import { useI18nContext } from 'i18n/i18n-react';
|
||||||
import { LogLevel } from 'types';
|
import { LogLevel } from 'types';
|
||||||
@@ -65,9 +66,8 @@ const SystemLog: FC = () => {
|
|||||||
|
|
||||||
const { LL } = useI18nContext();
|
const { LL } = useI18nContext();
|
||||||
|
|
||||||
const { loadData, data, setData, saveData, origData, dirtyFlags, setDirtyFlags } = useRest<LogSettings>({
|
const { loadData, data, setData, origData, dirtyFlags, blocker, setDirtyFlags } = useRest<LogSettings>({
|
||||||
read: SystemApi.readLogSettings,
|
read: SystemApi.readLogSettings
|
||||||
update: SystemApi.updateLogSettings
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const [errorMessage, setErrorMessage] = useState<string>();
|
const [errorMessage, setErrorMessage] = useState<string>();
|
||||||
@@ -125,7 +125,7 @@ const SystemLog: FC = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void fetchLog();
|
void fetchLog();
|
||||||
}, [fetchLog]);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const es = new EventSource(addAccessTokenParameter(LOG_EVENTSOURCE_URL));
|
const es = new EventSource(addAccessTokenParameter(LOG_EVENTSOURCE_URL));
|
||||||
@@ -140,6 +140,27 @@ const SystemLog: FC = () => {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const saveSettings = async () => {
|
||||||
|
if (data) {
|
||||||
|
try {
|
||||||
|
const response = await SystemApi.updateLogSettings({
|
||||||
|
level: data.level,
|
||||||
|
max_messages: data.max_messages,
|
||||||
|
compact: data.compact
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status !== 200) {
|
||||||
|
toast.error(LL.PROBLEM_UPDATING());
|
||||||
|
} else {
|
||||||
|
setDirtyFlags([]);
|
||||||
|
toast.success(LL.UPDATED_OF(LL.SETTINGS_OF('')));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(extractErrorMessage(error, LL.PROBLEM_UPDATING()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const content = () => {
|
const content = () => {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return <FormLoader onRetry={loadData} errorMessage={errorMessage} />;
|
return <FormLoader onRetry={loadData} errorMessage={errorMessage} />;
|
||||||
@@ -191,16 +212,28 @@ const SystemLog: FC = () => {
|
|||||||
label={LL.COMPACT()}
|
label={LL.COMPACT()}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<ButtonRow>
|
<Box
|
||||||
|
sx={{
|
||||||
|
'& button, & a, & .MuiCard-root': {
|
||||||
|
mt: 3,
|
||||||
|
mx: 0.6
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Button startIcon={<DownloadIcon />} variant="outlined" color="secondary" onClick={onDownload}>
|
<Button startIcon={<DownloadIcon />} variant="outlined" color="secondary" onClick={onDownload}>
|
||||||
{LL.EXPORT()}
|
{LL.EXPORT()}
|
||||||
</Button>
|
</Button>
|
||||||
{dirtyFlags && dirtyFlags.length !== 0 && (
|
{dirtyFlags && dirtyFlags.length !== 0 && (
|
||||||
<Button startIcon={<WarningIcon color="warning" />} variant="contained" color="info" onClick={saveData}>
|
<Button
|
||||||
{LL.UPDATE()}
|
startIcon={<WarningIcon color="warning" />}
|
||||||
|
variant="contained"
|
||||||
|
color="info"
|
||||||
|
onClick={saveSettings}
|
||||||
|
>
|
||||||
|
{LL.APPLY_CHANGES(dirtyFlags.length)}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</ButtonRow>
|
</Box>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
@@ -232,6 +265,7 @@ const SystemLog: FC = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<SectionContent title={LL.LOG_OF(LL.SYSTEM(2))} titleGutter id="log-window">
|
<SectionContent title={LL.LOG_OF(LL.SYSTEM(2))} titleGutter id="log-window">
|
||||||
|
{blocker ? <BlockNavigation blocker={blocker} /> : null}
|
||||||
{content()}
|
{content()}
|
||||||
</SectionContent>
|
</SectionContent>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user