frontPanel/src/pages/ViewPublicationPage/questions/Images.tsx

118 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-12-01 13:48:25 +00:00
import { useEffect } from "react";
2023-11-30 17:39:57 +00:00
import {
Box,
Typography,
RadioGroup,
FormControlLabel,
Radio,
useTheme,
useMediaQuery,
} from "@mui/material";
2023-12-01 13:48:25 +00:00
import { useQuizViewStore, updateAnswer } from "@root/quizView";
2023-11-30 17:39:57 +00:00
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import type { QuizQuestionImages } from "../../../model/questionTypes/images";
type ImagesProps = {
2023-12-01 13:48:25 +00:00
stepNumber: number;
2023-11-30 17:39:57 +00:00
question: QuizQuestionImages;
};
2023-12-01 13:48:25 +00:00
export const Images = ({ stepNumber, question }: ImagesProps) => {
const { answers } = useQuizViewStore();
2023-11-30 17:39:57 +00:00
const theme = useTheme();
2023-12-01 13:48:25 +00:00
const { answer } = answers.find(({ step }) => step === stepNumber) ?? {};
2023-11-30 17:39:57 +00:00
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
const isMobile = useMediaQuery(theme.breakpoints.down(500));
2023-12-01 13:48:25 +00:00
useEffect(() => {
if (!answer) {
updateAnswer(stepNumber, question.content.variants[0].id);
}
}, []);
2023-11-30 17:39:57 +00:00
return (
<Box>
<Typography variant="h5">{question.title}</Typography>
<RadioGroup
name={question.id}
2023-12-01 13:48:25 +00:00
value={question.content.variants.findIndex(({ id }) => answer === id)}
onChange={({ target }) =>
updateAnswer(
stepNumber,
question.content.variants[Number(target.value)].id
)
}
2023-11-30 17:39:57 +00:00
sx={{
display: "flex",
flexWrap: "wrap",
flexDirection: "row",
justifyContent: "space-between",
marginTop: "20px",
}}
>
<Box
sx={{
display: "grid",
gap: "15px",
gridTemplateColumns: isTablet
? isMobile
? "repeat(1, 1fr)"
: "repeat(2, 1fr)"
: "repeat(3, 1fr)",
width: "100%",
}}
>
{question.content.variants.map(
({ id, answer, extendedText }, index) => (
<Box
key={index}
sx={{
borderRadius: "5px",
border: `1px solid ${theme.palette.grey2.main}`,
}}
>
<Box
sx={{ display: "flex", alignItems: "center", gap: "10px" }}
>
<Box sx={{ width: "100%", height: "300px" }}>
{extendedText && (
<img
src={extendedText}
alt=""
style={{
display: "block",
width: "100%",
height: "100%",
objectFit: "cover",
}}
/>
)}
</Box>
</Box>
<FormControlLabel
key={id}
sx={{
display: "block",
textAlign: "center",
color: theme.palette.grey2.main,
marginTop: "10px",
}}
value={index}
control={
<Radio checkedIcon={<RadioCheck />} icon={<RadioIcon />} />
}
label={answer}
/>
</Box>
)
)}
</Box>
</RadioGroup>
</Box>
);
};