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

151 lines
5.5 KiB
TypeScript
Raw Normal View History

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