progress bar

main
alex 2024-08-10 22:28:53 +02:00
parent 413425adca
commit dd50db3186
1 changed files with 87 additions and 58 deletions

View File

@ -8,7 +8,6 @@ import {
Form, Form,
Grid, Grid,
InputNumber, InputNumber,
Progress,
Row, Row,
Select, Select,
Slider, Slider,
@ -36,6 +35,27 @@ const Constants = {
}, },
}; };
const PROGRES_BAR_INTERVAL_TIME = 20;
function setProgressBarTime(milliseconds) {
const elem = document.getElementById("progress-bar-time");
if (elem === null) return;
if (milliseconds <= 0) {
elem.innerText = "";
return;
}
const seconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
elem.innerText = `${hours > 0 ? hours + "h " : ""}${
minutes > 0 ? (minutes % 60) + "m " : ""
}${seconds % 60}s`;
}
function App() { function App() {
const screens = useBreakpoint(); const screens = useBreakpoint();
const [form] = useForm(); const [form] = useForm();
@ -47,14 +67,10 @@ function App() {
const [durationUnit, setDurationUnit] = useState("seconds"); const [durationUnit, setDurationUnit] = useState("seconds");
const remainingTimeRef = useRef(0); const remainingTimeRef = useRef(0);
const [remainingTime, setRemainingTime] = useState(0); const [processStatus, setProcessStatus] = useState("");
const [procesStatus, setProcessStatus] = useState("");
const intervalRef = useRef(null);
const setRemainingTimeProgress = (time) => { const setRemainingTimeProgress = (time) => {
remainingTimeRef.current = time; remainingTimeRef.current = time;
setRemainingTime(time);
}; };
const showErrorMessage = () => { const showErrorMessage = () => {
@ -90,36 +106,42 @@ function App() {
}, []); }, []);
useEffect(() => { useEffect(() => {
intervalRef.current = setInterval(() => { let elem = document.getElementById("progress-bar");
if (remainingTimeRef.current <= 0) { let width = 1;
clearInterval(intervalRef.current); let currentProgress = 0;
return;
}
setRemainingTime((prev) => { if (elem === null) return;
const newTime = prev - 150;
if (newTime <= 0) { const intervalId = setInterval(frame, PROGRES_BAR_INTERVAL_TIME);
clearInterval(intervalRef.current);
function frame() {
if (width >= 100) {
clearInterval(intervalId);
setRemainingTimeProgress(0); setRemainingTimeProgress(0);
if (processStatus !== "Angehalten" && processStatus !== "")
setProcessStatus("Fertig"); setProcessStatus("Fertig");
} else {
width = (100 / remainingTimeRef.current) * currentProgress + 1;
currentProgress += PROGRES_BAR_INTERVAL_TIME;
elem.style.width = width + "%";
} }
return newTime; setProgressBarTime(remainingTimeRef.current - currentProgress);
}); }
}, 150);
return () => { return () => {
clearInterval(intervalRef.current); clearInterval(intervalId);
}; };
}, [remainingTimeRef.current]); }, [remainingTimeRef.current]);
const sendControlRequest = (urlPath) => { const sendControlRequest = (urlPath) => {
setRemainingTimeProgress(0);
form form
.validateFields() .validateFields()
.then((values) => { .then((values) => {
setProcessStatus("Sende Anfrage");
myFetch({ myFetch({
url: urlPath, url: urlPath,
method: "POST", method: "POST",
@ -172,39 +194,44 @@ function App() {
layout="vertical" layout="vertical"
onFinish={() => sendControlRequest("/start")} onFinish={() => sendControlRequest("/start")}
> >
<Form.Item <div
label={`Status${procesStatus !== "" ? `: ${procesStatus}` : ""}`} style={{
> display: "flex",
<Space style={{ display: "block" }}> justifyContent: "space-between",
<Progress paddingBottom: 4,
percent={
procesStatus === "Fertig"
? 100
: 100 -
(
(100 / remainingTimeRef.current) *
remainingTime
).toFixed(2)
}
showInfo={false}
type="line"
status="active"
strokeColor={{
from: "#108ee9",
to: "#87d068",
}} }}
/> >
<span>{`Status${
processStatus !== "" ? `: ${processStatus}` : ""
}`}</span>
<span <span
id="progress-bar-time"
style={{ style={{
display: "inline-block", display: "inline-block",
width: 100, width: 100,
textAlign: "right", textAlign: "right",
}} }}
/>
</div>
<div
style={{
backgroundColor: "#f1f0f1",
height: 8,
borderRadius: 12,
}}
> >
{(remainingTime / 1000).toFixed(1)} Sekunden <div
</span> id="progress-bar"
</Space> style={{
</Form.Item> width: "0%",
height: "100%",
backgroundColor: "#98ccfc",
borderRadius: 12,
}}
/>
</div>
<Divider /> <Divider />
@ -341,6 +368,8 @@ function App() {
block block
danger danger
onClick={() => { onClick={() => {
setProcessStatus("Sende Anfrage");
myFetch({ myFetch({
url: "/stop", url: "/stop",
method: "POST", method: "POST",
@ -355,7 +384,7 @@ function App() {
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
showErrorMessage(); showErrorMessage();
setProcessStatus(""); setProcessStatus("Angehalten");
setRemainingTimeProgress(0); setRemainingTimeProgress(0);
}); });