2023-09-27 13:16:42 +03:00

177 lines
7.4 KiB
JavaScript

import React from "react";
import Head from 'next/head';
import Image from 'next/image';
import Link from "next/link";
import cookie from 'cookie';
import { connect } from "react-redux";
import numeral from "numeral";
import pluralize from 'pluralize-ru';
import { SpinnerCircular } from 'spinners-react';
import Dropzone from 'react-dropzone';
import { each, concat, concatSeries } from 'async';
import { uploadAttachmentFile, removeAttachmentFile } from "../../../actions";
export default class FilesList extends React.Component
{
constructor(props)
{
super(props);
this.state = {
loading: false,
errors: [],
};
}
_handle_onAddFile = (files) =>
{
const errors = [];
this.setState({ errors: [] }, () =>
{
const { name, onAddFile } = this.props;
concatSeries(files, (file, callback) =>
{
if(file.size > ( 1024 * 1024 * process.env.NEXT_PUBLIC_UPLOAD_FILESIZE_LIMIT_MB ))
{
errors.push(file.name);
this.setState({ errors });
callback(null, []);
}
else
{
this.setState({ loading: true }, () =>
{
uploadAttachmentFile(file)
.then((saved) =>
{
callback(null, [ saved ]);
})
.catch((error) =>
{
console.error(error);
callback(null, []);
});
});
}
}, (error, saved_files) =>
{
onAddFile(name, saved_files);
this.setState({ loading: false });
});
});
}
_handle_onRemoveFile = (file) =>
{
const { name, onRemoveFile } = this.props;
removeAttachmentFile(file.id)
.then(() =>
{
onRemoveFile(name, file.id);
})
.catch(() =>
{
});
}
_renderFileName = (name) =>
{
let chunks = name.split(/(.{19})/).filter(O => O);
if(chunks.length > 2)
{
chunks = chunks.slice(0, 2);
if(chunks[1].length > 17)
{
let second_line = chunks[1].split();
second_line.slice(0, 17);
second_line.push("...");
chunks[1] = second_line;
}
}
//return chunks.join("\n");
return chunks;
}
_renderFileType = (file) =>
{
return file.name?.split(".")?.pop()?.toString()?.substr(0, 3);
}
render()
{
const { files, checking, title, maxFiles = 5, } = this.props;
const { loading, errors } = this.state;
return (
<>
{ errors.length > 0 &&
(
<div className="questionnaire message error">
<svg width="44" height="45" viewBox="0 0 44 45" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M40.5425 31.1863L25.7969 8.08116C24.9653 6.77804 23.5459 6 22 6C20.4539 6 19.0345 6.77804 18.2032 8.08116L3.45741 31.1862C2.57234 32.5732 2.51363 34.3313 3.30467 35.7746C4.09572 37.2173 5.60918 38.1137 7.25444 38.1137H36.7456C38.3909 38.1137 39.9044 37.2175 40.6956 35.7742C41.4863 34.3313 41.4276 32.5733 40.5425 31.1863ZM22 34.2245C20.644 34.2245 19.5448 33.1252 19.5448 31.7694C19.5448 30.4133 20.6441 29.3141 22 29.3141C23.356 29.3141 24.4551 30.4133 24.4551 31.7694C24.4551 33.1252 23.3559 34.2245 22 34.2245ZM25.403 17.1635L24.1937 25.3052C24.0157 26.5037 22.8999 27.3309 21.7016 27.1529C20.7334 27.0091 20.0075 26.25 19.8582 25.333L18.5451 17.2074C18.2394 15.3155 19.5251 13.534 21.417 13.2283C23.3089 12.9226 25.0904 14.2083 25.3962 16.1002C25.4536 16.4565 25.4517 16.8243 25.403 17.1635Z" fill="white"/>
</svg>
<p><b>Ошибка</b>
{ errors.map((filename, index) => (
<React.Fragment key={ index }><span>Файл «{ filename }» превышает допустимый лимит в { process.env.NEXT_PUBLIC_UPLOAD_FILESIZE_LIMIT_MB }мб и не может быть загружен.</span><br/></React.Fragment>
)) }
</p>
</div>
) }
<div className="files_list" style={{ flexDirection: "row", display: "flex", alignItems: "flex-start", justifyContent: "flex-start", flexWrap: "wrap", gap: "2%", }}>
{ files.map((file, index) => {
if(file.name === undefined) { return null; }
return (
<div key={ index } className="docs_list medium-icon" style={{ marginBottom: "2%", position: "relative", border: "1px dashed rgb(28, 1, 169)", width: "32%", height: "100px", borderRadius: "4px", display: "flex", alignItems: "center", justifyContent: "center", }}>
<div className="row" style={{ alignItems: "center", justifyContent: "center", display: "flex", flexDirection: "row", flex: 1, marginBottom: "0px" }}>
<p className="doc_name extension" data-format={ this._renderFileType(file) } style={{ wordBreak: "break-all", lineHeight: "15px", }}>{ this._renderFileName(file.name) }{/*}<span style={{width: "100%"}}>Постановление</span>{*/}</p>
</div>
{ !checking && (
<div style={{ position: "absolute", top: "5px", right: "5px", width: "20px", height: "20px", background: "#edeff5", flex: 1, justifyContent: "center", display: "flex", alignItems: "center", borderRadius: "10px", cursor: "pointer" }} onClick={ () => this._handle_onRemoveFile(file) }>
<span style={{ color: "#919399", fontSize: "12px" }}></span>
</div>
) }
</div>
)
}) }
{ loading && (
<div className="docs_list medium-icon" style={{ position: "relative", border: "1px dashed rgb(28, 1, 169)", width: "32%", height: "100px", borderRadius: "4px", display: "flex", alignItems: "center", justifyContent: "center", }}>
<div className="row" style={{ alignItems: "center", justifyContent: "center", display: "flex", flexDirection: "row", flex: 1, marginBottom: "0px" }}>
<div style={{ display: "flex", justifyContent: "center", alignItems: "center", }}>
<SpinnerCircular size={ 50 } thickness={ 100 } speed={ 100 } color="rgba(28, 1, 169, 1)" secondaryColor="rgba(236, 239, 244, 1)" />
</div>
</div>
</div>
) }
{ files.length < (loading ? maxFiles - 1 : maxFiles) && !checking && (
<Dropzone onDrop={ (acceptedFiles) => this._handle_onAddFile(acceptedFiles) }>
{ ({getRootProps, getInputProps}) => (
<div className="file_upload dropzone" { ...getRootProps() } style={{ width: "32%", height: "100px", marginBottom: "25px", marginTop: "0px", cursor: "pointer", }}>
<div className="files"></div>
<div>
<svg width="36" height="36" viewBox="0 0 36 36" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M28.1251 31.5H7.87402C7.57565 31.5 7.28951 31.3815 7.07853 31.1705C6.86755 30.9595 6.74902 30.6734 6.74902 30.375V5.625C6.74902 5.32663 6.86755 5.04048 7.07853 4.82951C7.28951 4.61853 7.57565 4.5 7.87402 4.5H21.3751L29.2501 12.375V30.375C29.2501 30.5227 29.221 30.669 29.1645 30.8055C29.108 30.942 29.0251 31.066 28.9206 31.1705C28.8162 31.275 28.6921 31.3578 28.5556 31.4144C28.4192 31.4709 28.2729 31.5 28.1251 31.5Z" stroke="#1C01A9" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
<path d="M21.375 4.5V12.375H29.2511" stroke="#1C01A9" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
<path d="M14.625 21.375H21.375" stroke="#1C01A9" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
<path d="M18 18V24.75" stroke="#1C01A9" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>
<label htmlFor="" className="blue unselectable" style={{ lineHeight: "12px", paddingLeft: "5px", paddingRight: "9px", cursor: "pointer", }}>{ title !== undefined ? title : "Прикрепить скан документов" }</label>
</div>
<input { ...getInputProps() } />
</div>
) }
</Dropzone>
) }
</div>
</>
)
}
}