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

151 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import {
2024-02-02 14:35:02 +00:00
Box,
Typography,
RadioGroup,
FormControlLabel,
Radio,
useTheme,
useMediaQuery,
2023-12-16 14:55:56 +00:00
} from "@mui/material";
2024-01-19 11:46:17 +00:00
import { useQuizViewStore, updateAnswer, deleteAnswer } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import type { QuizQuestionImages } from "../../../model/questionTypes/images";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2024-02-02 14:35:02 +00:00
import { useQuizData } from "@utils/hooks/useQuizData";
2023-12-16 14:55:56 +00:00
type ImagesProps = {
2024-02-02 14:35:02 +00:00
currentQuestion: QuizQuestionImages;
2023-12-16 14:55:56 +00:00
};
export const Images = ({ currentQuestion }: ImagesProps) => {
2024-02-02 14:35:02 +00:00
const { settings } = useQuizData();
const { answers } = useQuizViewStore();
const theme = useTheme();
const { answer } =
answers.find(
({ questionId }) => questionId === currentQuestion.id
) ?? {};
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
const isMobile = useMediaQuery(theme.breakpoints.down(500));
2023-12-16 14:55:56 +00:00
2024-02-02 14:35:02 +00:00
return (
<Box>
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
<RadioGroup
name={currentQuestion.id}
value={currentQuestion.content.variants.findIndex(
({ id }) => answer === id
)}
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%",
}}
>
{currentQuestion.content.variants.map((variant, index) => (
<Box
key={index}
sx={{
cursor: "pointer",
borderRadius: "5px",
border: `1px solid`,
borderColor: answer === variant.id ? theme.palette.primary.main : "#9A9AAF",
}}
onClick={async (event) => {
event.preventDefault();
2024-02-02 14:35:02 +00:00
try {
2024-02-02 14:35:02 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: `${currentQuestion.content.variants[index].answer} <img style="width:100%; max-width:250px; max-height:250px" src="${currentQuestion.content.variants[index].extendedText}"/>`,
qid: settings.qid
});
2024-02-02 14:35:02 +00:00
updateAnswer(
currentQuestion.id,
currentQuestion.content.variants[index].id
);
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
2024-02-02 14:35:02 +00:00
if (answer === currentQuestion.content.variants[index].id) {
deleteAnswer(currentQuestion.id);
try {
2024-02-02 14:35:02 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: "",
qid: settings.qid
});
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
}
}}
>
<Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
<Box sx={{ width: "100%", height: "300px" }}>
{variant.extendedText && (
<img
src={variant.extendedText}
alt=""
style={{
display: "block",
width: "100%",
height: "100%",
objectFit: "cover",
}}
/>
)}
</Box>
</Box>
<FormControlLabel
key={variant.id}
sx={{
textAlign: "center",
color: theme.palette.text.primary,
marginTop: "10px",
marginLeft: 0,
padding: "10px",
"& .MuiFormControlLabel-label": {
wordBreak: "break-word",
},
}}
value={index}
control={
<Radio checkedIcon={<RadioCheck color={theme.palette.primary.main} />} icon={<RadioIcon />} />
}
label={variant.answer}
/>
</Box>
))}
2023-12-16 14:55:56 +00:00
</Box>
2024-02-02 14:35:02 +00:00
</RadioGroup>
2023-12-16 14:55:56 +00:00
</Box>
2024-02-02 14:35:02 +00:00
);
2023-12-29 00:58:19 +00:00
2024-01-19 11:46:17 +00:00
};