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

Changed output of questions on EditQuiz page #3143

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/components/question/Question.styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import palette from '~/styles/app-theme/app.pallete'
import { theme } from '~/styles/app-theme/custom-mui.styles'
import { TypographyVariantEnum } from '~/types'

const actionIconWrapper = {
Expand Down Expand Up @@ -78,6 +79,15 @@ export const styles = {
justifyContent: 'space-between',
alignItems: 'center'
},
openAnswer: {
padding: theme.spacing(1, 0)
},
singleAnswer: {
padding: '9px 0px 9px 0px'
Made1ra marked this conversation as resolved.
Show resolved Hide resolved
},
singleAnswers: {
ml: theme.spacing(3)
},
moreIcon: {
fontSize: '20px'
},
Expand Down
38 changes: 30 additions & 8 deletions src/components/question/Question.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Dispatch, FC, SetStateAction } from 'react'
import { useTranslation } from 'react-i18next'
import { MenuItem, SxProps } from '@mui/material'
import Box from '@mui/material/Box'
Expand All @@ -17,31 +16,37 @@ import IconTitleDescription from '~/components/icon-title-description/IconTitleD
import AppChip from '~/components/app-chip/AppChip'
import DragHandle from '~/components/drag-handle/DragHandle'
import { IconButton } from '~/design-system/components/icon-button/IconButton'
import RadioButton from '~/design-system/components/radio-button/RadioButton'

import {
ColorEnum,
Question as QuestionInterface,
QuestionTypesEnum,
TableActionFunc
} from '~/types'
import { styles } from '~/components/question/Question.styles'
import { spliceSx } from '~/utils/helper-functions'
import { determineQuestionType } from '~/components/question-editor/QuestionEditor.constants'

interface QuestionProps {
question: QuestionInterface
setQuestions: Dispatch<SetStateAction<QuestionInterface[]>>
setEditableItemId: Dispatch<SetStateAction<string>>
setQuestions: React.Dispatch<React.SetStateAction<QuestionInterface[]>>
setEditableItemId: React.Dispatch<React.SetStateAction<string>>
type?: QuestionTypesEnum
sx?: SxProps
}

const Question: FC<QuestionProps> = ({
const Question: React.FC<QuestionProps> = ({
question,
setQuestions,
setEditableItemId,
type = QuestionTypesEnum.MultipleChoice,
sx = {}
}) => {
const { t } = useTranslation()
const { openMenu, renderMenu, closeMenu } = useMenu()

const { isMultipleChoice, isSingleChoice, isOpenAnswer } =
determineQuestionType(type)
const onAction = async (actionFunc: TableActionFunc) => {
closeMenu()
await actionFunc(question._id)
Expand Down Expand Up @@ -83,15 +88,24 @@ const Question: FC<QuestionProps> = ({
))

const answersList = question.answers.map((answer) => (
<Box key={answer.text} sx={styles.answer}>
<Box
key={answer.text}
sx={[styles.answer, isSingleChoice && styles.singleAnswer]}
>
<FormControlLabel
checked={answer.isCorrect}
control={<Checkbox />}
control={isMultipleChoice ? <Checkbox /> : <RadioButton label='' />}
label={answer.text}
/>
{answer.isCorrect && <CheckIcon sx={styles.checkIcon} />}
</Box>
))
const openAnswersList = question.answers.map((answer) => (
<Box key={answer.text} sx={spliceSx(styles.answer, styles.openAnswer)}>
<Typography>{answer.text}</Typography>
{answer.isCorrect && <CheckIcon sx={styles.checkIcon} />}
</Box>
))

return (
<Box sx={spliceSx(styles.root, sx)}>
Expand Down Expand Up @@ -124,7 +138,15 @@ const Question: FC<QuestionProps> = ({
<Divider sx={styles.divider} />
<Box sx={styles.questionBody}>
<Typography sx={styles.questionText}>{question.text}</Typography>
<Box sx={styles.answers}>{answersList}</Box>
<Box
sx={spliceSx(
styles.answers,
isSingleChoice ? styles.singleAnswers : undefined
)}
>
{!isOpenAnswer && answersList}
{isOpenAnswer && openAnswersList}
</Box>
</Box>
</Box>
)
Expand Down
1 change: 1 addition & 0 deletions src/containers/questions-list/QuestionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const QuestionsList: FC<QuestionsListProps> = ({ items, setItems }) => {
question={item}
setEditableItemId={setEditableItemId}
setQuestions={setItems}
type={item.type}
/>
)

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/components/question/Question.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ describe('Question', () => {

expect(setQuestionsMock).toHaveBeenCalled()
})
it('should check correct answer checkbox', () => {
const correctAnswer = mockedQuestion.answers.find((a) => a.isCorrect)
const checkbox = screen.getByLabelText(correctAnswer.text)

expect(checkbox).toBeChecked()
})
it('should not check incorrect answer checkbox', () => {
const incorrectAnswer = mockedQuestion.answers.find((a) => !a.isCorrect)
const checkbox = screen.getByLabelText(incorrectAnswer.text)

expect(checkbox).not.toBeChecked()
})
})
Loading