Skip to content

Commit

Permalink
improvement: correctly mapping multi choice questions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Mar 17, 2024
1 parent e75234e commit 9b1317e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/components/AttemptHistoryTable/AttemptHistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function AttemptHistoryTable({ quizId }: AttemptHistoryTableProps) {
if (!userId) return null;

return (
<div className="flex w-full flex-col">
<div className="container flex flex-col">
<section className="flex items-center">
<Typography variant="h2" className="text-xl font-medium">
Attempt History
Expand Down
39 changes: 13 additions & 26 deletions src/components/QuizAttempt/QuizAttempt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { toast } from "react-toastify";
import useBreakpoint from "use-breakpoint";
import { QuestionTypeChip } from "@components/QuestionTypeChip/QuestionTypeChip";
import { QuizAttemptCancelDialog } from "@components/QuizAttemptCancelDialog/QuizAttemptCancelDialog";
import { QuestionType, QuizByIdQuery } from "@generated/graphql.ts";
import { QuizByIdQuery } from "@generated/graphql.ts";
import { useAttemptStore } from "@state/attemptStore.ts";
import { useUserStore } from "@state/userStore.ts";
import { BREAKPOINTS } from "@utils/constants.ts";
Expand Down Expand Up @@ -117,31 +117,18 @@ export function QuizAttempt({ quiz }: QuizAttemptProps) {
</Typography>

<div className="mt-10 flex flex-wrap gap-5 sm:gap-7">
{question.type === QuestionType.Single &&
question.options.map(({ text, chosen }) => (
<Button
key={text}
fullWidth
variant={chosen ? "filled" : "outlined"}
color="blue-gray"
onClick={() => setQuestionResponse(text)}
>
{text}
</Button>
))}

{question.type === QuestionType.Multi &&
question.options.map(({ text }) => (
<Button
key={text}
fullWidth
variant="outlined"
color="blue-gray"
onClick={() => setQuestionResponse(text)}
>
{text}
</Button>
))}
{question.options.map(({ text, chosen }) => (
<Button
key={text}
fullWidth
variant={chosen ? "filled" : "outlined"}
color="blue-gray"
className="outline-none focus:ring-0"
onClick={() => setQuestionResponse(text)}
>
{text}
</Button>
))}
</div>
<div className="mt-12 flex w-full justify-between">
<Button variant="outlined" color="gray" disabled={currentQuestion === 0} onClick={goToPreviousQuestion}>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Quiz/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export function Component() {
</section>

{!isAttemptRunning && (
<div className="mt-6 block w-full md:hidden">
<hr className="border-secondaryShades.5 mb-4 w-full" />
<div className="container mt-6 flex flex-col md:hidden">
<hr className="border-secondaryShades.5 mb-4 flex w-full" />
<AttemptHistoryTable quizId={quiz?.id || ""} />
</div>
)}
Expand Down
20 changes: 13 additions & 7 deletions src/state/attemptStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,29 @@ export const useAttemptStore = create<AttemptState>()(
return {
...question,
options: question.options.map(option => {
if (option.text === chosenOption) {
return {
...option,
chosen: true,
};
}
const chosen = option.text === chosenOption;
return {
...option,
chosen: false,
chosen: chosen,
};
}),
};
}

if (isMultipleChoice) {
return {
...question,
options: question.options.map(option => {
const chosen = option.text === chosenOption;
return {
...option,
chosen: chosen ? !option.chosen : option.chosen,
};
}),
};
}
}

return question;
});

Expand Down

0 comments on commit 9b1317e

Please sign in to comment.