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

153 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import {
Box,
Typography,
RadioGroup,
FormControlLabel,
Radio,
useTheme,
2023-12-29 00:58:19 +00:00
useMediaQuery
2023-12-16 14:55:56 +00:00
} from "@mui/material";
2023-12-29 00:58:19 +00:00
import { modes } from "../../../utils/themes/Publication/themePublication";
2023-12-16 14:55:56 +00:00
import gag from "./gag.png"
2023-12-16 14:55:56 +00:00
import { useQuestionsStore } from "@root/quizData/store"
import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView/store";
2023-12-16 14:55:56 +00:00
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import type { QuizQuestionVarImg } from "../../../model/questionTypes/varimg";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2023-12-16 14:55:56 +00:00
type VarimgProps = {
currentQuestion: QuizQuestionVarImg;
};
export const Varimg = ({ currentQuestion }: VarimgProps) => {
const { settings } = useQuestionsStore()
2023-12-16 14:55:56 +00:00
const { answers } = useQuizViewStore();
const theme = useTheme();
2023-12-29 00:58:19 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(650));
const mode = modes;
2023-12-16 14:55:56 +00:00
const { answer } =
answers.find(
({ questionId }) => questionId === currentQuestion.id
2023-12-16 14:55:56 +00:00
) ?? {};
const variant = currentQuestion.content.variants.find(
({ id }) => answer === id
);
return (
<Box>
2023-12-29 00:58:19 +00:00
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
<Box sx={{
display: "flex",
marginTop: "20px",
flexDirection: isMobile ? "column-reverse" : undefined,
gap: isMobile ? "30px" : undefined
}}>
2023-12-16 14:55:56 +00:00
<RadioGroup
2023-12-29 00:58:19 +00:00
name={currentQuestion.id}
2023-12-16 14:55:56 +00:00
value={currentQuestion.content.variants.findIndex(
({ id }) => answer === id
)}
sx={{
display: "flex",
flexWrap: "wrap",
flexDirection: "row",
justifyContent: "space-between",
flexBasis: "100%",
}}
>
2023-12-29 00:58:19 +00:00
<Box sx={{ display: "flex", flexDirection: "column", width: "100%", gap: isMobile ? "20px" : undefined }}>
2023-12-16 14:55:56 +00:00
{currentQuestion.content.variants.map((variant, index) => (
<FormControlLabel
key={variant.id}
sx={{
marginBottom: "15px",
borderRadius: "5px",
padding: "15px",
2023-12-29 00:58:19 +00:00
color: theme.palette.text.primary,
backgroundColor: mode[settings.cfg.theme] ? "white" : theme.palette.background.default,
border: `1px solid`,
borderColor: answer === variant.id ? theme.palette.primary.main : "#9A9AAF",
2023-12-16 14:55:56 +00:00
display: "flex",
2023-12-29 00:58:19 +00:00
margin: isMobile ? 0 : undefined,
2023-12-16 14:55:56 +00:00
}}
value={index}
onClick={async(event) => {
2023-12-16 14:55:56 +00:00
event.preventDefault();
2023-12-29 00:58:19 +00:00
try {
2023-12-29 00:58:19 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: currentQuestion.content.variants[index].id,
//@ts-ignore
qid: settings.qid
})
2023-12-29 00:58:19 +00:00
updateAnswer(
currentQuestion.id,
currentQuestion.content.variants[index].id
);
2023-12-29 00:58:19 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан")
}
2023-12-29 00:58:19 +00:00
2023-12-16 14:55:56 +00:00
if (answer === currentQuestion.content.variants[index].id) {
deleteAnswer(currentQuestion.id);
2023-12-16 14:55:56 +00:00
}
}}
control={
2023-12-29 00:58:19 +00:00
//@ts-ignore
<Radio checkedIcon={<RadioCheck color={theme.palette.primary.main}/>} icon={<RadioIcon />} />
2023-12-16 14:55:56 +00:00
}
label={variant.answer}
/>
))}
</Box>
</RadioGroup>
{/* {(variant?.extendedText || currentQuestion.content.back) && ( */}
<Box
sx={{
maxWidth: "450px",
width: "100%",
height: "450px",
border: "1px solid #9A9AAF",
borderRadius: "12px",
overflow: "hidden",
display: "flex",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#9A9AAF12",
color: "#9A9AAF"
}}
>
{
answer ?
<img
src={
variant?.extendedText || gag
}
2023-12-16 14:55:56 +00:00
style={{ width: "100%", height: "100%", objectFit: "cover" }}
alt=""
/>
:
2023-12-29 00:58:19 +00:00
(variant?.extendedText || isMobile ? ("Выберите вариант ответа ниже") : ("Выберите вариант ответа слева"))
}
</Box>
{/* )} */}
2023-12-16 14:55:56 +00:00
</Box>
</Box>
);
2023-12-29 00:58:19 +00:00
};