diff --git a/src/components/Workspace/Playground/Playground.tsx b/src/components/Workspace/Playground/Playground.tsx index a9d1b79..c8f5860 100644 --- a/src/components/Workspace/Playground/Playground.tsx +++ b/src/components/Workspace/Playground/Playground.tsx @@ -8,7 +8,8 @@ import EditorFooter from "./EditorFooter"; import { Problem } from "@/utils/types/problem"; import { useAuthState } from "react-firebase-hooks/auth"; import { auth, firestore } from "@/firebase/firebase"; -import { toast } from "react-toastify"; +import { toast, ToastContainer } from "react-toastify"; +import "react-toastify/dist/ReactToastify.css"; import { problems } from "@/utils/problems"; import { useRouter } from "next/router"; import { arrayUnion, doc, updateDoc } from "firebase/firestore"; @@ -114,7 +115,7 @@ const Playground: React.FC = ({ problem, setSuccess, setSolved return (
- +
>; + setSolved: React.Dispatch>; +}; + +export interface ISettings { + fontSize: string; + settingsModalIsOpen: boolean; + dropdownIsOpen: boolean; +} + +const Playground: React.FC = ({ problem, setSuccess, setSolved }) => { + const [activeTestCaseId, setActiveTestCaseId] = useState(0); + let [userCode, setUserCode] = useState(problem.starterCode); + + const [fontSize, setFontSize] = useLocalStorage("lcc-fontSize", "16px"); + + const [settings, setSettings] = useState({ + fontSize: fontSize, + settingsModalIsOpen: false, + dropdownIsOpen: false, + }); + + const [user] = useAuthState(auth); + const { + query: { pid }, + } = useRouter(); + + const handleSubmit = async () => { + if (!user) { + toast.error("Please login to submit your code", { + position: "top-center", + autoClose: 3000, + theme: "dark", + }); + return; + } + try { + userCode = userCode.slice(userCode.indexOf(problem.starterFunctionName)); + const cb = new Function(`return ${userCode}`)(); + const handler = problems[pid as string].handlerFunction; + + if (typeof handler === "function") { + const success = handler(cb); + if (success) { + toast.success("Congrats! All tests passed!", { + position: "top-center", + autoClose: 3000, + theme: "dark", + }); + setSuccess(true); + setTimeout(() => { + setSuccess(false); + }, 4000); + + const userRef = doc(firestore, "users", user.uid); + await updateDoc(userRef, { + solvedProblems: arrayUnion(pid), + }); + setSolved(true); + } + } + } catch (error: any) { + console.log(error.message); + if ( + error.message.startsWith("AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:") + ) { + toast.error("Oops! One or more test cases failed", { + position: "top-center", + autoClose: 3000, + theme: "dark", + }); + } else { + toast.error(error.message, { + position: "top-center", + autoClose: 3000, + theme: "dark", + }); + } + } + }; + + useEffect(() => { + const code = localStorage.getItem(`code-${pid}`); + if (user) { + setUserCode(code ? JSON.parse(code) : problem.starterCode); + } else { + setUserCode(problem.starterCode); + } + }, [pid, user, problem.starterCode]); + + const onChange = (value: string) => { + setUserCode(value); + localStorage.setItem(`code-${pid}`, JSON.stringify(value)); + }; + + return ( +
+ + + +
+ +
+
+ {/* testcase heading */} +
+
+
Testcases
+
+
+
+ +
+ {problem.examples.map((example, index) => ( +
setActiveTestCaseId(index)} + > +
+
+ Case {index + 1} +
+
+
+ ))} +
+ +
+

Input:

+
+ {problem.examples[activeTestCaseId].inputText} +
+

Output:

+
+ {problem.examples[activeTestCaseId].outputText} +
+
+
+
+ +
+ ); +}; +export default Playground;