2024-04-23 14:45:49 +00:00
|
|
|
import { Select as SelectComponent } from "@/components/ViewPublicationPage/tools/Select";
|
2024-05-31 17:56:17 +00:00
|
|
|
import { useQuizSettings } from "@contexts/QuizDataContext";
|
2024-04-23 14:45:49 +00:00
|
|
|
import type { QuizQuestionSelect } from "@model/questionTypes/select";
|
2024-06-29 09:32:16 +00:00
|
|
|
import { Box, Typography, useTheme } from "@mui/material";
|
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
type SelectProps = {
|
|
|
|
currentQuestion: QuizQuestionSelect;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Select = ({ currentQuestion }: SelectProps) => {
|
2024-06-29 09:32:16 +00:00
|
|
|
const { settings } = useQuizSettings();
|
2024-04-23 14:45:49 +00:00
|
|
|
const { updateAnswer, deleteAnswer } = useQuizViewStore((state) => state);
|
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
|
|
|
const theme = useTheme();
|
2024-05-31 16:41:18 +00:00
|
|
|
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const sendSelectedAnswer = async (value: number) => {
|
|
|
|
if (value < 0) {
|
|
|
|
deleteAnswer(currentQuestion.id);
|
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
return;
|
2024-04-23 14:45:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
updateAnswer(currentQuestion.id, String(value), 0);
|
2024-04-23 14:45:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
2024-06-29 09:32:16 +00:00
|
|
|
<Typography
|
|
|
|
variant="h5"
|
|
|
|
color={theme.palette.text.primary}
|
|
|
|
sx={{ wordBreak: "break-word" }}
|
|
|
|
>
|
2024-04-23 14:45:49 +00:00
|
|
|
{currentQuestion.title}
|
|
|
|
</Typography>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
width: "100%",
|
|
|
|
marginTop: "20px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SelectComponent
|
|
|
|
placeholder={currentQuestion.content.default}
|
|
|
|
activeItemIndex={answer ? Number(answer) : -1}
|
|
|
|
items={currentQuestion.content.variants.map(({ answer }) => answer)}
|
|
|
|
colorMain={theme.palette.primary.main}
|
|
|
|
sx={{
|
|
|
|
"& .MuiSelect-select.MuiSelect-outlined": { zIndex: 1 },
|
|
|
|
"& .MuiOutlinedInput-notchedOutline": {
|
|
|
|
background: settings.cfg.design
|
|
|
|
? quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#F2F3F7"
|
|
|
|
: "rgba(255,255,255, 0.3)"
|
|
|
|
: "transparent",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
onChange={(_, value) => sendSelectedAnswer(value)}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|