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 && (

Ошибка { errors.map((filename, index) => ( Файл «{ filename }» превышает допустимый лимит в { process.env.NEXT_PUBLIC_UPLOAD_FILESIZE_LIMIT_MB }мб и не может быть загружен.
)) }

) }
{ files.map((file, index) => { if(file.name === undefined) { return null; } return (

{ this._renderFileName(file.name) }{/*}Постановление{*/}

{ !checking && (
this._handle_onRemoveFile(file) }>
) }
) }) } { loading && (
) } { files.length < (loading ? maxFiles - 1 : maxFiles) && !checking && ( this._handle_onAddFile(acceptedFiles) }> { ({getRootProps, getInputProps}) => (
) }
) }
) } }