frontPanel/src/pages/Questions/UploadFile/UploadFile.tsx

203 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-08-25 09:14:53 +00:00
import { useState, useEffect } from "react";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
2023-08-24 11:09:47 +00:00
import {
Box,
FormControl,
MenuItem,
Select,
SelectChangeEvent,
Typography,
2023-10-04 13:40:22 +00:00
Tooltip,
2023-09-15 12:37:12 +00:00
useMediaQuery,
2023-08-24 11:09:47 +00:00
useTheme,
} from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";
2023-08-25 09:14:53 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
import InfoIcon from "../../../assets/icons/InfoIcon";
import ArrowDown from "../../../assets/icons/ArrowDownIcon";
import SwitchUpload from "./switchUpload";
2023-10-04 11:35:02 +00:00
import type { AnyQuizQuestion } from "../../../model/questionTypes/shared";
import type {
QuizQuestionFile,
UploadFileType,
} from "../../../model/questionTypes/file";
2023-10-03 14:03:57 +00:00
2023-08-24 11:09:47 +00:00
interface Props {
totalIndex: number;
}
2023-10-04 11:35:02 +00:00
type DesignItem = {
name: string;
value: UploadFileType;
};
const DESIGN_TYPES: DesignItem[] = [
2023-08-25 09:14:53 +00:00
{ name: "Все типы файлов", value: "all" },
{ name: "Изображения", value: "picture" },
{ name: "Видео", value: "video" },
{ name: "Аудио", value: "audio" },
{ name: "Документ", value: "document" },
];
2023-08-24 11:09:47 +00:00
export default function UploadFile({ totalIndex }: Props) {
2023-08-25 09:14:53 +00:00
const [switchState, setSwitchState] = useState("setting");
2023-09-06 13:20:12 +00:00
const quizId = Number(useParams().quizId);
2023-08-25 09:14:53 +00:00
const { listQuestions } = questionStore();
2023-08-24 11:09:47 +00:00
const theme = useTheme();
2023-09-15 12:37:12 +00:00
const isTablet = useMediaQuery(theme.breakpoints.down(980));
2023-10-03 14:03:57 +00:00
const question = listQuestions[quizId][totalIndex] as QuizQuestionFile;
2023-09-28 21:17:01 +00:00
const isFigmaTablet = useMediaQuery(theme.breakpoints.down(990));
2023-09-26 21:11:27 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(790));
2023-08-25 09:14:53 +00:00
2023-08-24 11:09:47 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-08-25 09:14:53 +00:00
const handleChange = ({ target }: SelectChangeEvent) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<AnyQuizQuestion>(quizId, totalIndex, {
content: { ...question.content, type: target.value as UploadFileType },
2023-10-03 14:03:57 +00:00
});
2023-08-24 11:09:47 +00:00
};
2023-08-25 09:14:53 +00:00
useEffect(() => {
2023-10-03 14:03:57 +00:00
const isTypeSetted = DESIGN_TYPES.find(
({ value }) => value === question.content.type
);
2023-08-25 09:14:53 +00:00
if (!isTypeSetted) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<AnyQuizQuestion>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, type: DESIGN_TYPES[0].value },
});
2023-08-25 09:14:53 +00:00
}
}, []);
2023-08-24 11:09:47 +00:00
return (
<>
<Box
sx={{
2023-09-15 12:37:12 +00:00
width: isTablet ? "auto" : "100%",
2023-08-24 11:09:47 +00:00
maxWidth: "640px",
display: "flex",
2023-09-26 21:11:27 +00:00
px: "20px",
2023-08-24 11:09:47 +00:00
flexDirection: "column",
}}
>
2023-10-03 20:11:58 +00:00
<Box sx={{ gap: "10px", display: "flex", mt: isMobile ? "15px" : "0px" }}>
2023-08-24 11:09:47 +00:00
<FormControl
fullWidth
size="small"
sx={{
width: "100%",
minWidth: "200px",
height: "48px",
2023-09-28 21:17:01 +00:00
maxWidth: isFigmaTablet ? "551px" : "640px",
2023-08-24 11:09:47 +00:00
}}
>
<Select
id="category-select"
variant="outlined"
2023-10-06 08:34:44 +00:00
value={question.content.type}
2023-08-24 11:09:47 +00:00
displayEmpty
onChange={handleChange}
sx={{
height: "48px",
borderRadius: "8px",
"& .MuiOutlinedInput-notchedOutline": {
border: `1px solid ${theme.palette.brightPurple.main} !important`,
},
}}
MenuProps={{
PaperProps: {
sx: {
mt: "8px",
p: "4px",
borderRadius: "8px",
border: "1px solid #EEE4FC",
boxShadow: "0px 8px 24px rgba(210, 208, 225, 0.4)",
},
},
MenuListProps: {
sx: {
py: 0,
display: "flex",
flexDirection: "column",
gap: "8px",
"& .Mui-selected": {
backgroundColor: theme.palette.background.default,
color: theme.palette.brightPurple.main,
},
},
},
}}
inputProps={{
sx: {
2023-09-28 21:17:01 +00:00
height: "48px",
2023-08-24 11:09:47 +00:00
color: theme.palette.brightPurple.main,
display: "flex",
alignItems: "center",
px: "9px",
gap: "20px",
},
}}
IconComponent={(props) => <ArrowDown {...props} />}
>
2023-08-25 09:14:53 +00:00
{DESIGN_TYPES.map(({ name, value }) => (
2023-08-24 11:09:47 +00:00
<MenuItem
2023-08-25 09:14:53 +00:00
key={value}
value={value}
2023-08-24 11:09:47 +00:00
sx={{
display: "flex",
alignItems: "center",
gap: "20px",
p: "4px",
borderRadius: "5px",
color: theme.palette.grey2.main,
}}
>
2023-08-25 09:14:53 +00:00
{name}
2023-08-24 11:09:47 +00:00
</MenuItem>
))}
</Select>
</FormControl>
</Box>
2023-09-26 21:11:27 +00:00
<Box
sx={{
display: "flex",
2023-10-03 20:11:58 +00:00
alignItems: "flex-start",
2023-09-26 21:11:27 +00:00
marginBottom: "20px",
2023-10-03 20:11:58 +00:00
marginTop: "15px",
2023-09-26 21:11:27 +00:00
}}
>
2023-08-24 11:09:47 +00:00
<Typography
sx={{
fontWeight: 400,
fontSize: "16px",
lineHeight: "18.96px",
color: theme.palette.grey2.main,
}}
>
Пользователь может загрузить любой собственный файл
</Typography>
2023-10-06 08:34:44 +00:00
<Tooltip
title="Можно загрузить файл в желаемом формате."
placement="top"
>
2023-10-04 13:40:22 +00:00
<Box>
<InfoIcon />
</Box>
</Tooltip>
2023-08-24 11:09:47 +00:00
</Box>
</Box>
2023-10-03 14:03:57 +00:00
<ButtonsOptions
switchState={switchState}
SSHC={SSHC}
totalIndex={totalIndex}
/>
2023-08-24 11:09:47 +00:00
<SwitchUpload switchState={switchState} totalIndex={totalIndex} />
</>
);
}