import React, { Component, useMemo } from "react"; import Router from "next/router"; import { getPelanggaranPublic } from "@/actions/pelanggaran"; import { createPelaporan } from "@/actions/pelaporan"; import Select from "react-select"; import { Row, Col, FormGroup, Input, Button, Progress } from "reactstrap"; import { connect } from "react-redux"; import { ToastContainer, toast } from "react-toastify"; import moment from "moment"; import "react-toastify/dist/ReactToastify.css"; import { Formik, Form, Field, ErrorMessage } from "formik"; import * as Yup from "yup"; import { getCsrf } from "../../actions/security"; import { createLog } from "@/actions/log"; import Swal from "sweetalert2"; let Dropzone = null; class DropzoneWrapper extends Component { state = { isClient: false, }; componentDidMount = () => { Dropzone = require("react-dropzone").default; this.setState({ isClient: true }); }; render() { return Dropzone ? {this.props.children} : null; } } const checkIfFilesAreTooBig = (files) => { let valid = true; if (files) { files.map((file) => { if (file.size > 15 * 1024 * 1024) { valid = false; } }); } return valid; }; const checkIfFilesAreCorrectType = (files) => { let valid = true; if (files) { files.map((file) => { if (!["image/jpeg", "image/png"].includes(file.type)) { valid = false; } }); } return valid; }; const selectInstanceId = 1; const laporanSchema = Yup.object().shape({ no_laporan: Yup.string().required(""), keterangan: Yup.string().min(3, "Minimal 3 Huruf").max(200).required("Wajib isi keterangan"), pelanggaran: Yup.array().min(1, "Wajib pilih pelanggaran").required(), dokumen: Yup.array().min(1, "Wajib upload file pendukung").required("Harus upload file").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig), }); export class InputData extends Component { constructor(props) { super(props); this.state = { dropdownOpen: false, splitButtonOpen: false, selectedOptionMulti: [], stat: "Waiting to add files..", pelaporanNumber: moment(new Date()).format("DDMM") + "" + Math.floor(Math.random() * 1000000), keteranganLaporan: "", files: [], pelanggaran: [], selectedFile: {}, }; } componentDidMount = async () => { const pelanggaran = await getPelanggaranPublic(); this.setState({ pelanggaran }); }; optionsJenisPelanggaran = (pelanggaran) => { return pelanggaran.data.map((e) => ({ value: e._id, label: e.pelanggaran, className: "State-ACT" })); }; setKeteranganPelaporan = (e) => { this.setState({ keteranganLaporan: e.target.value }); }; toggleDropDown = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen, }); }; toggleSplit = () => { this.setState({ splitButtonOpen: !this.state.splitButtonOpen, }); }; handleChangeSelectMulti = (selectedOptionMulti) => { this.setState({ selectedOptionMulti }); }; onDrop = (selectedFile) => { this.setState({ selectedFile: selectedFile.map((file) => Object.assign(file, { preview: URL.createObjectURL(file), }) ), stat: "Added " + selectedFile.length + " file(s)", }); const selectFile = this.state.selectedFile this.setState(prevState => ({ files: [...prevState.files, ...selectFile] })) }; uploadFiles = (e) => { e.preventDefault(); e.stopPropagation(); this.setState({ stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.", }); }; clearFiles = (e) => { e.preventDefault(); e.stopPropagation(); this.setState({ stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.", }); this.setState({ files: [], }); }; createLog = async () => { const { token } = this.props; const getTokenCsrf = await getCsrf(); const _csrf = getTokenCsrf.token; await createLog(token, { aktivitas: "Mengakses halaman Pelaporan", menu: "Pelaporan", _csrf: _csrf }); } onSubmit = async (data) => { if (this.props?.user?.role.id === 2071) { Swal.fire({ icon: 'error', title: 'Oops...', html: 'Maaf anda tidak memiliki akses untuk menyelesaikan

proses ini.

', confirmButtonColor: "#3e3a8e", confirmButtonText: 'Oke' // footer: 'Why do I have this issue?' }) } else { const { token, query } = this.props; const getTokenCsrf = await getCsrf(); const _csrf = getTokenCsrf.token; const formdata = new FormData(); formdata.append("no_laporan", data.no_laporan); formdata.append("pt_id", query.ptId); formdata.append("keterangan", data.keterangan); formdata.append("pelanggaran_id", data.pelanggaran.join()); this.state.files.forEach((e) => { formdata.append("dokumen", e); }); const create = createPelaporan(token, formdata, _csrf); await toast.promise(create, { pending: "Loading...", success: "Berhasil buat laporan", error: "Gagal buat laporan", }, this.createLog()); await Router.push({ pathname: "/app/pelaporan", }); } }; render() { const { selectedOptionMulti, files, pelanggaran } = this.state; const removeFile = file => () => { const newFiles = [...files] newFiles.splice(newFiles.indexOf(file), 1) this.setState({ files: newFiles, }); } const thumbs = files.map((file, index) => (

  {file.name} ); }} )}

{thumbs}

Ukuran setiap dokumen maksimal 15mb

)} ); } } const mapStateToProps = (state) => ({ user: state.user, token: state.token }); export default connect(mapStateToProps)(InputData);