diff --git a/src/components/QuizCards/index.js b/src/components/QuizCards/index.js index 3078419..9813d60 100644 --- a/src/components/QuizCards/index.js +++ b/src/components/QuizCards/index.js @@ -1,3 +1,4 @@ + import React, { useState } from "react" import { Card, Button, Form, Container, Row, Col, Spinner } from "react-bootstrap" import { useDispatch, useSelector } from "react-redux" @@ -22,136 +23,161 @@ export default function QuizCards(props){ const userNeeded = useSelector(selectUser) console.log("here is the user", userNeeded) - if(quizQuestions === undefined){ - return - } + if (quizQuestions === undefined) { + return ; + } - function correctOrNot(){ - if(review === ""){ - return "info" - } else if(review === "incorrect"){ - return "danger" - } else if(review === "correct"){ - return "success" - } else { - return "warning" - } + function correctOrNot() { + if (review === "") { + return "info"; + } else if (review === "incorrect") { + return "danger"; + } else if (review === "correct") { + return "success"; + } else { + return "warning"; } - console.log("variant test", correctOrNot()) + } - if(answered === 2){ - history.push("/homepage") + function randomAnswers(randomNum) { + if (randomNum <= 2.5) { + return ( +
+ + Possible Solutions: + + + + + + + +
+ ); + } else if (2.5 <= randomNum && randomNum <= 5) { + return ( +
+ + Possible Solutions: + + + + + + + +
+ ); + } else if (5 <= randomNum && randomNum <= 7.5) { + return ( +
+ + Possible Solutions: + + + + + + + +
+ ); + } else { + return ( +
+ + Possible Solutions: + + + + + + + +
+ ); } - console.log("answered test", answered) - - function randomAnswers(randomNum){ - // console.log("randome number test", randomNum) - if(randomNum <= 2.5){ - return ( -
- - - Possible Solutions: - - - - - - - - -
- ) - } else if(2.5 <= randomNum && randomNum <= 5){ - return ( -
- - - Possible Solutions: - - - - - - - - -
- ) - } else if(5 <= randomNum&& randomNum <=7.5){ - return ( -
- - - Possible Solutions: - - - - - - - - -
- ) - } else { - return ( -
- - - Possible Solutions: - - - - - - - - -
- ) - } - } + } return ( @@ -193,4 +219,5 @@ export default function QuizCards(props){ ) -} \ No newline at end of file +} + diff --git a/src/components/QuizCode/index.js b/src/components/QuizCode/index.js index 9c670b1..b0fda17 100644 --- a/src/components/QuizCode/index.js +++ b/src/components/QuizCode/index.js @@ -1,8 +1,6 @@ import React, { useState, useEffect } from "react"; import { useDispatch } from "react-redux"; -import { getExerciseById } from "../../store/exercise/actions"; import { updateCompletedExercise } from "../../store/user/actions"; - import { Controlled as CodeMirror } from "react-codemirror2"; import "codemirror/lib/codemirror.css"; import "codemirror/mode/javascript/javascript"; @@ -22,10 +20,6 @@ export default function QuizCode(props) { const minutes = date.getMinutes(); const seconds = date.getSeconds(); - useEffect(() => { - dispatch(getExerciseById(1)); - }, [dispatch]); - const codeMirrorOptions = { theme: "material", lineNumbers: true, @@ -116,6 +110,8 @@ export default function QuizCode(props) { } }; + console.log("we get here?"); + finish && dispatch( updateCompletedExercise(exerciseId, id, finalTime(), experience()) diff --git a/src/pages/Exercise/index.js b/src/pages/Exercise/index.js index 179ac24..541331b 100644 --- a/src/pages/Exercise/index.js +++ b/src/pages/Exercise/index.js @@ -10,32 +10,43 @@ import QuizCode from "../../components/QuizCode"; export default function Exercise() { const param = useParams(); + console.log("param", param); const exerciseId = parseInt(param.id); console.log("ex id test", exerciseId); + const dispatch = useDispatch(); const allCurrentExercises = useSelector(selectExercise); const completedExercises = useSelector(selectCompletedExercises); const [currentExercise, setCurrentExercise] = useState(""); - console.log("all ex", allCurrentExercises); + const [finish, setFinish] = useState(false); useEffect(() => { - completedExercises.forEach((item) => { - const correctExercise = allCurrentExercises.find((x) => { - if (item.quizQuestionId !== x.id) { - return true; - } else { - return false; + //check if any of the exercises are completed by user, add to new array "commonElements" + function getCommon(arr1, arr2) { + let common = []; + for (let i = 0; i < arr1.length; ++i) { + for (let j = 0; j < arr2.length; ++j) { + if (arr1[i].id === arr2[j].quizQuestionId) { + common.push(arr1[i]); + } } - }); - setCurrentExercise(correctExercise); + } + return common; + } + const commonElements = getCommon(allCurrentExercises, completedExercises); + + //Create a new array that has all exercises the user has not done yet (allCurrentExercises - commonElements) = leftOverExercises + const leftOverExercises = allCurrentExercises.filter((el) => { + return !commonElements.includes(el); }); - if (completedExercises.length === 0) { - const correctExercise = allCurrentExercises.find( - (item) => item.level === "level 1" - ); - setCurrentExercise(correctExercise); + if (leftOverExercises.length !== 0) { + setCurrentExercise(leftOverExercises[0]); + } + + if (leftOverExercises.length === 0 && commonElements.length !== 0) { + setFinish(true); } }, [completedExercises, allCurrentExercises]); @@ -43,7 +54,6 @@ export default function Exercise() { dispatch(getCompletedExercises()); dispatch(getExerciseById(exerciseId)); }, [dispatch, exerciseId]); - console.log("current exercise", currentExercise); const questionFormat = () => { if (currentExercise && currentExercise.level === "level 1") { @@ -52,10 +62,9 @@ export default function Exercise() { return ; } else { //return some loading indicator would be better - console.log("test"); return null; } }; - return
{questionFormat()}
; + return
{!finish ? questionFormat() : "You are finished!"}
; } diff --git a/src/pages/Homepage/index.js b/src/pages/Homepage/index.js index c4a2b0c..ecfe2ac 100644 --- a/src/pages/Homepage/index.js +++ b/src/pages/Homepage/index.js @@ -1,6 +1,10 @@ import React, { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; -import { getExercises } from "../../store/exercise/actions"; +import { + getExercises, + removeQuizQuestions, +} from "../../store/exercise/actions"; +import { removeCompletedExercises } from "../../store/user/actions"; import { selectMethod } from "../../store/exercise/selectors"; import "./homepage.css"; @@ -10,10 +14,14 @@ import { Link } from "react-router-dom"; export default function Homepage() { const dispatch = useDispatch(); const exercises = useSelector(selectMethod); - const [searchTerm, setSearchTerm] = useState(); const [searchResults, setSearchResults] = useState([]); + useEffect(() => { + dispatch(removeCompletedExercises()); + dispatch(removeQuizQuestions()); + }, []); + useEffect(() => { if (exercises.length === 0) { dispatch(getExercises()); diff --git a/src/store/exercise/actions.js b/src/store/exercise/actions.js index fc1ec18..aeffb56 100644 --- a/src/store/exercise/actions.js +++ b/src/store/exercise/actions.js @@ -7,6 +7,12 @@ export const GET_EXERCISE_SUCCESS = "GET_EXERCISE_SUCCESS"; export const SET_QUIZ_QUESTIONS = "SET_QUIZ_QUESTIONS"; +export const REMOVE_QUIZ_QUESTIONS = "REMOVE_QUIZ_QUESTIONS"; + +export const removeQuizQuestions = () => ({ + type: REMOVE_QUIZ_QUESTIONS, +}); + export const setQuizQuestions = (data) => { return { type: SET_QUIZ_QUESTIONS, @@ -35,7 +41,6 @@ export const getExercises = () => { }; export const getExerciseById = (id) => { - console.log("what is id in actions", id); return async (dispatch, getState) => { const token = selectToken(getState()); if (token === null) return; diff --git a/src/store/exercise/reducer.js b/src/store/exercise/reducer.js index cfa8f40..a87ab88 100644 --- a/src/store/exercise/reducer.js +++ b/src/store/exercise/reducer.js @@ -1,4 +1,8 @@ -import { GET_EXERCISE_SUCCESS, SET_QUIZ_QUESTIONS } from "./actions"; +import { + GET_EXERCISE_SUCCESS, + SET_QUIZ_QUESTIONS, + REMOVE_QUIZ_QUESTIONS, +} from "./actions"; const initialState = { exercises: [], @@ -10,6 +14,9 @@ const initialState = { export default (state = initialState, action) => { switch (action.type) { + case REMOVE_QUIZ_QUESTIONS: + return { ...state, questions: [] }; + case SET_QUIZ_QUESTIONS: return { ...state, diff --git a/src/store/user/actions.js b/src/store/user/actions.js index 0303a7d..ac8c204 100644 --- a/src/store/user/actions.js +++ b/src/store/user/actions.js @@ -14,6 +14,12 @@ export const LOG_OUT = "LOG_OUT"; export const GET_COMPLETED_EXERCISES_SUCCESS = "GET_COMPLETED_EXERCISES_SUCCESS"; +export const REMOVE_COMPLETED_EXERCISES = "REMOVE_COMPLETED_EXERCISES"; + +export const removeCompletedExercises = () => ({ + type: REMOVE_COMPLETED_EXERCISES, +}); + const loginSuccess = (userWithToken) => { return { type: LOGIN_SUCCESS, @@ -61,8 +67,6 @@ export const signUp = (fullName, email, password) => { export const login = (email, password) => { return async (dispatch, getState) => { - console.log("email", email); - console.log("passwrod", password); dispatch(appLoading()); try { const response = await axios.post(`${apiUrl}/login`, { @@ -156,6 +160,7 @@ export const updateCompletedExercise = (exerciseId, quizId, timeTaken, exp) => { ); if (response.status === 202) { + dispatch(getCompletedExercisesSuccess([response.data.completed])); dispatch(appDoneLoading()); } } catch (error) { @@ -182,8 +187,6 @@ export function sendCompletedQuiz(exerciseId, quizId) { }, } ); - console.log("updated info test", infoUpdated.data.user); - dispatch(getCompletedExercisesSuccess(infoUpdated.data.completedQuiz)); dispatch(tokenStillValid(infoUpdated.data.user)); } catch (error) { diff --git a/src/store/user/reducer.js b/src/store/user/reducer.js index 2f4d4c8..9fe56b5 100644 --- a/src/store/user/reducer.js +++ b/src/store/user/reducer.js @@ -3,6 +3,7 @@ import { LOGIN_SUCCESS, TOKEN_STILL_VALID, GET_COMPLETED_EXERCISES_SUCCESS, + REMOVE_COMPLETED_EXERCISES, } from "./actions"; const initialState = { @@ -20,6 +21,9 @@ export default (state = initialState, action) => { completedExercises: [...state.completedExercises, ...action.payload], }; + case REMOVE_COMPLETED_EXERCISES: + return { ...state, completedExercises: [] }; + case LOGIN_SUCCESS: localStorage.setItem("token", action.payload.token); return { ...state, ...action.payload };