frontAnswerer/lib/components/ViewPublicationPage/questions/File/index.tsx

123 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-04-23 14:45:49 +00:00
import { useState } from "react";
import { Box, Modal, Typography, useTheme } from "@mui/material";
import { UploadFile } from "./UploadFile";
import { UploadedFile } from "./UploadedFile";
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
import { useQuizViewStore } from "@stores/quizView";
import { ACCEPT_SEND_FILE_TYPES_MAP } from "@/components/ViewPublicationPage/tools/fileUpload";
import type { QuizQuestionFile } from "@model/questionTypes/file";
2025-04-20 15:16:22 +00:00
import { useTranslation } from "react-i18next";
2024-04-23 14:45:49 +00:00
2024-05-31 16:41:18 +00:00
export type ModalWarningType = "errorType" | "errorSize" | "picture" | "video" | "audio" | "document" | null;
2024-04-23 14:45:49 +00:00
type FileProps = {
currentQuestion: QuizQuestionFile;
};
export const File = ({ currentQuestion }: FileProps) => {
const theme = useTheme();
const answers = useQuizViewStore((state) => state.answers);
2024-05-31 16:41:18 +00:00
const [modalWarningType, setModalWarningType] = useState<ModalWarningType>(null);
2024-04-23 14:45:49 +00:00
const [isSending, setIsSending] = useState<boolean>(false);
const isMobile = useRootContainerSize() < 500;
2024-05-31 16:41:18 +00:00
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer as string;
2024-04-23 14:45:49 +00:00
return (
<Box>
2024-12-21 18:44:57 +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: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
maxWidth: answer?.split("|")[0] ? "640px" : "600px",
}}
>
{answer?.split("|")[0] ? (
2024-12-21 18:44:57 +00:00
<UploadedFile
currentQuestion={currentQuestion}
setIsSending={setIsSending}
/>
2024-04-23 14:45:49 +00:00
) : (
<UploadFile
currentQuestion={currentQuestion}
setModalWarningType={setModalWarningType}
isSending={isSending}
setIsSending={setIsSending}
/>
)}
{answer && currentQuestion.content.type === "picture" && (
2024-12-21 18:44:57 +00:00
<img
src={answer.split("|")[1]}
style={{ marginTop: "15px", maxWidth: "300px", maxHeight: "300px" }}
alt=""
/>
2024-04-23 14:45:49 +00:00
)}
{answer && currentQuestion.content.type === "video" && (
<video
src={answer.split("|")[1]}
style={{
marginTop: "15px",
maxWidth: "300px",
maxHeight: "300px",
objectFit: "cover",
}}
/>
)}
</Box>
2024-12-21 18:44:57 +00:00
<Modal
open={modalWarningType !== null}
onClose={() => setModalWarningType(null)}
>
2024-04-23 14:45:49 +00:00
<Box
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: isMobile ? 300 : 400,
bgcolor: "background.paper",
borderRadius: 3,
boxShadow: 24,
p: 4,
}}
>
<CurrentModal status={modalWarningType} />
</Box>
</Modal>
</Box>
);
};
const CurrentModal = ({ status }: { status: ModalWarningType }) => {
2025-04-20 15:16:22 +00:00
const { t } = useTranslation();
2024-04-23 14:45:49 +00:00
switch (status) {
case null:
return null;
case "errorType":
2025-04-20 15:16:22 +00:00
return <Typography>{t("Incorrect file type selected")}</Typography>;
2024-04-23 14:45:49 +00:00
case "errorSize":
2025-04-20 15:16:22 +00:00
return <Typography>{t("File is too big. Maximum size is 50 MB")}</Typography>;
2024-04-23 14:45:49 +00:00
default:
return (
<>
2025-04-20 15:16:22 +00:00
<Typography>{t("Acceptable file extensions")}:</Typography>
2024-05-31 16:41:18 +00:00
<Typography>{ACCEPT_SEND_FILE_TYPES_MAP[status].join(" ")}</Typography>
2024-04-23 14:45:49 +00:00
</>
);
}
};