frontAnswerer/lib/components/ViewPublicationPage/questions/Images.tsx

163 lines
7.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,
FormControlLabel,
Radio,
RadioGroup,
Typography,
useTheme
2023-12-16 14:55:56 +00:00
} from "@mui/material";
import { deleteAnswer, updateAnswer, useQuizViewStore } from "@stores/quizView";
2023-12-16 14:55:56 +00:00
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import { sendAnswer } from "@api/quizRelase";
import { enqueueSnackbar } from "notistack";
import { useRootContainerSize } from "../../../contexts/RootContainerWidthContext";
import type { QuizQuestionImages } from "../../../model/questionTypes/images";
import { useQuizData } from "@contexts/QuizDataContext";
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-15 09:08:05 +00:00
const { quizId } = useQuizData();
2024-02-02 14:35:02 +00:00
const { answers } = useQuizViewStore();
const theme = useTheme();
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer;
const isTablet = useRootContainerSize() < 1000;
const isMobile = useRootContainerSize() < 500;
2023-12-16 14:55:56 +00:00
2024-02-02 14:35:02 +00:00
return (
<Box>
2024-02-15 09:08:05 +00:00
<Typography variant="h5" color={theme.palette.text.primary} sx={{ wordBreak: "break-word" }}>{currentQuestion.title}</Typography>
2024-02-02 14:35:02 +00:00
<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: quizId,
2024-02-02 14:35:02 +00:00
});
2024-02-02 14:35:02 +00:00
updateAnswer(
currentQuestion.id,
currentQuestion.content.variants[index].id,
currentQuestion.content.variants[index].points || 0
2024-02-02 14:35:02 +00:00
);
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: quizId,
2024-02-02 14:35:02 +00:00
});
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",
display: "flex",
alignItems:
variant.answer.length <= 60 ? "center" : "flex-start",
position: "relative",
height: "80px",
2024-02-02 14:35:02 +00:00
"& .MuiFormControlLabel-label": {
wordBreak: "break-word",
height: variant.answer.length <= 60 ? undefined : "60px",
overflow: "auto",
paddingLeft: "45px",
2024-02-15 08:43:02 +00:00
"&::-webkit-scrollbar": {
width: "4px",
},
"&::-webkit-scrollbar-thumb": {
backgroundColor: "#b8babf",
}
2024-02-02 14:35:02 +00:00
},
}}
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
};