InputTanggal.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import React, { Component } from "react";
  2. import { Row, Col, Card, CardBody, FormGroup, Input, Button, Modal, ModalHeader, ModalBody, ModalFooter, CardHeader, CardTitle } 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_surat: Yup.string().required("Wajib isi no 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. pelanggaran_id: 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. };
  79. }
  80. async componentDidMount() {
  81. const { token, query } = this.props;
  82. const { id } = query;
  83. const sanksi = await getOneSanksi(token, id);
  84. const { data: listSanksi } = await getPelanggaranSanksi(token)
  85. this.setState({ sanksi, listSanksi })
  86. }
  87. toggleSplit = () => {
  88. this.setState({
  89. splitButtonOpen: !this.state.splitButtonOpen,
  90. });
  91. };
  92. toggleDropDown = () => {
  93. this.setState({
  94. dropdownOpen: !this.state.dropdownOpen,
  95. });
  96. };
  97. onDrop = (files) => {
  98. this.setState({
  99. files: files.map((file) =>
  100. Object.assign(file, {
  101. preview: URL.createObjectURL(file),
  102. })
  103. ),
  104. stat: "Added " + files.length + " file(s)",
  105. });
  106. };
  107. uploadFiles = (e) => {
  108. e.preventDefault();
  109. e.stopPropagation();
  110. this.setState({
  111. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  112. });
  113. };
  114. clearFiles = (e) => {
  115. e.preventDefault();
  116. e.stopPropagation();
  117. this.setState({
  118. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  119. });
  120. this.setState({
  121. files: [],
  122. });
  123. };
  124. static getInitialProps = async ({ query }) => {
  125. return { query };
  126. };
  127. setKeteranganPelaporan = (e) => {
  128. this.setState({ keteranganLaporan: e.target.value });
  129. };
  130. handleTmtCheck = () => {
  131. this.setState({ tmtCheck: !this.state.tmtCheck });
  132. }
  133. handelSimpan = async (data) => {
  134. const { token, query } = this.props;
  135. const { id } = query;
  136. const formdata = new FormData();
  137. formdata.append("no_surat", data.no_surat);
  138. formdata.append("keterangan", data.keterangan);
  139. formdata.append("from_date", data.from_date);
  140. formdata.append("to_date", data.to_date);
  141. formdata.append("pelanggaran_id", data.pelanggaran_id);
  142. this.state.files.forEach((e) => {
  143. formdata.append("dokumen", e);
  144. });
  145. const toastid = toast.loading("Please wait...");
  146. const added = await update(token, id, formdata);
  147. if (!added) {
  148. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  149. } else {
  150. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  151. Router.push("/app/naik-sanksi");
  152. }
  153. };
  154. render() {
  155. const { files } = this.state;
  156. const thumbs = files.map((file, index) => (
  157. <div md={3} key={index}>
  158. <span className="text-left">{index + 1}.{file.name}</span>
  159. </div>
  160. ));
  161. return (
  162. <Card className="card-default">
  163. <CardBody>
  164. <p className="lead bb">Dokumen Surat Naik Sanksi</p>
  165. <Formik
  166. initialValues={{
  167. no_surat: "",
  168. keterangan: "",
  169. from_date: "",
  170. to_date: "",
  171. pelanggaran_id: [],
  172. dokumen: [],
  173. }}
  174. validationSchema={rekomendasiSchema}
  175. onSubmit={this.handelSimpan}
  176. >
  177. {() => (
  178. <Form className="form-horizontal">
  179. <FormGroup row>
  180. <label className="col-md-2 col-form-label">Nomor Sanksi</label>
  181. <div className="col-md-10">
  182. <Field name="no_surat">{({ field }) => <Input type="textarea" placeholder="Nomor Sanksi" {...field} />}</Field>
  183. <ErrorMessage name="no_surat" component="div" className="form-text text-danger" />
  184. </div>
  185. </FormGroup>
  186. <FormGroup row>
  187. <label className="col-md-2 col-form-label">Keterangan</label>
  188. <div className="col-md-10">
  189. <Field name="keterangan">{({ field }) => <Input type="textarea" placeholder="Keterangan" {...field} />}</Field>
  190. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  191. </div>
  192. </FormGroup>
  193. <FormGroup row>
  194. <label className="col-md-2 col-form-label">Tidak Perlu TMT</label>
  195. <div className="col-md-10 mt-2">
  196. <div className="checkbox c-checkbox">
  197. <label>
  198. <Input type="checkbox" onChange={this.handleTmtCheck} defaultChecked={this.state.tmtCheck} />
  199. <span className="fa fa-check"></span></label>
  200. </div>
  201. </div>
  202. </FormGroup>
  203. {this.state.tmtCheck && (
  204. <FormGroup row className="mt-3">
  205. <label className="col-md-2 col-form-label">Tanggal Penetapan Sanksi</label>
  206. <span className="col-sm-3 float-left">
  207. <Field name="from_date">
  208. {({ field, form }) => (
  209. <DatePicker
  210. selected={this.state.from_date}
  211. onChange={(from_date) => {
  212. this.setState({ from_date })
  213. form.setFieldValue(field.name, from_date);
  214. }}
  215. dateFormat="dd/MM/yyyy"
  216. maxDate={this.state.startDay}
  217. placeholderText="Dari Tanggal"
  218. locale={id}
  219. className="form-control bg-white"
  220. />
  221. )}
  222. </Field>
  223. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  224. </span>
  225. </FormGroup>
  226. )}
  227. {!this.state.tmtCheck && (
  228. <FormGroup row className="mt-3">
  229. <label className="col-md-2 col-form-label">Isi TMT :</label>
  230. <div className="col-md-6">
  231. <Row >
  232. <Col>
  233. <FormGroup>
  234. <Field name="from_date">
  235. {({ field, form }) => (
  236. <DatePicker
  237. selected={this.state.from_date}
  238. onChange={(from_date) => {
  239. this.setState({ from_date })
  240. form.setFieldValue(field.name, from_date);
  241. }}
  242. dateFormat="dd/MM/yyyy"
  243. maxDate={this.state.startDay}
  244. placeholderText="Dari Tanggal"
  245. locale={id}
  246. className="form-control bg-white"
  247. />
  248. )}
  249. </Field>
  250. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  251. </FormGroup>
  252. </Col>
  253. <Col>
  254. <FormGroup>
  255. <Field name="to_date">
  256. {({ field, form }) => (
  257. <DatePicker
  258. selected={this.state.to_date}
  259. onChange={(to_date) => {
  260. this.setState({ to_date })
  261. form.setFieldValue(field.name, to_date);
  262. }}
  263. dateFormat="dd/MM/yyyy"
  264. minDate={this.state.from_date}
  265. maxDate={addMonths(new Date(this.state.from_date), 6)}
  266. placeholderText="Sampai tanggal"
  267. locale={id}
  268. className="form-control bg-white"
  269. />
  270. )}
  271. </Field>
  272. <ErrorMessage name="to_date" component="div" className="form-text text-danger" />
  273. </FormGroup>
  274. </Col>
  275. </Row>
  276. </div>
  277. </FormGroup>
  278. )}
  279. {!this.state.tmtCheck && (
  280. <FormGroup row className="mt-1">
  281. <label className="col-md-2 col-form-label">TMT berlaku</label>
  282. <div className="col-md-10 mt-2">
  283. <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>
  284. </div>
  285. </FormGroup>
  286. )}
  287. {!this.state.tmtCheck && (
  288. <FormGroup row className="mt-1">
  289. <label className="col-md-2 col-form-label">TMT</label>
  290. <div className="col-md-10 mt-2">
  291. <b>{this.state.to_date ? moment(this.state.to_date).diff(this.state.from_date, 'month') : "-"} bulan</b>
  292. </div>
  293. </FormGroup>
  294. )}
  295. <FormGroup row className="mt-3">
  296. <label className="col-md-2 col-form-label">List sanksi </label>
  297. <div className="col-md-10">
  298. <Field name="pelanggaran_id">{({ field, form }) => <Select
  299. options={this.props.listSanksi.map(e => ({ value: e, label: e }))}
  300. // formatOptionLabel={formatOptionLabel}
  301. isMulti
  302. onChange={(e) => {
  303. form.setFieldValue(field.name, e);
  304. }}
  305. />}</Field>
  306. <ErrorMessage name="pelanggaran_id" component="div" className="form-text text-danger" />
  307. </div>
  308. </FormGroup>
  309. <FormGroup row className="mt-3">
  310. <label className="col-md-2 col-form-label">Dokumen Perpanjangan Sanksi : <span className="text-danger">*</span></label>
  311. <div className="col-md-10">
  312. <Field name="dokumen">
  313. {({ field, form }) => (
  314. <DropzoneWrapper
  315. className=""
  316. onDrop={(e) => {
  317. this.onDrop(e);
  318. form.setFieldValue(field.name, e);
  319. }}
  320. >
  321. {({ getRootProps, getInputProps, isDragActive }) => {
  322. return (
  323. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  324. <input {...getInputProps()} />
  325. <div className="dropzone-previews flex">
  326. <div className="dropzone-style-1">
  327. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  328. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  329. <h5 className="text-center dz-default dz-message">Upload dokumen rekomendasi delegasi</h5>
  330. </div>
  331. }
  332. </div>
  333. </div>
  334. </div>
  335. <div className="d-flex align-items-center">
  336. <small className="ml-auto">
  337. <button
  338. type="button"
  339. className="btn btn-link"
  340. onClick={(e) => {
  341. this.clearFiles(e);
  342. form.setFieldValue(field.name, []);
  343. }}
  344. >
  345. Reset dokumen
  346. </button>
  347. </small>
  348. </div>
  349. </div>
  350. );
  351. }}
  352. </DropzoneWrapper>
  353. )}
  354. </Field>
  355. </div>
  356. </FormGroup>
  357. <FormGroup row>
  358. <div className="col-xl-10">
  359. <Button color className="color-3e3a8e btn-login" type="submit">
  360. <span className="font-color-white">Kirim</span>
  361. </Button>
  362. </div>
  363. </FormGroup>
  364. </Form>
  365. )}
  366. </Formik>
  367. </CardBody>
  368. </Card>
  369. );
  370. }
  371. }
  372. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  373. export default connect(mapStateToProps)(InputTanggal);