InputTanggal.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import React, { Component } from "react";
  2. import { Row, Col, Card, CardBody, FormGroup, Input, Button, Progress } from "reactstrap";
  3. import { toast } from "react-toastify";
  4. import { Formik, Form, Field, ErrorMessage } from "formik";
  5. import * as Yup from "yup";
  6. import { connect } from "react-redux";
  7. import { getOneSanksi, update } from "@/actions/sanksi";
  8. import { addDays, addMonths } from 'date-fns';
  9. import id from 'date-fns/locale/id';
  10. import moment from "moment";
  11. import 'moment/locale/id';
  12. moment.locale('id');
  13. import Router from "next/router";
  14. import { getPelanggaranSanksi } from "@/actions/pelanggaran";
  15. import Select from "react-select";
  16. import { getCsrf } from "../../actions/security";
  17. import Swal from "sweetalert2";
  18. import Datetime from "react-datetime";
  19. const selectInstanceId = 1;
  20. const checkIfFilesAreTooBig = (files) => {
  21. let valid = true;
  22. if (files) {
  23. files.map((file) => {
  24. if (file.size > 15 * 1024 * 1024) {
  25. valid = false;
  26. }
  27. });
  28. }
  29. return valid;
  30. };
  31. const checkIfFilesAreCorrectType = (files) => {
  32. let valid = true;
  33. if (files) {
  34. files.map((file) => {
  35. if (!["image/jpeg", "image/png"].includes(file.type)) {
  36. valid = false;
  37. }
  38. });
  39. }
  40. return valid;
  41. };
  42. const rekomendasiSchema = Yup.object().shape({
  43. no_sanksi: Yup.string().required("Wajib isi Nomor Sanksi"),
  44. keterangan: Yup.string().min(3, "Minimal 3 Huruf").max(200).required("Wajib isi keterangan"),
  45. from_date: Yup.date().required("Wajib diisi"),
  46. to_date: Yup.date().notRequired("Wajib diisi"),
  47. sanksi: Yup.array().required("Wajib isi pelanggaran"),
  48. dokumen: Yup.array().required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  49. });
  50. let Dropzone = null;
  51. const level_sanksi = {
  52. "Sanksi Administratif Ringan": 1,
  53. "Sanksi Administratif Sedang": 2,
  54. "Sanksi Administratif Berat": 3,
  55. }
  56. class DropzoneWrapper extends Component {
  57. state = {
  58. isClient: false,
  59. };
  60. componentDidMount = () => {
  61. Dropzone = require("react-dropzone").default;
  62. this.setState({ isClient: true });
  63. };
  64. render() {
  65. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  66. }
  67. }
  68. class InputTanggal extends Component {
  69. constructor(props) {
  70. super(props);
  71. const tmt_awal = new Date();
  72. this.state = {
  73. dropdownOpen: false,
  74. splitButtonOpen: false,
  75. files: [],
  76. sanksi: {},
  77. data: {},
  78. from_date: "",
  79. to_date: "",
  80. startDay: tmt_awal,
  81. keteranganLaporan: "",
  82. tmtCheck: false,
  83. listSanksi: "",
  84. selectedFile: {}
  85. };
  86. }
  87. async componentDidMount() {
  88. const { token, query } = this.props;
  89. const { id } = query;
  90. const sanksi = await getOneSanksi(token, id);
  91. const { data: listSanksi } = await getPelanggaranSanksi(token, { down: true })
  92. this.setState({ sanksi, listSanksi })
  93. }
  94. toggleSplit = () => {
  95. this.setState({
  96. splitButtonOpen: !this.state.splitButtonOpen,
  97. });
  98. };
  99. toggleDropDown = () => {
  100. this.setState({
  101. dropdownOpen: !this.state.dropdownOpen,
  102. });
  103. };
  104. onDrop = (selectedFile) => {
  105. this.setState({
  106. selectedFile: selectedFile.map((file) =>
  107. Object.assign(file, {
  108. preview: URL.createObjectURL(file),
  109. })
  110. ),
  111. stat: "Added " + selectedFile.length + " file(s)",
  112. });
  113. const selectFile = this.state.selectedFile
  114. this.setState(prevState => ({
  115. files: [...prevState.files, ...selectFile]
  116. }))
  117. };
  118. uploadFiles = (e) => {
  119. e.preventDefault();
  120. e.stopPropagation();
  121. this.setState({
  122. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  123. });
  124. };
  125. clearFiles = (e) => {
  126. e.preventDefault();
  127. e.stopPropagation();
  128. this.setState({
  129. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  130. });
  131. this.setState({
  132. files: [],
  133. });
  134. };
  135. static getInitialProps = async ({ query }) => {
  136. return { query };
  137. };
  138. setKeteranganPelaporan = (e) => {
  139. this.setState({ keteranganLaporan: e.target.value });
  140. };
  141. handleTmtCheck = () => {
  142. this.setState({ tmtCheck: !this.state.tmtCheck });
  143. };
  144. done = async (data) => {
  145. if (this.props?.user?.role.id === 2024) {
  146. Swal.fire({
  147. icon: 'error',
  148. title: 'Oops...',
  149. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  150. confirmButtonColor: "#3e3a8e",
  151. confirmButtonText: 'Oke'
  152. })
  153. } else {
  154. this.setState({
  155. loading: true
  156. })
  157. const sanksi = await this.ubahSanksi(data)
  158. if (sanksi && ENV === "production") {
  159. await this.updatePddikti(sanksi.data._id) //kirim sanksiID ke function updatePDDIKTI
  160. }
  161. await Router.push({
  162. pathname: "/app/turun-sanksi",
  163. });
  164. }
  165. };
  166. updatePddikti = async (sanksiId) => {
  167. const getToken = await getCsrf();
  168. const _csrf2 = getToken.token;
  169. const toastPddikti = toast.loading("Updating pddikti...");
  170. try {
  171. const { query, token } = this.props;
  172. const { id } = query;
  173. await updatePddikti(token, sanksiId, _csrf2)
  174. toast.update(toastPddikti, { render: "Berhasil Update PDDIKTI", type: "success", isLoading: false, autoClose: true, closeButton: true });
  175. }
  176. catch (error) {
  177. toast.update(toastPddikti, { render: ("Gagal Update PDDIKTI"), type: "error", isLoading: false, autoClose: true, closeButton: true });
  178. }
  179. }
  180. ubahSanksi = async (data) => {
  181. if (this.props?.user?.role.id === 2024) {
  182. Swal.fire({
  183. icon: 'error',
  184. title: 'Oops...',
  185. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  186. confirmButtonColor: "#3e3a8e",
  187. confirmButtonText: 'Oke'
  188. })
  189. } else {
  190. const getToken = await getCsrf();
  191. const _csrf = getToken.token;
  192. const { token, query } = this.props;
  193. const { id } = query;
  194. const formdata = new FormData();
  195. formdata.append("no_sanksi", data.no_sanksi);
  196. formdata.append("keterangan", data.keterangan);
  197. formdata.append("from_date", data.from_date);
  198. formdata.append("to_date", data.to_date);
  199. formdata.append("sanksi", JSON.stringify(data.sanksi.map((e) => ({ label: e.value.split(";")[0], description: e.value.split(";")[1], level: e.value.split(";")[2] }))));
  200. this.state.files.forEach((e) => {
  201. formdata.append("dokumen", e);
  202. });
  203. const toastid = toast.loading("Please wait...");
  204. const added = await update(token, id, formdata, _csrf);
  205. if (!added) {
  206. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  207. } else {
  208. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  209. }
  210. }
  211. };
  212. render() {
  213. const { files } = this.state;
  214. const removeFile = file => () => {
  215. const newFiles = [...files]
  216. newFiles.splice(newFiles.indexOf(file), 1)
  217. this.setState({
  218. files: newFiles,
  219. });
  220. }
  221. const thumbs = files.map((file, index) => (
  222. <p>
  223. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  224. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  225. </p>
  226. ));
  227. return (
  228. <Card className="card-default">
  229. <CardBody>
  230. <p className="lead bb">Dokumen Surat Turun Sanksi</p>
  231. <Formik
  232. initialValues={{
  233. no_sanksi: "",
  234. keterangan: "",
  235. from_date: "",
  236. to_date: "",
  237. sanksi: [],
  238. dokumen: [],
  239. }}
  240. validationSchema={rekomendasiSchema}
  241. onSubmit={this.done}
  242. >
  243. {({ isSubmitting }) => (
  244. <Form className="form-horizontal">
  245. <FormGroup row>
  246. <label className="col-md-2 col-form-label">Nomor Surat</label>
  247. <div className="col-md-10">
  248. <Field name="no_sanksi">{({ field }) => <Input type="textarea" placeholder="Nomor Surat" {...field} />}</Field>
  249. <ErrorMessage name="no_sanksi" component="div" className="form-text text-danger" />
  250. </div>
  251. </FormGroup>
  252. <FormGroup row>
  253. <label className="col-md-2 col-form-label">Keterangan</label>
  254. <div className="col-md-10">
  255. <Field name="keterangan">{({ field }) => <Input type="textarea" placeholder="Keterangan" {...field} />}</Field>
  256. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  257. </div>
  258. </FormGroup>
  259. <FormGroup row>
  260. <label className="col-md-2 col-form-label">Tidak Perlu TMT</label>
  261. <div className="col-md-10 mt-2">
  262. <div className="checkbox c-checkbox">
  263. <label>
  264. <Input type="checkbox" onChange={this.handleTmtCheck} defaultChecked={this.state.tmtCheck} />
  265. <span className="fa fa-check"></span></label>
  266. </div>
  267. </div>
  268. </FormGroup>
  269. {this.state.tmtCheck && (
  270. <FormGroup row className="mt-3">
  271. <label className="col-md-2 col-form-label">Tanggal Penetapan Sanksi</label>
  272. <span className="col-sm-3 float-left">
  273. <Field name="from_date">
  274. {({ field, form }) => (
  275. <Datetime
  276. timeFormat={false}
  277. inputProps={{ className: "form-control" }}
  278. value={field.value || "DD/MM/YYYY"}
  279. onChange={(from_date) => {
  280. form.setFieldValue(field.name, from_date);
  281. this.setState({ from_date })
  282. }}
  283. closeOnSelect={true}
  284. isValidDate={(e) => {
  285. return e.isBefore(new Date())
  286. }}
  287. />
  288. )}
  289. </Field>
  290. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  291. </span>
  292. </FormGroup>
  293. )}
  294. {!this.state.tmtCheck && (
  295. <FormGroup row className="mt-3">
  296. <label className="col-md-2 col-form-label">Isi TMT :</label>
  297. <div className="col-md-6">
  298. <Row >
  299. <Col>
  300. <FormGroup>
  301. <Field name="from_date">
  302. {({ field, form }) => (
  303. <Datetime
  304. timeFormat={false}
  305. inputProps={{ className: "form-control" }}
  306. value={field.value || "DD/MM/YYYY"}
  307. onChange={(from_date) => {
  308. form.setFieldValue(field.name, from_date);
  309. this.setState({ from_date })
  310. }}
  311. closeOnSelect={true}
  312. isValidDate={(e) => {
  313. return e.isBefore(new Date())
  314. }}
  315. />
  316. )}
  317. </Field>
  318. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  319. </FormGroup>
  320. </Col>
  321. <Col>
  322. <FormGroup>
  323. <Field name="to_date">
  324. {({ field, form }) => (
  325. <Datetime
  326. timeFormat={false}
  327. inputProps={{ className: "form-control" }}
  328. value={field.value || "DD/MM/YYYY"}
  329. onChange={(to_date) => {
  330. form.setFieldValue(field.name, to_date);
  331. this.setState({ to_date })
  332. }}
  333. closeOnSelect={true}
  334. isValidDate={(e) => {
  335. return e.isBefore(addMonths(new Date(this.state.from_date), 6)) && e.isAfter(new Date(this.state.from_date))
  336. }}
  337. />
  338. )}
  339. </Field>
  340. <ErrorMessage name="to_date" component="div" className="form-text text-danger" />
  341. </FormGroup>
  342. </Col>
  343. </Row>
  344. </div>
  345. </FormGroup>
  346. )}
  347. {!this.state.tmtCheck && (
  348. <FormGroup row className="mt-1">
  349. <label className="col-md-2 col-form-label">TMT berlaku</label>
  350. <div className="col-md-10 mt-2">
  351. <b>{this.state.from_date ? moment(this.state.from_date).format("DD-MM-YYYY") : "-"}</b> hingga <b>{this.state.to_date ? moment(this.state.to_date).format("DD-MM-YYYY") : "-"}</b>
  352. </div>
  353. </FormGroup>
  354. )}
  355. {!this.state.tmtCheck && (
  356. <FormGroup row className="mt-1">
  357. <label className="col-md-2 col-form-label">TMT</label>
  358. <div className="col-md-10 mt-2">
  359. <b>{this.state.to_date ? moment(this.state.to_date).diff(this.state.from_date, 'month') : "-"} bulan</b>
  360. </div>
  361. </FormGroup>
  362. )}
  363. <FormGroup row className="mt-3">
  364. <label className="col-md-2 col-form-label">List sanksi </label>
  365. <div className="col-md-10">
  366. <Field name="sanksi">{({ field, form }) => <Select
  367. options={this.props.listSanksi.map(e => ({ value: e, label: `Sanksi Administratif ${e.split(";")[0]} - ${e.split(";")[1]}` }))}
  368. isMulti
  369. onChange={(e) => {
  370. form.setFieldValue(field.name, e);
  371. }}
  372. />}</Field>
  373. <ErrorMessage name="sanksi" component="div" className="form-text text-danger" />
  374. </div>
  375. </FormGroup>
  376. <FormGroup row className="mt-3">
  377. <label className="col-md-2 col-form-label">Dokumen Perubahan sanksi : <span className="text-danger">*</span></label>
  378. <div className="col-md-10">
  379. <Field name="dokumen">
  380. {({ field, form }) => (
  381. <DropzoneWrapper
  382. className=""
  383. onDrop={(e) => {
  384. this.onDrop(e);
  385. form.setFieldValue(field.name, e);
  386. }}
  387. >
  388. {({ getRootProps, getInputProps, isDragActive }) => {
  389. return (
  390. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  391. <input {...getInputProps()} />
  392. <div className="dropzone-previews flex">
  393. <div className="dropzone-style-1">
  394. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  395. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  396. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  397. </div> :
  398. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  399. <h5 className="text-center dz-default dz-message">Upload dokumen Turun Sanksi</h5>
  400. </div>
  401. }
  402. </div>
  403. </div>
  404. </div>
  405. <div className="d-flex align-items-center">
  406. <small className="ml-auto">
  407. <button
  408. type="button"
  409. className="btn btn-link"
  410. onClick={(e) => {
  411. this.clearFiles(e);
  412. form.setFieldValue(field.name, []);
  413. }}
  414. >
  415. Reset dokumen
  416. </button>
  417. </small>
  418. </div>
  419. </div>
  420. );
  421. }}
  422. </DropzoneWrapper>
  423. )}
  424. </Field>
  425. <div>
  426. {thumbs}
  427. </div>
  428. </div>
  429. </FormGroup>
  430. <FormGroup row>
  431. <div className="col-xl-10">
  432. <Button color className="color-3e3a8e btn-login" type="submit" disabled={isSubmitting}>
  433. <span className="font-color-white">Kirim</span>
  434. </Button>
  435. </div>
  436. </FormGroup>
  437. </Form>
  438. )}
  439. </Formik>
  440. </CardBody>
  441. </Card>
  442. );
  443. }
  444. }
  445. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  446. export default connect(mapStateToProps)(InputTanggal);