move restarting to parent component - #1920

This commit is contained in:
proddy
2024-08-16 13:48:28 +02:00
parent d3a7ab8fc1
commit 5404537da8
3 changed files with 53 additions and 52 deletions

View File

@@ -1,6 +1,8 @@
import { useState } from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import DownloadIcon from '@mui/icons-material/GetApp'; import DownloadIcon from '@mui/icons-material/GetApp';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import { Box, Button, Divider, Link, Typography } from '@mui/material'; import { Box, Button, Divider, Link, Typography } from '@mui/material';
import * as SystemApi from 'api/system'; import * as SystemApi from 'api/system';
@@ -15,8 +17,10 @@ import { getDevVersion, getStableVersion } from 'api/system';
import { useRequest } from 'alova/client'; import { useRequest } from 'alova/client';
import type { APIcall } from 'app/main/types'; import type { APIcall } from 'app/main/types';
import RestartMonitor from 'app/status/RestartMonitor';
import { import {
FormLoader, FormLoader,
MessageBox,
SectionContent, SectionContent,
SingleUpload, SingleUpload,
useLayoutTitle useLayoutTitle
@@ -26,6 +30,9 @@ import { useI18nContext } from 'i18n/i18n-react';
const DownloadUpload = () => { const DownloadUpload = () => {
const { LL } = useI18nContext(); const { LL } = useI18nContext();
const [restarting, setRestarting] = useState<boolean>(false);
const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
const { send: sendSettings } = useRequest(getSettings(), { const { send: sendSettings } = useRequest(getSettings(), {
immediate: false immediate: false
}).onSuccess((event) => { }).onSuccess((event) => {
@@ -41,7 +48,7 @@ const DownloadUpload = () => {
const { send: sendEntities } = useRequest(getEntities(), { const { send: sendEntities } = useRequest(getEntities(), {
immediate: false immediate: false
}).onSuccess((event) => { }).onSuccess((event) => {
saveFile(event.data, 'entities.json'); saveFile(event.data, 'custom_entities.json');
}); });
const { send: sendSchedule } = useRequest(getSchedule(), { const { send: sendSchedule } = useRequest(getSchedule(), {
@@ -65,6 +72,20 @@ const DownloadUpload = () => {
error error
} = useRequest(SystemApi.readHardwareStatus); } = useRequest(SystemApi.readHardwareStatus);
const { send: restartCommand } = useRequest(SystemApi.restart(), {
immediate: false
});
const restart = async () => {
await restartCommand()
.then(() => {
setRestarting(true);
})
.catch((error: Error) => {
toast.error(error.message);
});
};
// called immediately to get the latest version, on page load // called immediately to get the latest version, on page load
// set immediate to false to avoid calling the API on page load and GH blocking while testing! // set immediate to false to avoid calling the API on page load and GH blocking while testing!
const { data: latestVersion } = useRequest(getStableVersion, { const { data: latestVersion } = useRequest(getStableVersion, {
@@ -100,14 +121,14 @@ const DownloadUpload = () => {
return data.esp_platform; return data.esp_platform;
}; };
const saveFile = (json: unknown, endpoint: string) => { const saveFile = (json: unknown, filename: string) => {
const anchor = document.createElement('a'); const anchor = document.createElement('a');
anchor.href = URL.createObjectURL( anchor.href = URL.createObjectURL(
new Blob([JSON.stringify(json, null, 2)], { new Blob([JSON.stringify(json, null, 2)], {
type: 'text/plain' type: 'text/plain'
}) })
); );
anchor.download = 'emsesp_' + endpoint; anchor.download = 'emsesp_' + filename;
anchor.click(); anchor.click();
URL.revokeObjectURL(anchor.href); URL.revokeObjectURL(anchor.href);
toast.info(LL.DOWNLOAD_SUCCESSFUL()); toast.info(LL.DOWNLOAD_SUCCESSFUL());
@@ -284,12 +305,27 @@ const DownloadUpload = () => {
)} )}
</Box> </Box>
<SingleUpload /> <SingleUpload setRestartNeeded={setRestartNeeded} />
{restartNeeded && (
<MessageBox mt={2} level="warning" message={LL.RESTART_TEXT(0)}>
<Button
startIcon={<PowerSettingsNewIcon />}
variant="contained"
color="error"
onClick={restart}
>
{LL.RESTART()}
</Button>
</MessageBox>
)}
</> </>
); );
}; };
return <SectionContent>{content()}</SectionContent>; return (
<SectionContent>{restarting ? <RestartMonitor /> : content()}</SectionContent>
);
}; };
export default DownloadUpload; export default DownloadUpload;

View File

@@ -1,5 +1,4 @@
// Code inspired by https://medium.com/@dprincecoder/creating-a-drag-and-drop-file-upload-component-in-react-a-step-by-step-guide-4d93b6cc21e0 // Code inspired by Prince Azubuike from https://medium.com/@dprincecoder/creating-a-drag-and-drop-file-upload-component-in-react-a-step-by-step-guide-4d93b6cc21e0
// (c) Prince Azubuike
import { type ChangeEvent, useRef, useState } from 'react'; import { type ChangeEvent, useRef, useState } from 'react';
import CancelIcon from '@mui/icons-material/Cancel'; import CancelIcon from '@mui/icons-material/Cancel';

View File

@@ -2,23 +2,17 @@ import { useEffect, useState } from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import CancelIcon from '@mui/icons-material/Cancel'; import CancelIcon from '@mui/icons-material/Cancel';
import PowerSettingsNewIcon from '@mui/icons-material/PowerSettingsNew';
import { Box, Button, LinearProgress, Typography } from '@mui/material'; import { Box, Button, LinearProgress, Typography } from '@mui/material';
import * as SystemApi from 'api/system'; import * as SystemApi from 'api/system';
import { useRequest } from 'alova/client'; import { useRequest } from 'alova/client';
import RestartMonitor from 'app/status/RestartMonitor';
import MessageBox from 'components/MessageBox';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
import DragNdrop from './DragNdrop'; import DragNdrop from './DragNdrop';
const SingleUpload = () => { const SingleUpload = ({ setRestartNeeded }) => {
const [md5, setMd5] = useState<string>(); const [md5, setMd5] = useState<string>();
const [restarting, setRestarting] = useState<boolean>(false);
const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
const [file, setFile] = useState<File>(); const [file, setFile] = useState<File>();
const { LL } = useI18nContext(); const { LL } = useI18nContext();
@@ -39,17 +33,6 @@ const SingleUpload = () => {
} }
}); });
const { send: restartCommand } = useRequest(SystemApi.restart(), {
immediate: false
});
const restart = async () => {
await restartCommand().catch((error: Error) => {
toast.error(error.message);
});
setRestarting(true);
};
useEffect(async () => { useEffect(async () => {
if (file) { if (file) {
console.log('going to upload file ', file.name); console.log('going to upload file ', file.name);
@@ -75,7 +58,7 @@ const SingleUpload = () => {
<Typography variant="body2">{LL.UPLOAD_TEXT()}</Typography> <Typography variant="body2">{LL.UPLOAD_TEXT()}</Typography>
</Box> </Box>
{isUploading || restartNeeded ? ( {isUploading ? (
<> <>
<Box width="100%" p={2}> <Box width="100%" p={2}>
<LinearProgress <LinearProgress
@@ -90,17 +73,15 @@ const SingleUpload = () => {
/> />
</Box> </Box>
{!restartNeeded && ( <Button
<Button sx={{ ml: 2 }}
sx={{ ml: 2 }} startIcon={<CancelIcon />}
startIcon={<CancelIcon />} variant="outlined"
variant="outlined" color="error"
color="error" onClick={cancelUpload}
onClick={cancelUpload} >
> {LL.CANCEL()}
{LL.CANCEL()} </Button>
</Button>
)}
</> </>
) : ( ) : (
<DragNdrop onFileSelected={setFile} /> <DragNdrop onFileSelected={setFile} />
@@ -111,21 +92,6 @@ const SingleUpload = () => {
<Typography variant="body2">{'MD5: ' + md5}</Typography> <Typography variant="body2">{'MD5: ' + md5}</Typography>
</Box> </Box>
)} )}
{restartNeeded && (
<MessageBox mt={2} level="warning" message={LL.RESTART_TEXT(0)}>
<Button
startIcon={<PowerSettingsNewIcon />}
variant="contained"
color="error"
onClick={restart}
>
{LL.RESTART()}
</Button>
</MessageBox>
)}
{restarting && <RestartMonitor />}
</> </>
); );
}; };