front-hub/src/pages/AccountSettings/DocumentsDialog/DocumentItem.tsx

150 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-05-27 15:43:38 +00:00
import axios from "axios";
import {
Box,
IconButton,
SxProps,
Theme,
Typography,
useTheme,
} from "@mui/material";
import { Document, Page } from "react-pdf";
import { Buffer } from "buffer";
import { downloadFileToDevice } from "@root/utils/downloadFileToDevice";
import EditIcon from "@mui/icons-material/Edit";
import { ChangeEvent, useEffect, useRef, useState } from "react";
2024-05-27 15:43:38 +00:00
import { SendDocumentsArgs, Verification } from "@root/model/auth";
import { updateDocument } from "@api/verification";
import { jsonToFormdata } from "@utils/jsonToFormdata";
import { parseAxiosError } from "@utils/parse-error";
import { readFile } from "@root/utils/readFile";
import { enqueueSnackbar } from "notistack";
2023-06-02 08:22:14 +00:00
2024-05-27 15:43:38 +00:00
type KeyNames = "inn" | "rule" | "certificate";
2023-06-02 08:22:14 +00:00
interface Props {
2024-05-27 15:43:38 +00:00
text: string;
documentUrl: string;
sx?: SxProps<Theme>;
keyName: KeyNames;
2023-06-02 08:22:14 +00:00
}
2024-05-27 15:43:38 +00:00
export default function DocumentItem({
text,
documentUrl = "",
sx,
keyName,
}: Props) {
const theme = useTheme();
2024-05-27 15:43:38 +00:00
const fileInputRef = useRef<HTMLInputElement>(null);
2023-07-25 22:31:04 +00:00
2024-05-27 15:43:38 +00:00
function handleChooseFileClick() {
fileInputRef.current?.click();
}
2024-05-27 15:43:38 +00:00
const downloadFile = async () => {
const { data } = await axios.get<ArrayBuffer>(documentUrl, {
responseType: "arraybuffer",
});
2024-05-27 15:43:38 +00:00
if (!data) {
return;
}
2023-07-25 22:31:04 +00:00
2024-05-27 15:43:38 +00:00
downloadFileToDevice(
`${documentUrl.split("/").pop()?.split(".")?.[0] || "document"}.pdf`,
Buffer.from(data)
);
2023-06-02 08:22:14 +00:00
2024-05-27 15:43:38 +00:00
return;
};
2023-07-25 22:31:04 +00:00
const [readyShowDocument, setReadyShowDocument] = useState(false);
useEffect(() => {
if (typeof documentUrl === 'string') {
if (!documentUrl.includes("pena.digital")) {
console.log(documentUrl)
fetch(documentUrl)
.then(e => {
console.log(e)
setReadyShowDocument(true)
})
.catch(e => console.log(e))
}
}
}, [])
2024-05-27 15:43:38 +00:00
async function sendDocument(e: ChangeEvent<HTMLInputElement>) {
const target = e.target as HTMLInputElement;
const file = target?.files?.[0] || null;
if (file !== null) {
const readedFile = await readFile(file, "binary");
const [, updateDocumentError] = await updateDocument(
jsonToFormdata({ [keyName]: readedFile })
);
2023-07-25 22:31:04 +00:00
2024-05-27 15:43:38 +00:00
if (updateDocumentError) {
return enqueueSnackbar(
`Ошибка отправки документов. ${updateDocumentError}`
);
}
2024-05-27 15:43:38 +00:00
enqueueSnackbar("Данные обновлены");
}
}
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
gap: "10px",
...sx,
}}
>
<Typography
sx={{
color: "#4D4D4D",
fontWeight: 500,
fontVariantNumeric: "tabular-nums",
}}
>
{text}
</Typography>
{documentUrl && readyShowDocument && (
2024-05-27 15:43:38 +00:00
<>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Typography
sx={{ color: theme.palette.purple.main, cursor: "pointer" }}
onClick={downloadFile}
>
{documentUrl.split("/").pop()?.split(".")?.[0]}
</Typography>
<IconButton onClick={handleChooseFileClick}>
<EditIcon sx={{ color: theme.palette.purple.main }} />
</IconButton>
<input
ref={fileInputRef}
style={{ display: "none" }}
onChange={sendDocument}
type="file"
id="image-file"
multiple
accept={"application/pdf"}
/>
</Box>
<Document file={documentUrl} className={"cnvs"}>
2024-05-27 15:43:38 +00:00
<Page
pageNumber={1}
width={80}
2024-05-27 15:43:38 +00:00
renderTextLayer={false}
renderAnnotationLayer={false}
/>
</Document>
</>
)}
</Box>
);
2023-07-25 22:31:04 +00:00
}