import React, { Component } from "react"; import { Row, Col, Card, CardBody, FormGroup, Input, Button, Progress } from "reactstrap"; import { toast } from "react-toastify"; import { Formik, Form, Field, ErrorMessage } from "formik"; import * as Yup from "yup"; import { connect } from "react-redux"; import { getOneSanksi, update } from "@/actions/sanksi"; import { addDays, addMonths } from 'date-fns'; import id from 'date-fns/locale/id'; import moment from "moment"; import 'moment/min/locales'; moment.locale('id'); import Router from "next/router"; import { getPelanggaranSanksi } from "@/actions/pelanggaran"; import Select from "react-select"; import { getCsrf } from "../../actions/security"; import Swal from "sweetalert2"; import Datetime from "react-datetime"; const selectInstanceId = 1; 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 rekomendasiSchema = Yup.object().shape({ no_sanksi: Yup.string().required("Wajib isi Nomor Sanksi"), keterangan: Yup.string().min(3, "Minimal 3 Huruf").max(200).required("Wajib isi keterangan"), from_date: Yup.date().notRequired("Wajib diisi"), to_date: Yup.date().notRequired("Wajib diisi"), sanksi: Yup.array().required("Wajib isi pelanggaran"), dokumen: Yup.array().required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig), }); 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; } } class InputTanggal extends Component { constructor(props) { super(props); const tmt_awal = new Date(); this.state = { dropdownOpen: false, splitButtonOpen: false, files: [], sanksi: {}, data: {}, from_date: "", to_date: "", startDay: tmt_awal, sanksi: {}, keteranganLaporan: "", tmtCheck: false, listSanksi: null, selectedFile: {} }; } async componentDidMount() { const { token, query } = this.props; const { id } = query; const sanksi = await getOneSanksi(token, id); const { data: listSanksi } = await getPelanggaranSanksi(token) this.setState({ sanksi, listSanksi }) } toggleSplit = () => { this.setState({ splitButtonOpen: !this.state.splitButtonOpen, }); }; toggleDropDown = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen, }); }; handleChangeListSanksi = (listSanksi) => { this.setState({ listSanksi }, this.setUploadSuratSanksi); }; 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: [], }); }; static getInitialProps = async ({ query }) => { return { query }; }; setKeteranganPelaporan = (e) => { this.setState({ keteranganLaporan: e.target.value }); }; handleTmtCheck = () => { this.setState({ tmtCheck: !this.state.tmtCheck }); }; handleChangeListSanksi = (listSanksi) => { this.setState({ listSanksi }); }; done = async (data) => { if (this.props?.user?.role.id === 2024) { Swal.fire({ icon: 'error', title: 'Oops...', html: 'Maaf anda tidak memiliki akses untuk menyelesaikan

proses ini.

', confirmButtonColor: "#3e3a8e", confirmButtonText: 'Oke' }) } else { this.setState({ loading: true }) const sanksi = await this.ubahSanksi(data) if (sanksi && ENV === "production") { await this.updatePddikti(sanksi.data._id) //kirim sanksiID ke function updatePDDIKTI } await Router.push({ pathname: "/app/naik-sanksi", }); } }; updatePddikti = async (sanksiId) => { const getToken = await getCsrf(); const _csrf2 = getToken.token; const toastPddikti = toast.loading("Updating pddikti..."); try { const { query, token } = this.props; const { id } = query; await updatePddikti(token, sanksiId, _csrf2) toast.update(toastPddikti, { render: "Berhasil Update PDDIKTI", type: "success", isLoading: false, autoClose: true, closeButton: true }); } catch (error) { toast.update(toastPddikti, { render: ("Gagal Update PDDIKTI"), type: "error", isLoading: false, autoClose: true, closeButton: true }); } } ubahSanksi = async (data) => { if (this.state.tmtCheck === true ||this.state.listSanksi?.find(z => z.label === "Sanksi Administratif Berat - Pencabutan izin Program Studi" || z.label === "Sanksi Administratif Berat - Pembubaran PTN atau pencabutan izin PTS")) { const getToken = await getCsrf(); const _csrf = getToken.token; const { token, query } = this.props; const { id } = query; const formdata = new FormData(); formdata.append("no_sanksi", data.no_sanksi); formdata.append("keterangan", data.keterangan); // formdata.append("from_date", data.from_date); // formdata.append("to_date", data.to_date); // formdata.append("sanksi", JSON.stringify(data.sanksi.map((e) => ({ label: e.value })))); formdata.append("sanksi", JSON.stringify(data.sanksi.map((e) => ({ label: e.value.split(";")[0], description: e.value.split(";")[1], level: e.value.split(";")[2] })))); this.state.files.forEach((e) => { formdata.append("dokumen", e); }); const toastid = toast.loading("Please wait..."); const added = await update(token, id, formdata, _csrf); if (!added) { toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true }); } else { toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true }); // Router.push("/app/naik-sanksi"); } }else{ console.log("ubahsanksi") const getToken = await getCsrf(); const _csrf = getToken.token; const { token, query } = this.props; const { id } = query; const formdata = new FormData(); formdata.append("no_sanksi", data.no_sanksi); formdata.append("keterangan", data.keterangan); formdata.append("from_date", data.from_date); formdata.append("to_date", data.to_date); // formdata.append("sanksi", JSON.stringify(data.sanksi.map((e) => ({ label: e.value })))); formdata.append("sanksi", JSON.stringify(data.sanksi.map((e) => ({ label: e.value.split(";")[0], description: e.value.split(";")[1], level: e.value.split(";")[2] })))); this.state.files.forEach((e) => { formdata.append("dokumen", e); }); const toastid = toast.loading("Please wait..."); const added = await update(token, id, formdata, _csrf); if (!added) { toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true }); } else { toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true }); // Router.push("/app/naik-sanksi"); } } }; render() { const { files } = 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}
)} ); } } const mapStateToProps = (state) => ({ user: state.user, token: state.token }); export default connect(mapStateToProps)(InputTanggal);