2024-05-31 17:56:17 +00:00
|
|
|
import { useQuizSettings } from "@contexts/QuizDataContext";
|
2024-04-23 14:45:49 +00:00
|
|
|
import { CheckboxIcon } from "@icons/Checkbox";
|
2024-06-29 09:32:16 +00:00
|
|
|
import type { QuestionVariant } from "@model/questionTypes/shared";
|
2024-09-12 11:14:54 +00:00
|
|
|
import {
|
|
|
|
Checkbox,
|
|
|
|
FormControlLabel,
|
|
|
|
Input,
|
|
|
|
TextField as MuiTextField,
|
|
|
|
Radio,
|
|
|
|
TextFieldProps,
|
|
|
|
TextareaAutosize,
|
2024-09-26 19:51:46 +00:00
|
|
|
Typography,
|
2024-09-12 11:14:54 +00:00
|
|
|
useTheme,
|
|
|
|
} from "@mui/material";
|
2024-06-29 09:32:16 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
2024-04-23 14:45:49 +00:00
|
|
|
import RadioCheck from "@ui_kit/RadioCheck";
|
|
|
|
import RadioIcon from "@ui_kit/RadioIcon";
|
2024-06-29 09:32:16 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2024-04-23 14:45:49 +00:00
|
|
|
import type { FC, MouseEvent } from "react";
|
2025-04-20 15:16:22 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const TextField = MuiTextField as unknown as FC<TextFieldProps>;
|
|
|
|
|
2024-09-12 11:14:54 +00:00
|
|
|
interface OwnInputProps {
|
|
|
|
questionId: string;
|
|
|
|
variant: QuestionVariant;
|
|
|
|
largeCheck: boolean;
|
|
|
|
ownPlaceholder: string;
|
|
|
|
}
|
2024-09-12 15:55:25 +00:00
|
|
|
const OwnInput = ({ questionId, variant, largeCheck, ownPlaceholder }: OwnInputProps) => {
|
2024-09-12 11:14:54 +00:00
|
|
|
const theme = useTheme();
|
2025-04-20 15:16:22 +00:00
|
|
|
|
2024-09-12 11:14:54 +00:00
|
|
|
const ownVariants = useQuizViewStore((state) => state.ownVariants);
|
|
|
|
const { updateOwnVariant } = useQuizViewStore((state) => state);
|
|
|
|
|
|
|
|
const ownAnswer = ownVariants[ownVariants.findIndex((v) => v.id === variant.id)]?.variant.answer || "";
|
2024-12-22 11:49:56 +00:00
|
|
|
|
2024-09-12 11:14:54 +00:00
|
|
|
return largeCheck ? (
|
|
|
|
<TextareaAutosize
|
2024-10-08 20:14:02 +00:00
|
|
|
placeholder={ownPlaceholder || "|"}
|
2024-09-12 11:14:54 +00:00
|
|
|
style={{
|
|
|
|
resize: "none",
|
|
|
|
width: "100%",
|
|
|
|
fontSize: "16px",
|
2024-11-24 23:42:33 +00:00
|
|
|
color: ownAnswer.length === 0 ? "ownPlaceholder" : theme.palette.text.primary,
|
2024-09-12 11:14:54 +00:00
|
|
|
letterSpacing: "-0.4px",
|
|
|
|
wordSpacing: "-3px",
|
|
|
|
outline: "0px none",
|
2024-10-08 20:14:02 +00:00
|
|
|
backgroundColor: "inherit",
|
|
|
|
border: "none",
|
2024-09-25 16:52:22 +00:00
|
|
|
//@ts-ignore
|
|
|
|
"&::-webkit-scrollbar": {
|
|
|
|
width: "4px",
|
|
|
|
},
|
|
|
|
"&::-webkit-scrollbar-thumb": {
|
2024-09-26 19:12:11 +00:00
|
|
|
backgroundColor: theme.palette.primary.main,
|
2024-09-25 16:52:22 +00:00
|
|
|
},
|
2024-09-26 19:12:11 +00:00
|
|
|
scrollbarColor: theme.palette.primary.main,
|
2024-09-12 11:14:54 +00:00
|
|
|
}}
|
|
|
|
value={ownAnswer}
|
|
|
|
onClick={(e: React.MouseEvent<HTMLTextAreaElement>) => e.stopPropagation()}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
updateOwnVariant(variant.id, e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Input
|
2024-10-08 20:14:02 +00:00
|
|
|
placeholder={ownPlaceholder || "|"}
|
2024-09-12 11:14:54 +00:00
|
|
|
sx={{
|
2024-10-08 20:14:02 +00:00
|
|
|
backgroundColor: "inherit",
|
2024-09-12 11:14:54 +00:00
|
|
|
width: "100%",
|
|
|
|
fontSize: "18px",
|
2024-11-24 23:42:33 +00:00
|
|
|
color: ownAnswer.length === 0 ? "ownPlaceholder" : theme.palette.text.primary,
|
2024-09-12 11:14:54 +00:00
|
|
|
}}
|
|
|
|
value={ownAnswer}
|
|
|
|
disableUnderline
|
|
|
|
onClick={(e: React.MouseEvent<HTMLElement>) => e.stopPropagation()}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
updateOwnVariant(variant.id, e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-04-23 14:45:49 +00:00
|
|
|
export const VariantItem = ({
|
2024-06-29 09:32:16 +00:00
|
|
|
questionId,
|
|
|
|
isMulti,
|
2024-04-23 14:45:49 +00:00
|
|
|
variant,
|
|
|
|
answer,
|
|
|
|
index,
|
|
|
|
own = false,
|
2024-09-12 11:14:54 +00:00
|
|
|
questionLargeCheck,
|
|
|
|
ownPlaceholder,
|
2024-04-23 14:45:49 +00:00
|
|
|
}: {
|
2024-06-29 09:32:16 +00:00
|
|
|
isMulti: boolean;
|
|
|
|
questionId: string;
|
2024-04-23 14:45:49 +00:00
|
|
|
variant: QuestionVariant;
|
|
|
|
answer: string | string[] | undefined;
|
|
|
|
index: number;
|
2024-09-12 15:55:25 +00:00
|
|
|
own: boolean;
|
2024-09-12 11:14:54 +00:00
|
|
|
questionLargeCheck: boolean;
|
|
|
|
ownPlaceholder: string;
|
2024-04-23 14:45:49 +00:00
|
|
|
}) => {
|
2024-06-29 09:32:16 +00:00
|
|
|
const { settings } = useQuizSettings();
|
2024-04-23 14:45:49 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const { updateAnswer, deleteAnswer } = useQuizViewStore((state) => state);
|
2025-04-20 15:16:22 +00:00
|
|
|
const { t } = useTranslation();
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const sendVariant = async (event: MouseEvent<HTMLLabelElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
const variantId = variant.id;
|
2024-04-23 14:45:49 +00:00
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
if (isMulti) {
|
2024-04-23 14:45:49 +00:00
|
|
|
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
return updateAnswer(
|
|
|
|
questionId,
|
|
|
|
currentAnswer.includes(variantId)
|
|
|
|
? currentAnswer?.filter((item) => item !== variantId)
|
|
|
|
: [...currentAnswer, variantId],
|
|
|
|
variant.points || 0
|
2024-04-23 14:45:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-29 09:32:16 +00:00
|
|
|
updateAnswer(questionId, variantId, answer === variantId ? 0 : variant.points || 0);
|
|
|
|
|
2024-04-23 14:45:49 +00:00
|
|
|
if (answer === variantId) {
|
2024-06-29 09:32:16 +00:00
|
|
|
deleteAnswer(questionId);
|
2024-04-23 14:45:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControlLabel
|
|
|
|
key={variant.id}
|
|
|
|
sx={{
|
2024-09-26 19:51:46 +00:00
|
|
|
position: "relative",
|
2024-04-23 14:45:49 +00:00
|
|
|
margin: "0",
|
2024-09-26 19:51:46 +00:00
|
|
|
mt: own ? "10px" : "0",
|
2024-04-23 14:45:49 +00:00
|
|
|
borderRadius: "12px",
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
padding: "15px",
|
|
|
|
border: `1px solid`,
|
2024-05-31 16:41:18 +00:00
|
|
|
borderColor: answer === variant.id ? theme.palette.primary.main : "#9A9AAF",
|
2024-04-23 14:45:49 +00:00
|
|
|
backgroundColor: settings.cfg.design
|
|
|
|
? quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#FFFFFF"
|
|
|
|
: "rgba(255,255,255, 0.3)"
|
|
|
|
: quizThemes[settings.cfg.theme].isLight
|
2024-05-31 16:41:18 +00:00
|
|
|
? "white"
|
|
|
|
: theme.palette.background.default,
|
2024-04-23 14:45:49 +00:00
|
|
|
display: "flex",
|
|
|
|
maxWidth: "685px",
|
|
|
|
maxHeight: "85px",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
width: "100%",
|
|
|
|
"&:hover": { borderColor: theme.palette.primary.main },
|
|
|
|
"&.MuiFormControl-root": { width: "100%" },
|
|
|
|
"& .MuiFormControlLabel-label": {
|
2024-09-12 11:14:54 +00:00
|
|
|
width: "100%",
|
2024-09-25 16:52:22 +00:00
|
|
|
maxHeight: "100%",
|
2024-04-23 14:45:49 +00:00
|
|
|
wordBreak: "break-word",
|
|
|
|
height: variant.answer.length <= 60 ? undefined : "60px",
|
|
|
|
overflow: "auto",
|
|
|
|
lineHeight: "normal",
|
2024-09-26 19:51:46 +00:00
|
|
|
"&::-webkit-scrollbar": { width: "4px" },
|
|
|
|
"&::-webkit-scrollbar-thumb": { backgroundColor: theme.palette.primary.main },
|
2024-09-26 19:12:11 +00:00
|
|
|
scrollbarColor: theme.palette.primary.main,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
"& .MuiFormControlLabel-label.Mui-disabled": {
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
value={index}
|
|
|
|
labelPlacement="start"
|
|
|
|
control={
|
2024-06-29 09:32:16 +00:00
|
|
|
isMulti ? (
|
2024-09-12 08:43:50 +00:00
|
|
|
<Radio
|
2024-04-23 14:45:49 +00:00
|
|
|
checked={!!answer?.includes(variant.id)}
|
2024-09-12 08:43:50 +00:00
|
|
|
checkedIcon={<RadioCheck color={theme.palette.primary.main} />}
|
|
|
|
icon={<RadioIcon />}
|
2024-04-23 14:45:49 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2024-06-29 09:32:16 +00:00
|
|
|
<Radio
|
|
|
|
checkedIcon={<RadioCheck color={theme.palette.primary.main} />}
|
|
|
|
icon={<RadioIcon />}
|
|
|
|
/>
|
2024-04-23 14:45:49 +00:00
|
|
|
)
|
|
|
|
}
|
2024-09-12 11:14:54 +00:00
|
|
|
label={
|
|
|
|
own ? (
|
2024-09-26 19:51:46 +00:00
|
|
|
<>
|
|
|
|
<Typography
|
|
|
|
sx={{
|
2024-10-08 20:14:02 +00:00
|
|
|
color: theme.palette.text.primary,
|
2024-09-26 19:51:46 +00:00
|
|
|
fontSize: "14px",
|
|
|
|
position: "absolute",
|
|
|
|
top: "-23px",
|
|
|
|
}}
|
|
|
|
>
|
2025-04-20 15:16:22 +00:00
|
|
|
{t("Enter your answer")}
|
2024-09-26 19:51:46 +00:00
|
|
|
</Typography>
|
|
|
|
<OwnInput
|
|
|
|
questionId={questionId}
|
|
|
|
variant={variant}
|
|
|
|
largeCheck={questionLargeCheck}
|
2024-10-08 20:14:02 +00:00
|
|
|
ownPlaceholder={ownPlaceholder || "|"}
|
2024-09-26 19:51:46 +00:00
|
|
|
/>
|
|
|
|
</>
|
2024-09-12 11:14:54 +00:00
|
|
|
) : (
|
|
|
|
variant.answer
|
|
|
|
)
|
|
|
|
}
|
2024-04-23 14:45:49 +00:00
|
|
|
onClick={sendVariant}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|