Skip to content

Commit

Permalink
feat: switch from md file data to meta.js files
Browse files Browse the repository at this point in the history
  • Loading branch information
iamaseem committed May 18, 2021
1 parent 61ef8dd commit 6a192b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
28 changes: 17 additions & 11 deletions component/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import { javascript } from "@codemirror/lang-javascript";
import { oneDark } from "@codemirror/theme-one-dark";
import Runbutton from "./RunButton";

const Codemirror = ({ initialValue }) => {
const Codemirror = ({ courseName, chapterName }) => {
useEffect(() => {
if (editor.current)
editor.current.setState(
EditorState.create({
doc: "",
extensions: [basicSetup, javascript(), oneDark, onUpdate(), baseTheme]
extensions: [
basicSetup,
javascript(),
oneDark,
onUpdate(),
baseTheme,
],
})
);
}, [initialValue]);
}, [chapterName]);
// Local state
const [editorValue, setEditorValue] = useState("");
const [editorTreeValue, setEditorTreeValue] = useState([]);
Expand All @@ -40,14 +46,14 @@ const Codemirror = ({ initialValue }) => {

let baseTheme = EditorView.baseTheme({
".cm-scroller": {
height: "85vh"
height: "85vh",
},
"&light .cm-o-replacement": {
backgroundColor: "#04c"
backgroundColor: "#04c",
},
"&dark .cm-o-replacement": {
backgroundColor: "#5bf"
}
backgroundColor: "#5bf",
},
});

// Initialize view
Expand All @@ -56,10 +62,10 @@ const Codemirror = ({ initialValue }) => {
editor.current = new EditorView({
state: EditorState.create({
doc: "",
extensions: [basicSetup, javascript(), oneDark, onUpdate(), baseTheme]
extensions: [basicSetup, javascript(), oneDark, onUpdate(), baseTheme],
}),
parent: el,
baseTheme
baseTheme,
});
}, []);

Expand All @@ -68,8 +74,8 @@ const Codemirror = ({ initialValue }) => {
<div id="codemirror-editor-wrapper" />
<Runbutton
editorVal={editorTreeValue}
courseName={initialValue.courseName}
chapterName={initialValue.chapterName}
courseName={courseName}
chapterName={chapterName}
/>
</div>
);
Expand Down
15 changes: 4 additions & 11 deletions context/user/UserState.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ const UserState = ({ children }) => {
const initialState = {
user: {},
run: false,
test: [
{
id: 1,
case: ["", ""],
hint: "Go forward",
isCorrect: true
}
]
test: [],
};

const [state, dispatch] = useReducer(UserReducer, initialState);
Expand All @@ -23,15 +16,15 @@ const UserState = ({ children }) => {
const setTest = (testCase) => {
dispatch({
type: SET_TESTS,
payload: testCase
payload: testCase,
});
};

//set run
const setRun = (value) => {
dispatch({
type: SET_RUN,
payload: value
payload: value,
});
};

Expand All @@ -44,7 +37,7 @@ const UserState = ({ children }) => {
run: state.run,
test: state.test,
setTest,
setRun
setRun,
}}
>
{children}
Expand Down
6 changes: 3 additions & 3 deletions courses/javascript/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ testCase:
{
id: 1,
chapterName: "introduction",
case: ["", ""],
case: [],
hint: "Go forward",
isCorrect: true
}
isCorrect: true,
},
]
---

Expand Down
31 changes: 23 additions & 8 deletions pages/curriculum/[courseName]/[chapterName].js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export default function Chapter({ chapterData, chapterName, courseName }) {
}, [testCase]);
useEffect(() => {
let progressTem = JSON.parse(window.localStorage.getItem("progress"));
console.log(progressTem);
if (progressTem) {
let progressData = progressTem.find((post, index) => {
if (post.courseName === courseName) {
Expand Down Expand Up @@ -118,9 +117,10 @@ export default function Chapter({ chapterData, chapterName, courseName }) {
}, [progress]);

let currentCourse = getCourse(courseName);
const routerClick = (courseName, chapterName, e) => {
const routerClick = (courseName, chapterEntry, e) => {
e.preventDefault();
if (chapterName) {
if (chapterEntry !== undefined) {
let chapterName = chapterEntry.chapterUrl;
router.push(`/curriculum/${courseName}/${chapterName}`);
}
return;
Expand Down Expand Up @@ -163,7 +163,7 @@ export default function Chapter({ chapterData, chapterName, courseName }) {
/>
</div>
<div>
<Editor initialValue={chapterData} />
<Editor courseName={courseName} chapterName={chapterName} />
</div>
<div>
<Output />
Expand All @@ -185,10 +185,16 @@ export default function Chapter({ chapterData, chapterName, courseName }) {
chapterData.prev ? `cursor-pointer` : `cursor-not-allowed`
}`}
onClick={(e) =>
routerClick(chapterData.courseName, chapterData.prev, e)
routerClick(
courseName,
currentCourse.chapters[
findCourseIndex(courses, chapterName, courseName, false) - 1
],
e
)
}
>
{chapterData.prev ? (
{findCourseIndex(courses, chapterName, courseName, false) > 0 ? (
<img
src="/images/svgs/leftarrawo.svg"
className="h-12 inline-block"
Expand All @@ -215,9 +221,18 @@ export default function Chapter({ chapterData, chapterName, courseName }) {
className={`text-white ${
chapterData.next ? `cursor-pointer` : `cursor-not-allowed`
}`}
onClick={(e) => routerClick(courseName, chapterData.next, e)}
onClick={(e) =>
routerClick(
courseName,
currentCourse.chapters[
findCourseIndex(courses, chapterName, courseName, false) + 1
],
e
)
}
>
{chapterData.next ? (
{findCourseIndex(courses, chapterName, courseName, false) + 1 <
currentCourse.chapters.length ? (
<img
src="/images/svgs/rightarrow.svg"
className="h-12 inline-block"
Expand Down

0 comments on commit 6a192b3

Please sign in to comment.