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, useRef } from "react" import { SendDocumentsArgs, Verification } from "@root/model/auth" import makeRequest from "@api/makeRequest" import { jsonToFormdata } from "@utils/jsonToFormdata" import { parseAxiosError } from "@utils/parse-error" import { readFile } from "@root/utils/readFile" import { enqueueSnackbar } from "notistack" type KeyNames = "inn" | "rule" | "certificate" interface Props { text: string; documentUrl: string; sx?: SxProps; keyName: KeyNames } export default function DocumentItem({ text, documentUrl = "", sx, keyName }: Props) { const theme = useTheme() const fileInputRef = useRef(null) function handleChooseFileClick() { fileInputRef.current?.click() } const downloadFile = async () => { const { data } = await axios.get(documentUrl, { responseType: "arraybuffer", }) if (!data) { return } downloadFileToDevice( `${documentUrl.split("/").pop()?.split(".")?.[0] || "document"}.pdf`, Buffer.from(data) ) return } async function sendDocument( e: ChangeEvent ) { const target = e.target as HTMLInputElement; const file = target?.files?.[0] || null; if (file !== null) { const readedFile = await readFile(file, "binary") try { await makeRequest({ url: process.env.REACT_APP_DOMAIN + "/verification" + "/verification", method: "PATCH", useToken: true, withCredentials: true, body: jsonToFormdata({ [keyName]: readedFile }), }) enqueueSnackbar("Данные обновлены") } catch (nativeError) { const [error] = parseAxiosError(nativeError) enqueueSnackbar(`Ошибка отправки документов. ${error}`) } } } return ( {text} {documentUrl && ( <> {documentUrl.split("/").pop()?.split(".")?.[0]} )} ) }