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

263 lines
8.4 KiB
TypeScript
Raw Normal View History

2023-11-05 23:33:40 +00:00
import { Box, Button, Dialog, IconButton, Typography } from "@mui/material"
import { enqueueSnackbar } from "notistack"
import CloseSmallIcon from "@root/components/icons/CloseSmallIcon"
2023-09-08 07:51:24 +00:00
import {
2023-11-05 23:33:40 +00:00
closeDocumentsDialog,
setDocument,
useUserStore,
} from "@root/stores/user"
import DocumentUploadItem from "./DocumentUploadItem"
import DocumentItem from "./DocumentItem"
import { verify } from "../helper"
import { VerificationStatus } from "@root/model/account"
import { sendDocuments, updateDocuments } from "@root/api/verification"
import { readFile } from "@root/utils/readFile"
import { deleteEmptyKeys } from "@root/utils/deleteEmptyKeys"
import { useState } from "react"
import { theme } from "@root/utils/theme"
2023-06-02 08:22:14 +00:00
const dialogContainerStyle = {
2023-11-05 23:33:40 +00:00
height: "100%",
overflowY: "scroll",
}
2023-06-02 08:22:14 +00:00
export default function NkoDocumentsDialog() {
2023-11-05 23:33:40 +00:00
const isOpen = useUserStore((state) => state.isDocumentsDialogOpen)
const verificationStatus = useUserStore((state) => state.verificationStatus)
const documents = useUserStore((state) => state.documents)
const documentsUrl = useUserStore((state) => state.documentsUrl)
const userId = useUserStore((state) => state.userId) ?? ""
2023-06-02 08:22:14 +00:00
2023-11-05 23:33:40 +00:00
const sendUploadedDocuments = async () => {
if (
documents["ИНН"].file &&
2023-09-15 12:28:46 +00:00
documents["Устав"].file &&
documents["Свидетельство о регистрации НКО"].file
2023-10-27 23:13:54 +00:00
&& !documentsUrl["ИНН"] && !documentsUrl["Устав"] && !documentsUrl["Свидетельство о регистрации НКО"]
2023-11-05 23:33:40 +00:00
) {
closeDocumentsDialog()
//Пользователь заполнил все поля и на беке пусто
const inn = await readFile(documents["ИНН"].file, "binary")
const rule = await readFile(documents["Устав"].file, "binary")
const certificate = await readFile(
documents["Свидетельство о регистрации НКО"].file,
"binary"
)
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
const [_, sendDocumentsError] = await sendDocuments({
status: "nko",
inn,
rule,
certificate,
})
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
if (sendDocumentsError) {
enqueueSnackbar(sendDocumentsError)
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
return
}
if (_ === "OK") {
enqueueSnackbar("Информация доставлена")
}
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
setDocument("ИНН", null)
setDocument("Устав", null)
setDocument("Свидетельство о регистрации НКО", null)
2023-10-27 23:13:54 +00:00
2023-11-05 23:33:40 +00:00
await verify(userId)
} else { //Пользователь заполнил не все, или на беке что-то есть
if ((documents["ИНН"].file || documents["Устав"].file || documents["Свидетельство о регистрации НКО"].file) && (documentsUrl["ИНН"] || documentsUrl["Устав"] || documentsUrl["Свидетельство о регистрации НКО"])) { //минимум 1 поле заполнено на фронте и минимум 1 поле на беке записано
closeDocumentsDialog()
const inn = documents["ИНН"].file
? await readFile(documents["ИНН"].file, "binary")
: undefined
const rule = documents["Устав"].file
? await readFile(documents["Устав"].file, "binary")
: undefined
const certificate = documents["Свидетельство о регистрации НКО"].file
? await readFile(
documents["Свидетельство о регистрации НКО"].file,
"binary"
)
: undefined
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
const [_, updateDocumentsError] = await updateDocuments(
deleteEmptyKeys({
2024-05-22 20:41:34 +00:00
status: "nko",
2023-11-05 23:33:40 +00:00
inn,
rule,
certificate,
})
)
2023-09-15 12:28:46 +00:00
2023-11-05 23:33:40 +00:00
if (updateDocumentsError) {
enqueueSnackbar(updateDocumentsError)
2023-09-08 07:51:24 +00:00
2023-11-05 23:33:40 +00:00
return
}
if (_ === "OK") {
enqueueSnackbar("Информация доставлена")
}
2023-07-06 21:52:07 +00:00
2023-11-05 23:33:40 +00:00
setDocument("ИНН", null)
setDocument("Устав", null)
setDocument("Свидетельство о регистрации НКО", null)
2023-10-27 23:13:54 +00:00
2023-11-05 23:33:40 +00:00
await verify(userId)
}
}
2023-07-06 21:52:07 +00:00
2023-11-05 23:33:40 +00:00
}
2023-07-06 21:52:07 +00:00
2023-11-05 23:33:40 +00:00
const disbutt = () => {
if (documents["ИНН"].file && documents["Устав"].file && documents["Свидетельство о регистрации НКО"].file && !documentsUrl["ИНН"] && !documentsUrl["Устав"] && !documentsUrl["Свидетельство о регистрации НКО"]) { //post
//все поля заполнены и на беке пусто
return false
} else {//patch
if (documents["ИНН"].file || documents["Устав"].file || documents["Свидетельство о регистрации НКО"].file ) {
//минимум одно поле заполнено
return false
}
return true
}
}
2023-10-27 23:13:54 +00:00
2023-11-05 23:33:40 +00:00
const documentElements =
verificationStatus === VerificationStatus.VERIFICATED ? (
2023-11-05 23:33:40 +00:00
<>
<DocumentItem
text="1. Свидетельство о регистрации НКО"
keyName="certificate"
2023-11-05 23:33:40 +00:00
documentUrl={documentsUrl["Свидетельство о регистрации НКО"]}
/>
<DocumentItem
text="2. Скан ИНН организации НКО (выписка из ЕГЮРЛ)"
keyName="inn"
2023-11-05 23:33:40 +00:00
documentUrl={documentsUrl["ИНН"]}
/>
<DocumentItem
text="3. Устав организации"
documentUrl={documentsUrl["Устав"]}
keyName="rule"
2023-11-05 23:33:40 +00:00
/>
</>
) : (
2023-11-05 23:33:40 +00:00
<>
<DocumentUploadItem
text="1. Свидетельство о регистрации НКО"
accept="application/pdf"
document={documents["Свидетельство о регистрации НКО"]}
documentUrl={documentsUrl["Свидетельство о регистрации НКО"]}
onFileChange={(e) =>
setDocument(
"Свидетельство о регистрации НКО",
e.target?.files?.[0] || null
)
}
/>
<DocumentUploadItem
text="2. Скан ИНН организации НКО (выписка из ЕГЮРЛ)"
accept="application/pdf"
document={documents["ИНН"]}
documentUrl={documentsUrl["ИНН"]}
onFileChange={(e) => setDocument("ИНН", e.target?.files?.[0] || null)}
/>
<DocumentUploadItem
text="3. Устав организации"
accept="application/pdf"
document={documents["Устав"]}
documentUrl={documentsUrl["Устав"]}
onFileChange={(e) =>
setDocument("Устав", e.target?.files?.[0] || null)
}
/>
</>
)
2023-06-02 08:22:14 +00:00
2023-11-05 23:33:40 +00:00
return (
<Dialog
open={isOpen}
onClose={closeDocumentsDialog}
PaperProps={{
sx: {
width: "600px",
maxWidth: "600px",
backgroundColor: "white",
position: "relative",
display: "flex",
flexDirection: "column",
gap: "20px",
borderRadius: "12px",
boxShadow: "none",
overflowY: "hidden",
},
}}
slotProps={{
backdrop: { style: { backgroundColor: "rgb(0 0 0 / 0.7)" } },
}}
>
<IconButton
onClick={closeDocumentsDialog}
sx={{
position: "absolute",
right: "7px",
top: "7px",
}}
>
<CloseSmallIcon />
</IconButton>
<Box sx={{ p: "40px", overflowY: "scroll" }}>
<Typography variant="h5" lineHeight="100%">
{verificationStatus === VerificationStatus.VERIFICATED
? "Ваши документы"
: "Загрузите документы"}
</Typography>
<Typography
sx={{
fontWeight: 400,
fontSize: "16px",
lineHeight: "100%",
mt: "12px",
}}
>
для верификации НКО в формате PDF
2023-11-05 23:33:40 +00:00
</Typography>
{Boolean(!documentsUrl["ИНН"] && !documentsUrl["Устав"] && !documentsUrl["Свидетельство о регистрации НКО"]) && <Typography
sx={{
fontWeight: 400,
fontSize: "16px",
lineHeight: "100%",
mt: "12px",
color: theme.palette.purple.main
}}
>
2023-10-27 23:13:54 +00:00
Пожалуйста, заполните все поля!
2023-11-05 23:33:40 +00:00
</Typography>}
<Box sx={dialogContainerStyle}>
<Box
sx={{
maxHeight: "280px",
mt: "30px",
display: "flex",
flexDirection: "column",
gap: "25px",
}}
>
{documentElements}
</Box>
</Box>
</Box>
{verificationStatus === VerificationStatus.NOT_VERIFICATED && (
<Button
sx={{ position: "absolute", bottom: "20px", right: "20px" }}
onClick={sendUploadedDocuments}
variant="pena-contained-dark"
disabled={disbutt()}
>
Отправить
2023-11-05 23:33:40 +00:00
</Button>
)}
<></>
</Dialog>
)
2023-09-16 16:29:37 +00:00
}