diff --git a/src/App.js b/src/App.js index bf07a25..e211fa2 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,6 @@ import { Form, Grid, InputNumber, - Progress, Row, Select, 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() { const screens = useBreakpoint(); const [form] = useForm(); @@ -47,14 +67,10 @@ function App() { const [durationUnit, setDurationUnit] = useState("seconds"); const remainingTimeRef = useRef(0); - const [remainingTime, setRemainingTime] = useState(0); - const [procesStatus, setProcessStatus] = useState(""); - - const intervalRef = useRef(null); + const [processStatus, setProcessStatus] = useState(""); const setRemainingTimeProgress = (time) => { remainingTimeRef.current = time; - setRemainingTime(time); }; const showErrorMessage = () => { @@ -90,36 +106,42 @@ function App() { }, []); useEffect(() => { - intervalRef.current = setInterval(() => { - if (remainingTimeRef.current <= 0) { - clearInterval(intervalRef.current); - return; + let elem = document.getElementById("progress-bar"); + let width = 1; + let currentProgress = 0; + + 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) => { - const newTime = prev - 150; - - if (newTime <= 0) { - clearInterval(intervalRef.current); - setRemainingTimeProgress(0); - setProcessStatus("Fertig"); - } - - return newTime; - }); - }, 150); + setProgressBarTime(remainingTimeRef.current - currentProgress); + } return () => { - clearInterval(intervalRef.current); + clearInterval(intervalId); }; }, [remainingTimeRef.current]); const sendControlRequest = (urlPath) => { - setRemainingTimeProgress(0); - form .validateFields() .then((values) => { + setProcessStatus("Sende Anfrage"); + myFetch({ url: urlPath, method: "POST", @@ -172,39 +194,44 @@ function App() { layout="vertical" onFinish={() => sendControlRequest("/start")} > -