Skip to content

Commit 87b8531

Browse files
committed
Working loading icons
1 parent 6b96bf9 commit 87b8531

File tree

3 files changed

+104
-38
lines changed

3 files changed

+104
-38
lines changed

react_frontend/src/components/buttons/PlayPause.tsx

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,31 @@ import React from "react";
1212

1313
import PlayArrowRoundedIcon from "@mui/icons-material/PlayArrowRounded";
1414
import PauseRoundedIcon from "@mui/icons-material/PauseRounded";
15+
import SyncRoundedIcon from "@mui/icons-material/SyncRounded";
1516

1617
const PlayPauseButton = ({
1718
project,
1819
language,
19-
appRunning,
20-
setAppRunning,
2120
dlModel,
2221
hasDLModel,
2322
connectManager,
2423
}: {
2524
project: string;
2625
language?: string;
27-
appRunning: boolean;
28-
setAppRunning: (running: boolean) => void;
2926
dlModel: ArrayBuffer | undefined;
3027
hasDLModel: boolean;
31-
connectManager: (desiredState?: string, callback?: () => void) => Promise<void>;
28+
connectManager: (
29+
desiredState?: string,
30+
callback?: () => void
31+
) => Promise<void>;
3232
}) => {
3333
const theme = useAcademyTheme();
3434
const exerciseContext = useExercise();
3535
const { warning, error, info, close } = useError();
3636
const codeRef = useRef("");
3737
const runningCodeRef = useRef("");
38+
const [state, setState] = useState<string>(CommsManager.getInstance().getState());
39+
const [loading, setLoading] = useState<boolean>(false);
3840
const isCodeUpdatedRef = useRef<boolean | undefined>(undefined);
3941
const [isCodeUpdated, _updateCode] = useState<boolean | undefined>(false);
4042

@@ -43,28 +45,45 @@ const PlayPauseButton = ({
4345
_updateCode(data);
4446
};
4547

48+
const updateState = (e: any) => {
49+
setState(e.detail.state);
50+
};
51+
4652
useEffect(() => {
4753
subscribe("autoSaveCompleted", () => {
4854
updateCode(true);
4955
});
56+
subscribe("CommsManagerStateChange", updateState);
5057

5158
return () => {
5259
unsubscribe("autoSaveCompleted", () => {});
60+
unsubscribe("CommsManagerStateChange", () => {});
5361
};
5462
}, []);
5563

5664
useEffect(() => {
5765
codeRef.current = exerciseContext.code;
5866
}, [exerciseContext]);
5967

68+
useEffect(() => {
69+
if (state === states.RUNNING || state === states.PAUSED) {
70+
setLoading(false);
71+
}
72+
}, [state]);
73+
6074
// App handling
6175

6276
const onAppStateChange = async (save?: boolean) => {
77+
6378
const manager = CommsManager.getInstance();
79+
setLoading(true);
6480

6581
if (manager.getState() === states.IDLE) {
66-
info("Connecting with the Robotics Backend ...")
67-
connectManager(states.TOOLS_READY, () => {onAppStateChange(); close()});
82+
info("Connecting with the Robotics Backend ...");
83+
connectManager(states.TOOLS_READY, () => {
84+
onAppStateChange();
85+
close();
86+
});
6887
return;
6988
}
7089

@@ -77,13 +96,13 @@ const PlayPauseButton = ({
7796
warning(
7897
"Failed to found a running simulation. Please make sure an universe is selected."
7998
);
99+
setLoading(false);
80100
return;
81101
}
82102

83-
if (appRunning) {
103+
if (state === states.RUNNING) {
84104
try {
85105
await manager.pause();
86-
setAppRunning(false);
87106
console.log("App paused correctly!");
88107
return;
89108
} catch (e: unknown) {
@@ -108,7 +127,6 @@ const PlayPauseButton = ({
108127
runningCodeRef.current === codeRef.current
109128
) {
110129
await manager.resume();
111-
setAppRunning(true);
112130
console.log("App resumed correctly!");
113131
return;
114132
}
@@ -125,7 +143,6 @@ const PlayPauseButton = ({
125143
} else {
126144
commonsZip = zip;
127145
}
128-
129146

130147
const extraFiles: { name: string; content: string }[] =
131148
await getProjectExtraFiles(project, language ? language : "python");
@@ -166,9 +183,9 @@ const PlayPauseButton = ({
166183
reader.readAsDataURL(content);
167184
});
168185

169-
setAppRunning(true);
170186
console.log("App started successfully");
171187
} catch (e: unknown) {
188+
setLoading(false);
172189
if (e instanceof Error) {
173190
console.error("Error running app: " + e.message);
174191
error("Error running app: " + e.message);
@@ -177,20 +194,29 @@ const PlayPauseButton = ({
177194
};
178195

179196
return (
180-
<StyledHeaderButton
181-
bgColor={theme.palette.primary}
182-
hoverColor={theme.palette.secondary}
183-
roundness={theme.roundness}
184-
id="run-app"
185-
onClick={() => onAppStateChange(undefined)}
186-
title="Run app"
187-
>
188-
{appRunning ? (
189-
<PauseRoundedIcon htmlColor={theme.palette.text} />
190-
) : (
191-
<PlayArrowRoundedIcon htmlColor={theme.palette.text} />
192-
)}
193-
</StyledHeaderButton>
197+
<>
198+
<StyledHeaderButton
199+
bgColor={theme.palette.primary}
200+
hoverColor={theme.palette.secondary}
201+
roundness={theme.roundness}
202+
id="run-app"
203+
onClick={() => onAppStateChange(undefined)}
204+
title="Run app"
205+
disabled={loading}
206+
>
207+
{loading ? (
208+
<SyncRoundedIcon htmlColor={theme.palette.text} id="loading-spin" />
209+
) : (
210+
<>
211+
{state === states.RUNNING ? (
212+
<PauseRoundedIcon htmlColor={theme.palette.text} />
213+
) : (
214+
<PlayArrowRoundedIcon htmlColor={theme.palette.text} />
215+
)}
216+
</>
217+
)}
218+
</StyledHeaderButton>
219+
</>
194220
);
195221
};
196222

react_frontend/src/components/buttons/Reset.tsx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
import { StyledHeaderButton } from "Styles/headers/HeaderMenu.styles";
22
import { useError } from "jderobot-ide-interface";
3-
import { CommsManager } from "jderobot-commsmanager";
3+
import { CommsManager, events, states } from "jderobot-commsmanager";
44
import ReplayRoundedIcon from "@mui/icons-material/ReplayRounded";
55
import { useAcademyTheme } from "Contexts/AcademyThemeContext";
6-
import React from "react";
6+
import React, { useEffect, useState } from "react";
7+
import { subscribe, unsubscribe } from "Helpers/utils";
8+
import SyncRoundedIcon from "@mui/icons-material/SyncRounded";
79

8-
const ResetButton = ({
9-
setAppRunning,
10-
}: {
11-
setAppRunning: (running: boolean) => void;
12-
}) => {
10+
const ResetButton = ({}: {}) => {
1311
const theme = useAcademyTheme();
1412
const { warning } = useError();
1513

14+
const [state, setState] = useState<string>(
15+
CommsManager.getInstance().getState()
16+
);
17+
const [loading, setLoading] = useState<boolean>(false);
18+
19+
const updateState = (e: any) => {
20+
setState(e.detail.state);
21+
};
22+
23+
useEffect(() => {
24+
subscribe("CommsManagerStateChange", updateState);
25+
26+
return () => {
27+
unsubscribe("CommsManagerStateChange", () => {});
28+
};
29+
}, []);
30+
31+
useEffect(() => {
32+
if (state === states.TOOLS_READY) {
33+
setLoading(false);
34+
}
35+
}, [state]);
36+
1637
const onResetApp = async () => {
1738
const manager = CommsManager.getInstance();
1839

1940
if (
20-
manager.getState() !== "tools_ready" &&
21-
manager.getState() !== "application_running" &&
22-
manager.getState() !== "paused"
41+
state === states.CONNECTED ||
42+
state === states.IDLE ||
43+
state === states.WORLD_READY
2344
) {
2445
console.error("Simulation is not ready!");
2546
warning(
@@ -28,9 +49,10 @@ const ResetButton = ({
2849
return;
2950
}
3051

52+
setLoading(true);
53+
3154
await manager.terminateApplication();
3255
console.log("App reseted!");
33-
setAppRunning(false);
3456
};
3557

3658
return (
@@ -42,7 +64,11 @@ const ResetButton = ({
4264
onClick={onResetApp}
4365
title="Reset app"
4466
>
45-
<ReplayRoundedIcon htmlColor={theme.palette.text} />
67+
{loading ? (
68+
<SyncRoundedIcon htmlColor={theme.palette.text} id="loading-spin" />
69+
) : (
70+
<ReplayRoundedIcon htmlColor={theme.palette.text} />
71+
)}
4672
</StyledHeaderButton>
4773
);
4874
};

react_frontend/src/styles/headers/HeaderMenu.styles.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ export const StyledHeaderButton = styled.button<StyledHeaderButtonProps>`
6969
width: 24px;
7070
height: 24px;
7171
}
72+
73+
@keyframes spin {
74+
from {
75+
transform: rotate(360deg);
76+
}
77+
to {
78+
transform: rotate(0deg);
79+
}
80+
}
81+
82+
#loading-spin {
83+
animation: spin 2s linear infinite;
84+
opacity: 50%;
85+
}
7286
`;
7387

7488
interface StyledDropdownProps {

0 commit comments

Comments
 (0)