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;
if (elem === null) return;
const intervalId = setInterval(frame, PROGRES_BAR_INTERVAL_TIME);
function frame() {
if (width >= 100) {
clearInterval(intervalId);
setRemainingTimeProgress(0);
if (processStatus !== "Angehalten" && processStatus !== "")
setProcessStatus("Fertig");
} else {
width = (100 / remainingTimeRef.current) * currentProgress + 1;
currentProgress += PROGRES_BAR_INTERVAL_TIME;
elem.style.width = width + "%";
} }
setRemainingTime((prev) => { setProgressBarTime(remainingTimeRef.current - currentProgress);
const newTime = prev - 150; }
if (newTime <= 0) {
clearInterval(intervalRef.current);
setRemainingTimeProgress(0);
setProcessStatus("Fertig");
}
return newTime;
});
}, 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",
justifyContent: "space-between",
paddingBottom: 4,
}}
> >
<Space style={{ display: "block" }}> <span>{`Status${
<Progress processStatus !== "" ? `: ${processStatus}` : ""
percent={ }`}</span>
procesStatus === "Fertig"
? 100 <span
: 100 - id="progress-bar-time"
( style={{
(100 / remainingTimeRef.current) * display: "inline-block",
remainingTime width: 100,
).toFixed(2) textAlign: "right",
} }}
showInfo={false} />
type="line" </div>
status="active"
strokeColor={{ <div
from: "#108ee9", style={{
to: "#87d068", backgroundColor: "#f1f0f1",
}} height: 8,
/> borderRadius: 12,
<span }}
style={{ >
display: "inline-block", <div
width: 100, id="progress-bar"
textAlign: "right", style={{
}} width: "0%",
> height: "100%",
{(remainingTime / 1000).toFixed(1)} Sekunden backgroundColor: "#98ccfc",
</span> borderRadius: 12,
</Space> }}
</Form.Item> />
</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);
}); });