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