Skip to content

Commit ec1236e

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix-linting-errors
2 parents 81e20a2 + 7a42015 commit ec1236e

5 files changed

Lines changed: 60 additions & 45 deletions

File tree

src/components/Button/Button.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const Button = (props) => {
99

1010
var buttonClass="btn"
1111
buttonClass = (className ? buttonClass += ` ${className}`: buttonClass)
12-
buttonClass = (disabled ? buttonClass += " btn--disabled" : buttonClass)
1312

1413
const onButtonClick = (e) => {
1514
if (!confirmText) {
@@ -32,7 +31,7 @@ const Button = (props) => {
3231
}
3332

3433
return (
35-
<button className={buttonClass} onClick={onButtonClick}>{buttonText}</button>
34+
<button className={buttonClass} disabled={disabled} onClick={onButtonClick}>{buttonText}</button>
3635
)
3736
};
3837

src/components/Button/Button.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
background-color: $editor-dark-turquoise;
2121
}
2222

23-
&.btn--disabled {
23+
&:disabled {
2424
background-color: $editor-grey;
2525
color: $editor-white;
2626
cursor: default;

src/components/Editor/Runners/PythonRunner/PythonRunner.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ const PythonRunner = () => {
2525

2626
const [senseHatEnabled, setSenseHatEnabled] = useState(false);
2727

28+
const getInput = () => {
29+
const pageInput = document.getElementById("input")
30+
const webComponentInput = document.querySelector('editor-wc') ? document.querySelector('editor-wc').shadowRoot.getElementById("input") : null;
31+
return pageInput || webComponentInput
32+
}
33+
2834
useEffect(() => {
2935
if (codeRunTriggered) {
3036
runCode();
3137
}
3238
}, [codeRunTriggered]);
3339

3440
useEffect(() => {
35-
if (codeRunStopped && document.getElementById("input")) {
36-
const input = document.getElementById("input")
41+
if (codeRunStopped && getInput()) {
42+
const input = getInput()
3743
input.removeAttribute("id")
3844
input.removeAttribute("contentEditable")
3945
dispatch(setError("Execution interrupted"));
@@ -44,8 +50,8 @@ const PythonRunner = () => {
4450
useEffect(() => {
4551
if (!drawTriggered && p5Output.current && p5Output.current.innerHTML !== '') {
4652
Sk.p5.stop();
47-
if (document.getElementById("input")) {
48-
const input = document.getElementById("input")
53+
if (getInput()) {
54+
const input = getInput()
4955
input.removeAttribute("id")
5056
input.removeAttribute("contentEditable")
5157
}
@@ -189,7 +195,7 @@ const PythonRunner = () => {
189195
const outputPane = output.current;
190196
outputPane.appendChild(inputSpan());
191197

192-
const input = document.getElementById("input") || document.querySelector('editor-wc').shadowRoot.getElementById("input")
198+
const input = getInput()
193199
input.focus();
194200

195201
return new Promise(function (resolve, reject) {
@@ -252,9 +258,10 @@ const PythonRunner = () => {
252258
},
253259
).catch(err => {
254260
const message = err.message || err.toString();
261+
console.log(message)
255262
dispatch(setError(message));
256-
if (document.getElementById("input")) {
257-
const input = document.getElementById("input") || document.querySelector('editor-wc').shadowRoot.getElementById("input")
263+
if (getInput()) {
264+
const input = getInput()
258265
input.removeAttribute("id")
259266
input.removeAttribute("contentEditable")
260267
}
@@ -274,9 +281,9 @@ const PythonRunner = () => {
274281
return;
275282
}
276283

277-
const inputBox = document.getElementById("input") || document.querySelector('editor-wc').shadowRoot.getElementById("input");
284+
const inputBox = getInput();
278285
if (inputBox && e.target !== inputBox) {
279-
const input = document.getElementById("input") || document.querySelector('editor-wc').shadowRoot.getElementById("input")
286+
const input = getInput()
280287
const selection = window.getSelection();
281288
selection.removeAllRanges();
282289

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Button from '../Button/Button'
2-
import React from 'react';
2+
import React, { useEffect, useState } from 'react';
33
import { useDispatch, useSelector } from 'react-redux'
44
import { stopCodeRun, stopDraw } from '../Editor/EditorSlice'
55

66
const StopButton = (props) => {
77

88
const codeRunStopped = useSelector((state) => state.editor.codeRunStopped);
99
const codeRunTriggered = useSelector((state) => state.editor.codeRunTriggered);
10+
const [, setTimeoutId] = useState(null);
1011
const dispatch = useDispatch();
1112

1213
const onClickStop = () => {
@@ -16,15 +17,23 @@ const StopButton = (props) => {
1617
dispatch(stopDraw());
1718
}
1819

19-
if (codeRunStopped) {
20-
return (
21-
<Button buttonText="Stopping..." disabled="true"/>
22-
)
23-
} else {
24-
return (
25-
<Button className={"btn--stop"} onClickHandler={onClickStop} {...props} />
26-
)
27-
}
20+
const stop = <Button className={"btn--stop"} onClickHandler={onClickStop} {...props} />
21+
const [button, setButton] = useState(stop)
22+
23+
useEffect(() => {
24+
const stopping = <Button buttonText="Stopping..." disabled />
25+
const id = setTimeout(
26+
function() {
27+
if (codeRunStopped) {
28+
setButton(stopping)
29+
}
30+
}, 100);
31+
setTimeoutId(id);
32+
}, [codeRunStopped]);
33+
34+
return (
35+
button
36+
)
2837
};
2938

3039
export default StopButton;

src/components/RunButton/StopButton.test.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ import { codeRunHandled, triggerCodeRun } from '../Editor/EditorSlice'
77

88

99
test("Clicking stop button sets codeRunStopped to true", () => {
10-
store.dispatch(codeRunHandled())
11-
store.dispatch(triggerCodeRun())
10+
store.dispatch(codeRunHandled())
11+
store.dispatch(triggerCodeRun())
1212

13-
const { getByText } = render(
14-
<Provider store={store}>
15-
<StopButton buttonText="Stop Code" />
16-
</Provider>);
13+
const { getByText } = render(
14+
<Provider store={store}>
15+
<StopButton buttonText="Stop Code" />
16+
</Provider>);
1717

18-
const stopButton = getByText(/Stop Code/);
19-
fireEvent.click(stopButton);
18+
const stopButton = getByText(/Stop Code/);
19+
fireEvent.click(stopButton);
2020

21-
expect(store.getState().editor.codeRunStopped).toEqual(true);
21+
expect(store.getState().editor.codeRunStopped).toEqual(true);
2222
})
2323

24-
test("Clicking stop button changes it to 'Stopping...'", () => {
25-
store.dispatch(codeRunHandled())
26-
store.dispatch(triggerCodeRun())
27-
28-
const { getByText } = render(
29-
<Provider store={store}>
30-
<StopButton buttonText="Stop Code" />
31-
<span id="input">hello world</span>
32-
</Provider>
33-
);
34-
const stopButton = getByText(/Stop Code/);
35-
fireEvent.click(stopButton);
36-
24+
test("Clicking stop button changes it to 'Stopping...' after 100ms", () => {
25+
store.dispatch(codeRunHandled())
26+
store.dispatch(triggerCodeRun())
27+
28+
const { getByText } = render(
29+
<Provider store={store}>
30+
<StopButton buttonText="Stop Code" />
31+
</Provider>
32+
);
33+
const stopButton = getByText(/Stop Code/);
34+
fireEvent.click(stopButton);
35+
expect(stopButton.textContent).toEqual("Stop Code")
36+
new Promise(resolve => setTimeout(resolve, 100)).then( () =>
3737
expect(stopButton.textContent).toEqual("Stopping...")
38-
38+
)
3939
})

0 commit comments

Comments
 (0)