Advertisement
Fahim_7861

MCQ EXAM

Aug 8th, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import React, { useEffect, useRef, useState } from "react";
  2. import { useDispatch, useSelector } from "react-redux";
  3. import { useHistory, useParams } from "react-router";
  4. import QuestionCard from "../../Components/AllCard/QuestionCard/QuestionCard";
  5. import SaveIcon from "@material-ui/icons/Save";
  6. import { Container, duration, makeStyles } from "@material-ui/core";
  7. import Button from "@material-ui/core/Button";
  8. import { EXAMID } from "../../constants/types";
  9. import { mcqAnsActions } from "../../redux/actions/mcqAnsActions";
  10. import submitAnsAction from "../../redux/actions/submitAnsAction";
  11. import { archieved, mainpage, RESPONSE } from "../../constants/constants";
  12. import submitAnsArcievedAction from "../../redux/actions/submitAnsArchievedAction";
  13. import { resultDetails } from "../../urls";
  14. import { Prompt } from 'react-router'
  15. import LoaderComponent from "../../Components/LoaderComponent/LoaderComponent";
  16. import { Redirect } from "react-router-dom";
  17. const useStyles = makeStyles((theme) => ({
  18. button: {
  19. margin: theme.spacing(1),
  20. margin: "50px",
  21. },
  22. }));
  23.  
  24.  
  25. const MCQExam = () => {
  26.  
  27. // console.log('paghes ','MCQExam')
  28.  
  29. const [purchased,setPurchased] = React.useState(false);
  30.  
  31. const history = useHistory();
  32.  
  33. const auth = useSelector(state =>state.auth);
  34.  
  35. let purchasedExamList = [];
  36.  
  37. if(auth!=undefined && auth.userId!=undefined && auth.userId.user_info!=undefined && auth.userId.user_info.billing_profile!=undefined )
  38. {
  39. if(auth.userId.user_info.billing_profile.purchased_exams!=undefined)
  40. {
  41. purchasedExamList = auth.userId.user_info.billing_profile.purchased_exams;
  42. }
  43. }
  44.  
  45.  
  46. React.useEffect(()=>
  47. {
  48. const check = purchasedExamList.some(ex=>ex==id);
  49.  
  50. if(check){
  51.  
  52. setPurchased(true)
  53.  
  54.  
  55.  
  56. }
  57.  
  58. return ()=>
  59. {
  60. setPurchased(false);
  61. }
  62.  
  63. },[purchasedExamList.length])
  64.  
  65.  
  66.  
  67. const [submitState,setSubmitState] = React.useState(true);
  68.  
  69. const { id, program,examState } = useParams();
  70.  
  71. let exams={};
  72.  
  73. const archievedExamsList=useSelector((state) =>state.archievedData.exams);
  74. const initDataExamList=useSelector((state) =>state.initData.exams);
  75.  
  76.  
  77. let allQuestions;
  78.  
  79. const allQuestionsinitData = useSelector((state) => state.initData.questions);
  80.  
  81. const allQuestionsArchievedData=useSelector((state)=>state.archievedData.questions);
  82.  
  83. if(examState==archieved)
  84. {
  85. exams=archievedExamsList.filter((ex) => ex.id == id)[0];
  86. allQuestions=allQuestionsArchievedData
  87. }
  88.  
  89. else {
  90. exams=initDataExamList.filter((ex) => ex.id == id)[0];
  91. allQuestions=allQuestionsinitData;
  92. }
  93.  
  94.  
  95.  
  96. const questionsId = exams!=undefined ? exams.questions : Infinity;
  97.  
  98. const classes = useStyles();
  99.  
  100. const dispatch = useDispatch();
  101.  
  102.  
  103.  
  104. const mcqAns = useSelector((state) => state.mcqAns);
  105.  
  106. const token = useSelector((state) => state.auth.userId.token);
  107.  
  108.  
  109.  
  110. const answerSubmit = () => {
  111.  
  112. if(submitState){
  113. setSubmitState(false);
  114. setPurchased(false);
  115.  
  116.  
  117. if(examState==archieved)
  118. dispatch(submitAnsArcievedAction(token, { ...mcqAns }));
  119.  
  120. else
  121. dispatch(submitAnsAction(token, { ...mcqAns }));
  122.  
  123. history.replace(resultDetails(id,mainpage,RESPONSE));
  124. }
  125. else
  126. {
  127. return <Redirect to='/'></Redirect>;
  128. }
  129.  
  130.  
  131. };
  132.  
  133. const [timeLeft, setTimeLeft] = useState(exams!==undefined ? exams.duration/1000 : 1000); // remove 5000
  134.  
  135. let intervalId;
  136.  
  137. useEffect(()=>{
  138.  
  139.  
  140.  
  141. intervalId = setInterval(() => {
  142.  
  143.  
  144. setTimeLeft(timeLeft=>timeLeft - 1);
  145. }, 1000);
  146.  
  147.  
  148.  
  149.  
  150.  
  151. return ()=>{
  152.  
  153. clearInterval(intervalId);
  154.  
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. },[])
  162.  
  163. useEffect(() => {
  164.  
  165. if (timeLeft<=0) {
  166.  
  167. answerSubmit();
  168.  
  169.  
  170. return;
  171. }
  172.  
  173.  
  174.  
  175. }, [timeLeft]);
  176.  
  177.  
  178.  
  179.  
  180. useEffect(() => {
  181. window.addEventListener("beforeunload", alertUser);
  182.  
  183.  
  184. return () => {
  185. window.removeEventListener("beforeunload", alertUser);
  186.  
  187. };
  188. }, []);
  189.  
  190.  
  191. const alertUser = (e) => {
  192.  
  193. e.preventDefault();
  194. e.returnValue = "";
  195.  
  196. };
  197.  
  198.  
  199. const getTimeFormat = () => {
  200.  
  201. let left = timeLeft;
  202.  
  203. const hours = Math.floor(left / 3600).toString();
  204. left %= 3600;
  205. const minutes = (left / 60) < 10 ? `0${Math.floor(left / 60).toString()}` : Math.floor(left / 60).toString();
  206. left %= 60;
  207. const seconds = left < 10 ? `0${left.toString()}` : left.toString();
  208. return `${hours}:${minutes}:${seconds}`;
  209.  
  210.  
  211.  
  212. };
  213.  
  214. //here we set out exam id using dispatch
  215.  
  216. let numbering = 0;
  217.  
  218.  
  219.  
  220.  
  221.  
  222. return (
  223. purchased==false?<LoaderComponent> </LoaderComponent> :
  224. <div style={{margin:'5px'}}>
  225.  
  226.  
  227.  
  228. <div style={{paddingTop:'10px'}}>
  229. <h5 style={{ alignItems: "center", margin: "5px", textAlign: "center" }}>
  230. Time Left : {getTimeFormat()}
  231. </h5>
  232. </div>
  233.  
  234. {questionsId.map((qid) => (
  235. <QuestionCard
  236. numbering={++numbering}
  237. key={qid}
  238. question={allQuestions[qid].question}
  239. options={allQuestions[qid].options}
  240. qid={qid}
  241. />
  242. ))}
  243.  
  244. <div className='container text-center'>
  245. <Button
  246. onClick={answerSubmit}
  247. variant="contained"
  248. color="primary"
  249. size="large"
  250. className={classes.button}
  251. style={{
  252.  
  253.  
  254. textAlign : 'center',
  255. alignItems : 'center',
  256. justify : 'center' }}
  257. startIcon={<SaveIcon />}
  258. >
  259. Submit
  260. </Button>
  261.  
  262. </div>
  263. </div>
  264.  
  265. );
  266. };
  267.  
  268. export default MCQExam;
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement