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_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)
  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/naik-sanksi");
  153. }
  154. };
  155. render() {
  156. const { files } = this.state;
  157. const thumbs = files.map((file, index) => (
  158. <div md={3} key={index}>
  159. <span className="text-left">{index + 1}.{file.name}</span>
  160. </div>
  161. ));
  162. return (
  163. <Card className="card-default">
  164. <CardBody>
  165. <p className="lead bb">Dokumen Surat Naik Sanksi</p>
  166. <Formik
  167. initialValues={{
  168. no_sanksi: "",
  169. keterangan: "",
  170. from_date: "",
  171. to_date: "",
  172. sanksi: [],
  173. dokumen: [],
  174. }}
  175. validationSchema={rekomendasiSchema}
  176. onSubmit={this.handelSimpan}
  177. >
  178. {() => (
  179. <Form className="form-horizontal">
  180. <FormGroup row>
  181. <label className="col-md-2 col-form-label">Nomor Sanksi</label>
  182. <div className="col-md-10">
  183. <Field name="no_sanksi">{({ field }) => <Input type="textarea" placeholder="Nomor Sanksi" {...field} />}</Field>
  184. <ErrorMessage name="no_sanksi" component="div" className="form-text text-danger" />
  185. </div>
  186. </FormGroup>
  187. <FormGroup row>
  188. <label className="col-md-2 col-form-label">Keterangan</label>
  189. <div className="col-md-10">
  190. <Field name="keterangan">{({ field }) => <Input type="textarea" placeholder="Keterangan" {...field} />}</Field>
  191. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  192. </div>
  193. </FormGroup>
  194. <FormGroup row>
  195. <label className="col-md-2 col-form-label">Tidak Perlu TMT</label>
  196. <div className="col-md-10 mt-2">
  197. <div className="checkbox c-checkbox">
  198. <label>
  199. <Input type="checkbox" onChange={this.handleTmtCheck} defaultChecked={this.state.tmtCheck} />
  200. <span className="fa fa-check"></span></label>
  201. </div>
  202. </div>
  203. </FormGroup>
  204. {this.state.tmtCheck && (
  205. <FormGroup row className="mt-3">
  206. <label className="col-md-2 col-form-label">Tanggal Penetapan Sanksi</label>
  207. <span className="col-sm-3 float-left">
  208. <Field name="from_date">
  209. {({ field, form }) => (
  210. <DatePicker
  211. selected={this.state.from_date}
  212. onChange={(from_date) => {
  213. this.setState({ from_date })
  214. form.setFieldValue(field.name, from_date);
  215. }}
  216. dateFormat="dd/MM/yyyy"
  217. maxDate={this.state.startDay}
  218. placeholderText="Dari Tanggal"
  219. locale={id}
  220. className="form-control bg-white"
  221. />
  222. )}
  223. </Field>
  224. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  225. </span>
  226. </FormGroup>
  227. )}
  228. {!this.state.tmtCheck && (
  229. <FormGroup row className="mt-3">
  230. <label className="col-md-2 col-form-label">Isi TMT :</label>
  231. <div className="col-md-6">
  232. <Row >
  233. <Col>
  234. <FormGroup>
  235. <Field name="from_date">
  236. {({ field, form }) => (
  237. <DatePicker
  238. selected={this.state.from_date}
  239. onChange={(from_date) => {
  240. this.setState({ from_date })
  241. form.setFieldValue(field.name, from_date);
  242. }}
  243. dateFormat="dd/MM/yyyy"
  244. maxDate={this.state.startDay}
  245. placeholderText="Dari Tanggal"
  246. locale={id}
  247. className="form-control bg-white"
  248. />
  249. )}
  250. </Field>
  251. <ErrorMessage name="from_date" component="div" className="form-text text-danger" />
  252. </FormGroup>
  253. </Col>
  254. <Col>
  255. <FormGroup>
  256. <Field name="to_date">
  257. {({ field, form }) => (
  258. <DatePicker
  259. selected={this.state.to_date}
  260. onChange={(to_date) => {
  261. this.setState({ to_date })
  262. form.setFieldValue(field.name, to_date);
  263. }}
  264. dateFormat="dd/MM/yyyy"
  265. minDate={this.state.from_date}
  266. maxDate={addMonths(new Date(this.state.from_date), 6)}
  267. placeholderText="Sampai tanggal"
  268. locale={id}
  269. className="form-control bg-white"
  270. />
  271. )}
  272. </Field>
  273. <ErrorMessage name="to_date" component="div" className="form-text text-danger" />
  274. </FormGroup>
  275. </Col>
  276. </Row>
  277. </div>
  278. </FormGroup>
  279. )}
  280. {!this.state.tmtCheck && (
  281. <FormGroup row className="mt-1">
  282. <label className="col-md-2 col-form-label">TMT berlaku</label>
  283. <div className="col-md-10 mt-2">
  284. <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>
  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</label>
  291. <div className="col-md-10 mt-2">
  292. <b>{this.state.to_date ? moment(this.state.to_date).diff(this.state.from_date, 'month') : "-"} bulan</b>
  293. </div>
  294. </FormGroup>
  295. )}
  296. <FormGroup row className="mt-3">
  297. <label className="col-md-2 col-form-label">List sanksi </label>
  298. <div className="col-md-10">
  299. <Field name="sanksi">{({ field, form }) => <Select
  300. options={this.props.listSanksi.map(e => ({ value: e, label: e }))}
  301. isMulti
  302. onChange={(e) => {
  303. form.setFieldValue(field.name, e);
  304. }}
  305. />}</Field>
  306. <ErrorMessage name="sanksi" 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);