Skip to content

Commit 9477404

Browse files
committed
add the types again and create handleValidateFunction
1 parent fea3fb2 commit 9477404

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

app/components/CodeEditor/CodeEditor.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen";
1212
import { useRouter } from "next/navigation";
1313
import { useUserSolutionStore, useEditorStore } from "@/lib/stores";
1414
import { sendGAEvent } from "@next/third-parties/google";
15+
import { CodeFile, OutputResult } from "@/lib/types";
16+
import { OutputReducerAction } from "@/lib/reducers";
1517

1618
export default function CodeEditor({
1719
codeString,
@@ -22,14 +24,23 @@ export default function CodeEditor({
2224
stepIndex,
2325
chapterIndex,
2426
outputResult,
27+
}: {
28+
codeString: string;
29+
setCodeString: (codeString: string) => void;
30+
codeFile: CodeFile;
31+
dispatchOutput: React.Dispatch<OutputReducerAction>;
32+
nextStepPath: string | undefined;
33+
stepIndex: number;
34+
chapterIndex: number;
35+
outputResult: OutputResult;
2536
}) {
2637
const { colorMode } = useColorMode();
27-
const [monaco, setMonaco] = useState(null);
38+
const [monaco, setMonaco] = useState<any>(null);
2839
const [isValidating, setIsValidating] = useState(false);
2940
const router = useRouter();
3041
const editorStore = useEditorStore();
3142
const userSolutionStore = useUserSolutionStore();
32-
const editorRef = useRef(null);
43+
const editorRef = useRef<any>(null);
3344

3445
useEffect(() => {
3546
if (monaco) {
@@ -52,18 +63,7 @@ export default function CodeEditor({
5263
value: "Validate (through shortcut)",
5364
});
5465
event.preventDefault();
55-
setIsValidating(true);
56-
setTimeout(() => {
57-
tryFormattingCode(editorRef, setCodeString);
58-
validateCode(
59-
codeString,
60-
codeFile,
61-
dispatchOutput,
62-
stepIndex,
63-
chapterIndex,
64-
);
65-
setIsValidating(false);
66-
}, 500);
66+
handleValidate();
6767
}
6868
};
6969

@@ -97,6 +97,21 @@ export default function CodeEditor({
9797
}
9898
}, [userSolutionStore]);
9999

100+
const handleValidate = () => {
101+
setIsValidating(true);
102+
setTimeout(() => {
103+
tryFormattingCode(editorRef, setCodeString);
104+
validateCode(
105+
codeString,
106+
codeFile,
107+
dispatchOutput,
108+
stepIndex,
109+
chapterIndex,
110+
);
111+
setIsValidating(false);
112+
}, 500);
113+
};
114+
100115
return (
101116
<>
102117
<div className={ctx(styles.codeEditor, GeistMono.className)}>
@@ -124,24 +139,11 @@ export default function CodeEditor({
124139
<div className={styles.buttonsWrapper}>
125140
<Flex dir="row" gap="8px" alignItems="end">
126141
<MyBtn
127-
onClick={() => {
128-
setIsValidating(true);
129-
setTimeout(() => {
130-
tryFormattingCode(editorRef, setCodeString);
131-
validateCode(
132-
codeString,
133-
codeFile,
134-
dispatchOutput,
135-
stepIndex,
136-
chapterIndex,
137-
);
138-
setIsValidating(false);
139-
}, 500);
140-
}}
141-
isDisabled={isValidating}
142+
onClick={() => handleValidate()}
142143
variant={
143144
outputResult.validityStatus === "valid" ? "success" : "default"
144145
}
146+
isDisabled={isValidating}
145147
tooltip="Shift + Enter"
146148
>
147149
{isValidating ? "Validating..." : "Validate"}

0 commit comments

Comments
 (0)