From f090699e9340a3e95b4330832595e4bcb69cb5a5 Mon Sep 17 00:00:00 2001 From: Muhammed Rameez <109953720+rameez1010@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:33:32 +0530 Subject: [PATCH] feat: Give Color To List In All Questions (#21) --- .../src/machines/MCQ/actions/index.ts | 16 ++++++++- .../src/machines/MCQ/machine.ts | 6 +++- .../src/machines/MCQ/types/context.ts | 7 +++- .../src/modules/mcq-exams/AllQuestions.tsx | 34 ++++++++++++++++++- .../src/modules/mcq-exams/McqExamPage.tsx | 7 +++- 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/audire/audire-mobile-app/src/machines/MCQ/actions/index.ts b/audire/audire-mobile-app/src/machines/MCQ/actions/index.ts index ab70d16..b710f91 100644 --- a/audire/audire-mobile-app/src/machines/MCQ/actions/index.ts +++ b/audire/audire-mobile-app/src/machines/MCQ/actions/index.ts @@ -88,6 +88,7 @@ export function setStartExam( numCorrectAnswers: 0, numUnattended: 0, numWrongAnswers: 0, + visited: {}, }; } @@ -96,7 +97,7 @@ export function calculateMarks(context: MCQMachineContext): MCQMachineContext { numWrong = 0, numUnattended = 0; const score = context.questions.reduce((mark, question, idx) => { - if (context.markAnswer[idx] !== undefined) { + if (context.markAnswer[idx] >= 0) { if (context.markAnswer[idx] === question.correctOption) { mark += 4; numCorrect += 1; @@ -119,3 +120,16 @@ export function calculateMarks(context: MCQMachineContext): MCQMachineContext { numUnattended: numUnattended, }; } + +export function markQuestionVisited( + context: MCQMachineContext +): MCQMachineContext { + return { + ...context, + visited: { + ...context.visited, + + [context.currentQuestionIdx]: true, + }, + }; +} diff --git a/audire/audire-mobile-app/src/machines/MCQ/machine.ts b/audire/audire-mobile-app/src/machines/MCQ/machine.ts index 9c56b91..fa5748c 100644 --- a/audire/audire-mobile-app/src/machines/MCQ/machine.ts +++ b/audire/audire-mobile-app/src/machines/MCQ/machine.ts @@ -8,6 +8,7 @@ import { setMarkToRevisit, setStartExam, calculateMarks, + markQuestionVisited, } from './actions'; import { MCQMachineEvents } from './events'; import { hasNextQuestion, hasPrevQuestion } from './guards'; @@ -23,7 +24,7 @@ export const MCQMachine = createMachine( context: {} as MCQMachineContext, states: { 'attending-question': { - entry: ['calculateMarks'], + entry: ['calculateMarks', 'markQuestionVisited'], on: { SUBMIT_EXAM: { actions: ['calculateMarks'], @@ -119,6 +120,9 @@ export const MCQMachine = createMachine( calculateMarks: assign(({ context }) => { return calculateMarks(context); }), + markQuestionVisited: assign(({ context }) => { + return markQuestionVisited(context); + }), }, guards: { hasNextQuestion, diff --git a/audire/audire-mobile-app/src/machines/MCQ/types/context.ts b/audire/audire-mobile-app/src/machines/MCQ/types/context.ts index 40f13a6..af6bbe3 100644 --- a/audire/audire-mobile-app/src/machines/MCQ/types/context.ts +++ b/audire/audire-mobile-app/src/machines/MCQ/types/context.ts @@ -6,7 +6,11 @@ export type MarkAnswer = { }; export type MarkToRevisit = { - [questionId: string]: boolean; + [questionIdx: string]: boolean; +}; + +export type VisitedQuestions = { + [questionIdx: number]: boolean; }; export type MCQMachineContext = { @@ -15,6 +19,7 @@ export type MCQMachineContext = { currentQuestionIdx: number; markAnswer: MarkAnswer; markToRevisit: MarkToRevisit; + visited: VisitedQuestions; noOfQuestionToRevisit: number; // On Submit the following are filled diff --git a/audire/audire-mobile-app/src/modules/mcq-exams/AllQuestions.tsx b/audire/audire-mobile-app/src/modules/mcq-exams/AllQuestions.tsx index c73534f..581438e 100644 --- a/audire/audire-mobile-app/src/modules/mcq-exams/AllQuestions.tsx +++ b/audire/audire-mobile-app/src/modules/mcq-exams/AllQuestions.tsx @@ -14,19 +14,32 @@ import { } from '@gluestack-ui/themed'; import { TouchableOpacity } from 'react-native'; import { McqQuestion } from '@learning-app/syllabus'; +import { + MarkAnswer, + MarkToRevisit, + VisitedQuestions, +} from '../../machines/MCQ/types/context'; type AllQuestionsProps = ComponentProps & { isOpen: boolean; onClose: () => void; + currentQuestionIdx: number; questions: McqQuestion[]; onJumpToQuestion: (questionIdx: number) => void; + markedToRevisit: MarkToRevisit; + markAnswer: MarkAnswer; + visited: VisitedQuestions; }; const AllQuestions: FC = ({ isOpen, onClose, + currentQuestionIdx, questions, onJumpToQuestion, + markedToRevisit, + markAnswer, + visited, }) => { const statusData = [ { color: '#8FC37D', text: 'Answered' }, @@ -36,6 +49,23 @@ const AllQuestions: FC = ({ { color: '#79D3E7', text: 'To Revisit' }, { color: 'white', text: 'Not visited' }, ]; + const getQuestionColor = (index: number) => { + if (markedToRevisit[index] === true) { + // To Revisit + return '#79D3E7'; + } else if (markAnswer[index] >= 0) { + // Answer Marked + return '#8FC37D'; + } else if (visited[index] === true) { + // Unanswered + return '#F07A7A'; + } else { + return '$white'; + } + }; + + console.log('bbbbb', markedToRevisit); + console.log('ccc', markAnswer); const ref = React.useRef(null); return ( = ({ }} > = ({ questions }) => { {showQuestionsDialog && ( setShowQuestionsDialog(false)} questions={state.context.questions} onJumpToQuestion={(idx) => @@ -100,6 +101,9 @@ const McqExamPage: FC = ({ questions }) => { questionIdx: idx, }) } + markedToRevisit={state.context.markToRevisit} + markAnswer={state.context.markAnswer} + visited={state.context.visited} /> )} @@ -187,7 +191,8 @@ const McqExamPage: FC = ({ questions }) => { onPress={() => send({ type: 'MARK_ANSWER', - selectedOption: option.id, + selectedOption: + markAnswer[currentQuestionIdx] === option.id ? -1 : option.id, }) } //MarkAnswer >