InputTanggal.js 26 KB

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