Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diego code mirror component #1

Merged
merged 11 commits into from
Aug 25, 2020
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"@testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"bootstrap": "^4.4.1",
"codemirror": "^5.57.0",
"react": "^16.13.0",
"react-bootstrap": "^1.0.0-beta.17",
"react-codemirror2": "^7.2.1",
"react-dom": "^16.13.0",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
Expand Down
71 changes: 71 additions & 0 deletions src/components/CodeMirror/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { selectCorrectAnswer } from "../../store/exercise/selectors";
import { getExerciseById } from "../../store/exercise/actions";

import { Controlled as CodeMirror } from "react-codemirror2";
import "codemirror/lib/codemirror.css";
import "codemirror/mode/javascript/javascript";
import "codemirror/theme/material.css";

export default function CodeMirrorComponent() {
const dispatch = useDispatch();
const correctAnswer = useSelector(selectCorrectAnswer);
const [code, setCode] = useState("");
const [message, setMessage] = useState("");

//this is something we should do in the parent component I think
useEffect(() => {
dispatch(getExerciseById(1));
}, []);

const codeMirrorOptions = {
theme: "material",
lineNumbers: true,
scrollbarStyle: null,
lineWrapping: true,
};

function equal(a, b) {
const condition2 = typeof a === "string" && typeof b === "string";
if (condition2) return a === b;
}

function submitAnswer() {
const result = equal(correctAnswer, code);
if (!result) {
setMessage({
backgroundColor: "red",
text: "oeh-oeh-ahh-ahh monkey want banana? - This answer is incorrect!",
});
}
if (result) {
setMessage({
backgroundColor: "green",
text: "Long live the master! - This answer is correct!",
});
}
}

return (
<div style={{ backgroundColor: "grey" }}>
<CodeMirror
value={code}
options={{ mode: "javascript", ...codeMirrorOptions }}
onBeforeChange={(editor, data, js) => {
setCode(js);
}}
/>

{message && (
<p
style={{ backgroundColor: message.backgroundColor, color: "yellow" }}
>
{message.text}
</p>
)}

<button onClick={() => submitAnswer()}>Submit</button>
</div>
);
}
41 changes: 41 additions & 0 deletions src/store/exercise/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { apiUrl } from "../../config/constants";
import axios from "axios";
import { appLoading, appDoneLoading, setMessage } from "../appState/actions";
import { selectToken } from "../user/selectors";

export const GET_EXERCISE_SUCCESS = "GET_EXERCISE_SUCCESS";

const getExerciseSuccess = (exercise) => {
return {
type: GET_EXERCISE_SUCCESS,
payload: exercise,
};
};

export const getExerciseById = (id) => {
console.log("what is id in actions", id);
return async (dispatch, getState) => {
const token = selectToken(getState());
if (token === null) return;

dispatch(appLoading());
try {
const response = axios.get(`${apiUrl}/exercises/${id}/quiz`, {
headers: { Authorization: `Bearer ${token}` },
});

dispatch(getExerciseSuccess(response.data));

dispatch(appDoneLoading());
} catch (error) {
if (error.response) {
console.log(error.response.data.message);
dispatch(setMessage("danger", true, error.response.data.message));
} else {
console.log(error.message);
dispatch(setMessage("danger", true, error.message));
}
dispatch(appDoneLoading());
}
};
};
16 changes: 16 additions & 0 deletions src/store/exercise/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GET_EXERCISE_SUCCESS } from "./actions";

const initialState = {
name: "",
content: "",
correctAnswer: "",
};

export default (state = initialState, action) => {
switch (action.type) {
case GET_EXERCISE_SUCCESS:
return { ...state, ...action.payload };
default:
return state;
}
};
1 change: 1 addition & 0 deletions src/store/exercise/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectCorrectAnswer = (state) => state.exercise.correctAnswer;
4 changes: 3 additions & 1 deletion src/store/rootReducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { combineReducers } from "redux";
import appState from "./appState/reducer";
import user from "./user/reducer";
import exercise from "./exercise/reducer";

export default combineReducers({
appState,
user
user,
exercise,
});