2024-04-23 14:45:49 +00:00
|
|
|
import { useState } from "react";
|
2024-05-31 16:41:18 +00:00
|
|
|
import { Box, Rating as RatingComponent, Typography, useTheme } from "@mui/material";
|
2024-04-23 14:45:49 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
2024-05-31 17:56:17 +00:00
|
|
|
import { useQuizSettings } from "@contexts/QuizDataContext";
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
import FlagIcon from "@icons/questionsPage/FlagIcon";
|
|
|
|
import StarIconMini from "@icons/questionsPage/StarIconMini";
|
|
|
|
import HashtagIcon from "@icons/questionsPage/hashtagIcon";
|
|
|
|
import HeartIcon from "@icons/questionsPage/heartIcon";
|
|
|
|
import LightbulbIcon from "@icons/questionsPage/lightbulbIcon";
|
|
|
|
import LikeIcon from "@icons/questionsPage/likeIcon";
|
|
|
|
import TropfyIcon from "@icons/questionsPage/tropfyIcon";
|
|
|
|
|
|
|
|
import type { QuizQuestionRating } from "@model/questionTypes/rating";
|
|
|
|
|
|
|
|
const RATING_FORM_BUTTONS = [
|
|
|
|
{
|
|
|
|
name: "star",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <StarIconMini width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "trophie",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <TropfyIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "flag",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <FlagIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "heart",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <HeartIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "like",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <LikeIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bubble",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <LightbulbIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hashtag",
|
2024-05-31 16:41:18 +00:00
|
|
|
icon: (color: string, width: number) => <HashtagIcon width={width} color={color} />,
|
2024-04-23 14:45:49 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
type RatingProps = {
|
|
|
|
currentQuestion: QuizQuestionRating;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Rating = ({ currentQuestion }: RatingProps) => {
|
|
|
|
const [isSending, setIsSending] = useState<boolean>(false);
|
2024-05-31 17:56:17 +00:00
|
|
|
const { quizId, preview } = useQuizSettings();
|
2024-04-23 14:45:49 +00:00
|
|
|
const { updateAnswer } = useQuizViewStore((state) => state);
|
|
|
|
const answers = useQuizViewStore((state) => state.answers);
|
|
|
|
const theme = useTheme();
|
|
|
|
const isMobile = useRootContainerSize() < 650;
|
|
|
|
const isTablet = useRootContainerSize() < 750;
|
2024-05-31 16:41:18 +00:00
|
|
|
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
|
|
|
|
const form = RATING_FORM_BUTTONS.find(({ name }) => name === currentQuestion.content.form);
|
2024-04-23 14:45:49 +00:00
|
|
|
|
|
|
|
const sendRating = async (value: number | null) => {
|
|
|
|
setIsSending(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: String(value) + " из " + currentQuestion.content.steps,
|
|
|
|
qid: quizId,
|
|
|
|
preview,
|
|
|
|
});
|
|
|
|
|
|
|
|
updateAnswer(currentQuestion.id, String(value), 0);
|
|
|
|
} catch (error) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSending(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
2024-05-31 16:41:18 +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: "inline-flex",
|
|
|
|
alignItems: "center",
|
|
|
|
gap: "20px",
|
|
|
|
marginTop: "20px",
|
|
|
|
flexDirection: "column",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Box sx={{ display: "inline-block", width: "100%" }}>
|
|
|
|
<RatingComponent
|
|
|
|
disabled={isSending}
|
|
|
|
value={Number(answer || 0)}
|
|
|
|
onChange={(_, value) => sendRating(value)}
|
|
|
|
sx={{
|
|
|
|
height: "50px",
|
|
|
|
opacity: "1!important",
|
|
|
|
"& .MuiRating-root.Mui-disabled": { opacity: "1!important" },
|
|
|
|
"& .MuiRating-icon": { mr: isMobile ? undefined : "15px" },
|
|
|
|
}}
|
|
|
|
max={currentQuestion.content.steps}
|
2024-05-31 16:41:18 +00:00
|
|
|
icon={form?.icon(theme.palette.primary.main, isMobile ? 30 : isTablet ? 40 : 50)}
|
|
|
|
emptyIcon={form?.icon("#9A9AAF", isMobile ? 30 : isTablet ? 40 : 50)}
|
2024-04-23 14:45:49 +00:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
gap: 2,
|
|
|
|
width: "100%",
|
|
|
|
}}
|
|
|
|
>
|
2024-05-31 16:41:18 +00:00
|
|
|
<Typography sx={{ color: "#9A9AAF" }}>{currentQuestion.content.ratingNegativeDescription}</Typography>
|
|
|
|
<Typography sx={{ color: "#9A9AAF" }}>{currentQuestion.content.ratingPositiveDescription}</Typography>
|
2024-04-23 14:45:49 +00:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|