frontPanel/src/pages/Questions/helpQuestions.tsx

119 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-09-20 09:07:33 +00:00
import { useState } from "react";
2023-09-05 13:54:41 +00:00
import { Box, ButtonBase, Typography } from "@mui/material";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
import SelectableButton from "@ui_kit/SelectableButton";
import CustomTextField from "@ui_kit/CustomTextField";
2023-09-20 09:07:33 +00:00
import { useDebouncedCallback } from "use-debounce";
import UploadIcon from "../../assets/icons/UploadIcon";
import UploadBox from "@ui_kit/UploadBox";
2023-08-24 11:09:47 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-09-05 13:54:41 +00:00
import { UploadVideoModal } from "./UploadVideoModal";
2023-08-15 11:00:45 +00:00
2023-10-03 14:03:57 +00:00
import type { QuizQuestionBase } from "../../model/questionTypes/shared";
type BackgroundType = "text" | "video";
2023-08-24 11:09:47 +00:00
type HelpQuestionsProps = {
totalIndex: number;
};
export default function HelpQuestions({ totalIndex }: HelpQuestionsProps) {
2023-08-15 11:00:45 +00:00
const [open, setOpen] = useState(false);
const [backgroundType, setBackgroundType] = useState<BackgroundType>("text");
2023-09-06 13:20:12 +00:00
const quizId = Number(useParams().quizId);
2023-08-24 11:09:47 +00:00
const { listQuestions } = questionStore();
2023-10-03 14:03:57 +00:00
const question = listQuestions[quizId][totalIndex] as QuizQuestionBase;
2023-09-20 09:07:33 +00:00
const debounced = useDebouncedCallback((value) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionBase>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
hint: { text: value, video: question.content.hint.video },
},
});
2023-09-20 09:07:33 +00:00
}, 1000);
2023-08-15 11:00:45 +00:00
2023-09-05 13:54:41 +00:00
const videoHC = (url: string) => {
2023-10-04 09:45:51 +00:00
const clonedContent = { ...question.content };
clonedContent.hint.video = url;
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionBase>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
hint: { video: url, text: question.content.hint.text },
},
});
2023-08-15 11:00:45 +00:00
};
return (
2023-08-15 09:51:18 +00:00
<Box
sx={{
padding: "20px",
display: "flex",
flexDirection: "column",
gap: "20px",
}}
>
<Typography sx={{ fontWeight: "500" }}>Подсказка консультанта</Typography>
<Box
sx={{
display: "flex",
gap: "10px",
}}
>
2023-08-15 09:51:18 +00:00
<SelectableButton
isSelected={backgroundType === "text"}
onClick={() => setBackgroundType("text")}
sx={{ maxWidth: "130px" }}
>
Текст
</SelectableButton>
2023-08-15 09:51:18 +00:00
<SelectableButton
isSelected={backgroundType === "video"}
onClick={() => setBackgroundType("video")}
sx={{ maxWidth: "130px" }}
>
Видео
</SelectableButton>
</Box>
{backgroundType === "text" ? (
<>
2023-08-24 11:09:47 +00:00
<CustomTextField
placeholder={"Текст консультанта"}
2023-10-03 14:03:57 +00:00
text={question.content.hint.text}
2023-09-20 09:07:33 +00:00
onChange={({ target }) => debounced(target.value)}
2023-08-24 11:09:47 +00:00
/>
</>
) : (
<Box>
2023-08-15 09:51:18 +00:00
<Typography sx={{ paddingBottom: "15px", fontWeight: "500" }}>
Загрузите видео
</Typography>
<ButtonBase
2023-09-05 13:54:41 +00:00
onClick={() => setOpen(true)}
2023-08-15 09:51:18 +00:00
sx={{ justifyContent: "flex-start" }}
>
2023-10-03 14:03:57 +00:00
{question.content.hint.video ? (
<video src={question.content.hint.video} width="400" controls />
) : (
<>
<UploadBox
icon={<UploadIcon />}
sx={{
height: "48px",
width: "48px",
}}
/>
</>
)}
</ButtonBase>
2023-09-05 13:54:41 +00:00
<UploadVideoModal
open={open}
2023-09-05 13:54:41 +00:00
onClose={() => setOpen(false)}
2023-10-03 14:03:57 +00:00
video={question.content.hint.video}
2023-09-05 13:54:41 +00:00
onUpload={videoHC}
/>
</Box>
)}
</Box>
);
}