InputTanggal.js 21 KB

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