InputEvaluasi.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 updateLaporan(token, id, formdata, _csrf + `&redudansi=true`);
  216. await Router.push({
  217. pathname: "/app/pemeriksaan",
  218. });
  219. }
  220. };
  221. render() {
  222. const { files, selectedOption } = this.state;
  223. const removeFile = file => () => {
  224. const newFiles = [...files]
  225. newFiles.splice(newFiles.indexOf(file), 1)
  226. this.setState({
  227. files: newFiles,
  228. });
  229. }
  230. const thumbs = files.map((file, index) => (
  231. <p>
  232. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  233. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  234. </p>
  235. ));
  236. return (
  237. <>
  238. <Formik
  239. enableReinitialize={true}
  240. initialValues={{
  241. status: this.getStatus()[0],
  242. keterangan: "",
  243. dokumen: [],
  244. }}
  245. validationSchema={selectedOption?.value === this.getStatus()[1].value ? ditutupSchema : null}
  246. onSubmit={this.handleTutupLaporan}
  247. >
  248. {({ isSubmitting }) => (
  249. <Form>
  250. <FormGroup row>
  251. <label className="col-md-2 col-form-label font-weight-bold font-color-black">Status Laporan</label>
  252. <div className="col-md-10">
  253. <Field name="status">
  254. {({ field, form }) => (
  255. <Select
  256. value={field.value}
  257. onChange={(e) => {
  258. form.setFieldValue(field.name, e);
  259. this.handleChangeSelect(e);
  260. }}
  261. options={this.getStatus()}
  262. required
  263. />
  264. )}
  265. </Field>
  266. <ErrorMessage name="status" component="div" className="form-text text-danger" />
  267. </div>
  268. </FormGroup>
  269. {selectedOption?.value === this.getStatus()[0].value ? (
  270. ""
  271. ) : (
  272. <div>
  273. <FormGroup row>
  274. <label className="col-md-2 col-form-label">Keterangan<span className=" text-danger">*</span></label>
  275. <div className="col-md-10">
  276. <Field name="keterangan">{({ field, form }) => <Input type="text" placeholder="Keterangan" {...field} />}</Field>
  277. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  278. </div>
  279. </FormGroup>
  280. <FormGroup row>
  281. <label className="col-md-2 col-form-label">Upload File Pendukung<span className="text-danger">*</span></label>
  282. <div className="col-md-10">
  283. <Field name="dokumen">
  284. {({ field, form, meta }) => (
  285. <DropzoneWrapper
  286. className=""
  287. onDrop={(e) => {
  288. this.onDrop(e);
  289. form.setFieldValue(field.name, e);
  290. }}
  291. >
  292. {({ getRootProps, getInputProps, isDragActive }) => {
  293. return (
  294. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  295. <input name="dokumen" {...getInputProps()} />
  296. <div className="dropzone-style-1">
  297. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  298. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  299. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  300. </div> :
  301. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  302. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  303. </div>
  304. }
  305. </div>
  306. </div>
  307. <div className="d-flex align-items-center">
  308. <small className="ml-auto">
  309. <button
  310. type="button"
  311. className="btn btn-link"
  312. onClick={(e) => {
  313. this.clearFiles(e);
  314. form.setFieldValue(field.name, []);
  315. }}
  316. >
  317. Reset dokumen
  318. </button>
  319. </small>
  320. </div>
  321. </div>
  322. );
  323. }}
  324. </DropzoneWrapper>
  325. )}
  326. </Field>
  327. {thumbs}
  328. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  329. <p className="mrgn-top-5 font-color-black">
  330. Ukuran setiap dokumen maksimal 15mb
  331. </p>
  332. </div>
  333. <FormGroup>
  334. <div className="col-xl-10">
  335. <Button color className="btn-login" type="submit" disabled={isSubmitting}>
  336. <span className="font-color-white">
  337. Tutup Laporan
  338. </span>
  339. </Button>
  340. </div>
  341. </FormGroup>
  342. </FormGroup>
  343. </div>
  344. )}
  345. </Form>
  346. )}
  347. </Formik>
  348. {selectedOption?.value === this.getStatus()[1].value ? (
  349. ""
  350. ) : (
  351. <Card>
  352. <Formik
  353. initialValues={{
  354. tanggal: new Date(),
  355. judul: "",
  356. dokumen: [],
  357. }}
  358. validationSchema={evaluasiSchema}
  359. onSubmit={this.onSubmit}
  360. >
  361. {({ isSubmitting }) => (
  362. <Form className="form-horizontal">
  363. <FormGroup row>
  364. <label className="col-md-2 col-form-label">Tanggal Evaluasi</label>
  365. <div className="col-md-10">
  366. <Field name="tanggal">
  367. {({ field, form }) => (
  368. <Datetime
  369. timeFormat={false}
  370. inputProps={{ className: "form-control" }}
  371. value={field.value}
  372. onChange={(e) => {
  373. form.setFieldValue(field.name, e);
  374. }}
  375. />
  376. )}
  377. </Field>
  378. <ErrorMessage name="tanggal" component="div" className="form-text text-danger" />
  379. </div>
  380. </FormGroup>
  381. <FormGroup row>
  382. <label className="col-md-2 col-form-label">Judul Dokumen</label>
  383. <div className="col-md-10">
  384. <Field name="judul">{({ field, form }) => <Input type="text" placeholder="judul" {...field} />}</Field>
  385. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  386. </div>
  387. </FormGroup>
  388. <FormGroup row>
  389. <label className="col-md-2 col-form-label">Upload File Pendukung<span className="text-danger">*</span></label>
  390. <div className="col-md-10">
  391. <Field name="dokumen">
  392. {({ field, form, meta }) => (
  393. <DropzoneWrapper
  394. className=""
  395. onDrop={(e) => {
  396. this.onDrop(e);
  397. form.setFieldValue(field.name, e);
  398. }}
  399. >
  400. {({ getRootProps, getInputProps, isDragActive }) => {
  401. return (
  402. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  403. <input name="dokumen" {...getInputProps()} />
  404. <div className="dropzone-style-1">
  405. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  406. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  407. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  408. </div> :
  409. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  410. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  411. </div>
  412. }
  413. </div>
  414. </div>
  415. <div className="d-flex align-items-center">
  416. <small className="ml-auto">
  417. <button
  418. type="button"
  419. className="btn btn-link"
  420. onClick={(e) => {
  421. this.clearFiles(e);
  422. form.setFieldValue(field.name, []);
  423. }}
  424. >
  425. Reset dokumen
  426. </button>
  427. </small>
  428. </div>
  429. </div>
  430. );
  431. }}
  432. </DropzoneWrapper>
  433. )}
  434. </Field>
  435. {thumbs}
  436. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  437. <p className="mrgn-top-5 font-color-black">
  438. Ukuran setiap dokumen maksimal 15mb
  439. </p>
  440. </div>
  441. </FormGroup>
  442. {this.props.user?.role.id === 2021 ? (
  443. <FormGroup row>
  444. <label className="col-md-2 col-form-label">Delegasi ke dikti</label>
  445. <div className="col-md-10 mt-2">
  446. <div className="checkbox c-checkbox">
  447. <label>
  448. <Input type="checkbox" onChange={this.handlechecklist} defaultChecked={this.state.delegasichecklist} />
  449. <span className="fa fa-check"></span></label>
  450. </div>
  451. </div>
  452. </FormGroup>
  453. ) : ("")}
  454. <FormGroup row>
  455. <div className="col-xl-10">
  456. <Button color className="btn-login" type="submit" disabled={isSubmitting}>
  457. <span className="font-color-white">
  458. Simpan Evaluasi
  459. </span>
  460. </Button>
  461. </div>
  462. </FormGroup>
  463. </Form>
  464. )}
  465. </Formik>
  466. </Card>
  467. )}
  468. </>
  469. );
  470. }
  471. }
  472. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  473. export default connect(mapStateToProps)(InputEvaluasi);