InputEvaluasi.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import React, { Component } from "react";
  2. import { insertPemeriksaan } from "@/actions/pemeriksaan";
  3. import Router from "next/router";
  4. import Datetime from "react-datetime";
  5. import moment from "moment";
  6. import { Row, Col, FormGroup, Input, Button, Progress, Card } from "reactstrap";
  7. import { ToastContainer, toast } from "react-toastify";
  8. import { Formik, Form, Field, ErrorMessage } from "formik";
  9. import * as Yup from "yup";
  10. import { getOneLaporan, updateLaporan } from "@/actions/pelaporan";
  11. import { connect } from "react-redux";
  12. import { getCsrf } from "../../actions/security";
  13. import Swal from "sweetalert2";
  14. import Select from "react-select";
  15. const selectInstanceId = 1;
  16. const checkIfFilesAreTooBig = (files) => {
  17. let valid = true;
  18. if (files) {
  19. files.map((file) => {
  20. if (file.size > 15 * 1024 * 1024) {
  21. valid = false;
  22. }
  23. });
  24. }
  25. return valid;
  26. };
  27. const checkIfFilesAreCorrectType = (files) => {
  28. let valid = true;
  29. if (files) {
  30. files.map((file) => {
  31. if (!["image/jpeg", "image/png"].includes(file.type)) {
  32. valid = false;
  33. }
  34. });
  35. }
  36. return valid;
  37. };
  38. const evaluasiSchema = Yup.object().shape({
  39. tanggal: Yup.date().required("Wajib diisi"),
  40. judul: Yup.string().min(3).max(150).required("Wajib diisi"),
  41. dokumen: Yup.array().min(1).required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  42. });
  43. const ditutupSchema = Yup.object().shape({
  44. keterangan: Yup.string().required("Harus diisi"),
  45. dokumen: Yup.array().min(1).required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  46. });
  47. let Dropzone = null;
  48. class DropzoneWrapper extends Component {
  49. state = {
  50. isClient: false,
  51. };
  52. componentDidMount = () => {
  53. Dropzone = require("react-dropzone").default;
  54. this.setState({ isClient: true });
  55. };
  56. render() {
  57. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  58. }
  59. }
  60. const status = [
  61. { value: "Evaluasi", label: "Evaluasi", className: "State-ACT" },
  62. { value: "Ditutup", label: "Ditutup", className: "State-ACT" },
  63. ];
  64. class InputEvaluasi extends Component {
  65. constructor(props) {
  66. super(props);
  67. this.state = {
  68. dropdownOpen: false,
  69. splitButtonOpen: false,
  70. judulEvaluasi: "",
  71. tanggal: moment().format("D MMMM YYYY"),
  72. files: [],
  73. delegasichecklist: false,
  74. rolelldikti: false,
  75. selectedFile: {},
  76. selectedOption: null,
  77. };
  78. }
  79. async componentDidMount() {
  80. this.defaultStatus();
  81. }
  82. defaultStatus = async () => {
  83. return this.setState({ selectedOption: status[0] });
  84. };
  85. handleChangeSelect = (selectedOption) => this.setState({ selectedOption });
  86. getStatus = () => (status);
  87. setjudulEvaluasi = (e) => {
  88. this.setState({ judulEvaluasi: e.target.value });
  89. };
  90. setTanggal = (moment) => {
  91. this.setState({ tanggal: moment.format("D MMMM YYYY") });
  92. };
  93. toggleSplit = () => {
  94. this.setState({
  95. splitButtonOpen: !this.state.splitButtonOpen,
  96. });
  97. };
  98. toggleDropDown = () => {
  99. this.setState({
  100. dropdownOpen: !this.state.dropdownOpen,
  101. });
  102. };
  103. onDrop = (selectedFile) => {
  104. this.setState({
  105. selectedFile: selectedFile.map((file) =>
  106. Object.assign(file, {
  107. preview: URL.createObjectURL(file),
  108. })
  109. ),
  110. stat: "Added " + selectedFile.length + " file(s)",
  111. });
  112. const selectFile = this.state.selectedFile
  113. // this.state.files.push(...this.state.selectedFile)
  114. // this.setState({
  115. // files: files.map((file) =>
  116. // Object.assign(file, {
  117. // preview: URL.createObjectURL(file),
  118. // })
  119. // ),
  120. // stat: "Added " + files.length + " file(s)",
  121. // });
  122. this.setState(prevState => ({
  123. files: [...prevState.files, ...selectFile]
  124. }))
  125. };
  126. uploadFiles = (e) => {
  127. e.preventDefault();
  128. e.stopPropagation();
  129. this.setState({
  130. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  131. });
  132. };
  133. clearFiles = (e) => {
  134. e.preventDefault();
  135. e.stopPropagation();
  136. this.setState({
  137. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  138. });
  139. this.setState({
  140. files: [],
  141. });
  142. };
  143. onSubmit = async (data, { resetForm }) => {
  144. if (this.props?.user?.role.id === 2024) {
  145. Swal.fire({
  146. icon: 'error',
  147. title: 'Oops...',
  148. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  149. confirmButtonColor: "#3e3a8e",
  150. confirmButtonText: 'Oke'
  151. })
  152. } else {
  153. const getToken = await getCsrf();
  154. const _csrf = getToken.token;
  155. const { token, query } = this.props;
  156. const { id } = query;
  157. const formdata = new FormData();
  158. formdata.append("judul", data.judul);
  159. formdata.append("tanggal", data.tanggal);
  160. this.state.files.forEach((e) => {
  161. formdata.append("dokumen", e);
  162. });
  163. if (this.state.delegasichecklist == true) {
  164. await toast.promise(insertPemeriksaan(token, id, formdata, _csrf), {
  165. pending: "Loading",
  166. success: "Success",
  167. error: "Error",
  168. autoClose: 1000
  169. });
  170. data.change_role = "true";
  171. data.keterangan = "delegasi ke DIKTI"
  172. Router.push("/app/pemeriksaan");
  173. update = await updateLaporan(token, id, data);
  174. } else {
  175. await toast.promise(insertPemeriksaan(token, id, formdata, _csrf), {
  176. pending: "Loading",
  177. success: {
  178. render: "success",
  179. autoClose: 1000
  180. },
  181. error: "Error",
  182. });
  183. Router.push("/app/pemeriksaan");
  184. }
  185. this.setState({ files: [] });
  186. resetForm();
  187. const pelaporan = await getOneLaporan(token, query.id);
  188. this.props.changePelaporan(pelaporan);
  189. }
  190. };
  191. handlechecklist = () => {
  192. this.setState({ delegasichecklist: !this.state.delegasichecklist })
  193. };
  194. handleTutupLaporan = async (data, value) => {
  195. if (this.props?.user?.role.id === 2024) {
  196. Swal.fire({
  197. icon: 'error',
  198. title: 'Oops...',
  199. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  200. confirmButtonColor: "#3e3a8e",
  201. confirmButtonText: 'Oke'
  202. })
  203. } else {
  204. const getToken = await getCsrf();
  205. const _csrf = getToken.token;
  206. const { token, query } = this.props;
  207. const { id } = query;
  208. const formdata = new FormData();
  209. formdata.append("keterangan", data.keterangan);
  210. this.state.files.forEach((e) => {
  211. formdata.append("dokumen", e);
  212. });
  213. formdata.append("aktif", "false");
  214. // formdata.append("_csrf", _csrf)
  215. await toast.promise(updateLaporan(token, id, formdata, _csrf + `&redudansi=true`), {
  216. pending: "Loading",
  217. success: {
  218. render: "success",
  219. autoClose: 1000
  220. },
  221. error: "Error",
  222. });
  223. await Router.push({
  224. pathname: "/app/pemeriksaan",
  225. });
  226. }
  227. };
  228. render() {
  229. const { files, selectedOption } = this.state;
  230. const removeFile = file => () => {
  231. const newFiles = [...files]
  232. newFiles.splice(newFiles.indexOf(file), 1)
  233. this.setState({
  234. files: newFiles,
  235. });
  236. }
  237. const thumbs = files.map((file, index) => (
  238. <p>
  239. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  240. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  241. </p>
  242. ));
  243. return (
  244. <>
  245. <Formik
  246. enableReinitialize={true}
  247. initialValues={{
  248. status: this.getStatus()[0],
  249. keterangan: "",
  250. dokumen: [],
  251. }}
  252. validationSchema={selectedOption?.value === this.getStatus()[1].value ? ditutupSchema : null}
  253. onSubmit={this.handleTutupLaporan}
  254. >
  255. {({ isSubmitting }) => (
  256. <Form>
  257. <FormGroup row>
  258. <label className="col-md-2 col-form-label font-weight-bold font-color-black">Status Laporan</label>
  259. <div className="col-md-10">
  260. <Field name="status">
  261. {({ field, form }) => (
  262. <Select
  263. value={field.value}
  264. onChange={(e) => {
  265. form.setFieldValue(field.name, e);
  266. this.handleChangeSelect(e);
  267. }}
  268. options={this.getStatus()}
  269. required
  270. />
  271. )}
  272. </Field>
  273. <ErrorMessage name="status" component="div" className="form-text text-danger" />
  274. </div>
  275. </FormGroup>
  276. {selectedOption?.value === this.getStatus()[0].value ? (
  277. ""
  278. ) : (
  279. <div>
  280. <FormGroup row>
  281. <label className="col-md-2 col-form-label">Keterangan<span className=" text-danger">*</span></label>
  282. <div className="col-md-10">
  283. <Field name="keterangan">{({ field, form }) => <Input type="text" placeholder="Keterangan" {...field} />}</Field>
  284. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  285. </div>
  286. </FormGroup>
  287. <FormGroup row>
  288. <label className="col-md-2 col-form-label">Upload File Pendukung<span className="text-danger">*</span></label>
  289. <div className="col-md-10">
  290. <Field name="dokumen">
  291. {({ field, form, meta }) => (
  292. <DropzoneWrapper
  293. className=""
  294. onDrop={(e) => {
  295. this.onDrop(e);
  296. form.setFieldValue(field.name, e);
  297. }}
  298. >
  299. {({ getRootProps, getInputProps, isDragActive }) => {
  300. return (
  301. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  302. <input name="dokumen" {...getInputProps()} />
  303. <div className="dropzone-style-1">
  304. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  305. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  306. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  307. </div> :
  308. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  309. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  310. </div>
  311. }
  312. </div>
  313. </div>
  314. <div className="d-flex align-items-center">
  315. <small className="ml-auto">
  316. <button
  317. type="button"
  318. className="btn btn-link"
  319. onClick={(e) => {
  320. this.clearFiles(e);
  321. form.setFieldValue(field.name, []);
  322. }}
  323. >
  324. Reset dokumen
  325. </button>
  326. </small>
  327. </div>
  328. </div>
  329. );
  330. }}
  331. </DropzoneWrapper>
  332. )}
  333. </Field>
  334. {thumbs}
  335. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  336. <p className="mrgn-top-5 font-color-black">
  337. Ukuran setiap dokumen maksimal 15mb
  338. </p>
  339. </div>
  340. <FormGroup>
  341. <div className="col-xl-10">
  342. <Button color className="btn-login width-133 mt-4" type="submit" disabled={isSubmitting}>
  343. <span className="font-color-white">
  344. Tutup Laporan
  345. </span>
  346. </Button>
  347. </div>
  348. </FormGroup>
  349. </FormGroup>
  350. </div>
  351. )}
  352. </Form>
  353. )}
  354. </Formik>
  355. {selectedOption?.value === this.getStatus()[1].value ? (
  356. ""
  357. ) : (
  358. <Card>
  359. <Formik
  360. initialValues={{
  361. tanggal: new Date(),
  362. judul: "",
  363. dokumen: [],
  364. }}
  365. validationSchema={evaluasiSchema}
  366. onSubmit={this.onSubmit}
  367. >
  368. {({ isSubmitting }) => (
  369. <Form className="form-horizontal">
  370. <FormGroup row>
  371. <label className="col-md-2 col-form-label">Tanggal Evaluasi</label>
  372. <div className="col-md-10">
  373. <Field name="tanggal">
  374. {({ field, form }) => (
  375. <Datetime
  376. timeFormat={false}
  377. inputProps={{ className: "form-control" }}
  378. value={field.value}
  379. onChange={(e) => {
  380. form.setFieldValue(field.name, e);
  381. }}
  382. />
  383. )}
  384. </Field>
  385. <ErrorMessage name="tanggal" component="div" className="form-text text-danger" />
  386. </div>
  387. </FormGroup>
  388. <FormGroup row>
  389. <label className="col-md-2 col-form-label">Judul Dokumen</label>
  390. <div className="col-md-10">
  391. <Field name="judul">{({ field, form }) => <Input type="text" placeholder="judul" {...field} />}</Field>
  392. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  393. </div>
  394. </FormGroup>
  395. <FormGroup row>
  396. <label className="col-md-2 col-form-label">Upload File Pendukung<span className="text-danger">*</span></label>
  397. <div className="col-md-10">
  398. <Field name="dokumen">
  399. {({ field, form, meta }) => (
  400. <DropzoneWrapper
  401. className=""
  402. onDrop={(e) => {
  403. this.onDrop(e);
  404. form.setFieldValue(field.name, e);
  405. }}
  406. >
  407. {({ getRootProps, getInputProps, isDragActive }) => {
  408. return (
  409. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  410. <input name="dokumen" {...getInputProps()} />
  411. <div className="dropzone-style-1">
  412. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  413. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  414. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  415. </div> :
  416. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  417. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  418. </div>
  419. }
  420. </div>
  421. </div>
  422. <div className="d-flex align-items-center">
  423. <small className="ml-auto">
  424. <button
  425. type="button"
  426. className="btn btn-link"
  427. onClick={(e) => {
  428. this.clearFiles(e);
  429. form.setFieldValue(field.name, []);
  430. }}
  431. >
  432. Reset dokumen
  433. </button>
  434. </small>
  435. </div>
  436. </div>
  437. );
  438. }}
  439. </DropzoneWrapper>
  440. )}
  441. </Field>
  442. {thumbs}
  443. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  444. <p className="mrgn-top-5 font-color-black">
  445. Ukuran setiap dokumen maksimal 15mb
  446. </p>
  447. </div>
  448. </FormGroup>
  449. {this.props.user?.role.id === 2021 ? (
  450. <FormGroup row>
  451. <label className="col-md-2 col-form-label">Delegasi ke dikti</label>
  452. <div className="col-md-10 mt-2">
  453. <div className="checkbox c-checkbox">
  454. <label>
  455. <Input type="checkbox" onChange={this.handlechecklist} defaultChecked={this.state.delegasichecklist} />
  456. <span className="fa fa-check"></span></label>
  457. </div>
  458. </div>
  459. </FormGroup>
  460. ) : ("")}
  461. <FormGroup row>
  462. <div className="col-xl-10">
  463. <Button color className="btn-login" type="submit" disabled={isSubmitting}>
  464. <span className="font-color-white">
  465. Simpan Evaluasi
  466. </span>
  467. </Button>
  468. </div>
  469. </FormGroup>
  470. </Form>
  471. )}
  472. </Formik>
  473. </Card>
  474. )}
  475. </>
  476. );
  477. }
  478. }
  479. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  480. export default connect(mapStateToProps)(InputEvaluasi);