ModalPermohonan.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import { Row, Col, FormGroup, Button, Modal, ModalHeader, ModalBody, ModalFooter, Progress } from "reactstrap";
  4. import { addBanding } from "@/actions/banding";
  5. import { connect } from "react-redux";
  6. import { toast } from "react-toastify";
  7. import { Formik, Form, Field, ErrorMessage } from "formik";
  8. import * as Yup from "yup";
  9. import { updatePT } from "@/actions/sanksi";
  10. import { getCsrf } from "../../../actions/security";
  11. let Dropzone = null;
  12. class DropzoneWrapper extends Component {
  13. state = {
  14. isClient: false,
  15. };
  16. componentDidMount = () => {
  17. Dropzone = require("react-dropzone").default;
  18. this.setState({ isClient: true });
  19. };
  20. render() {
  21. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  22. }
  23. }
  24. const checkIfFilesAreTooBig = (files) => {
  25. let valid = true;
  26. if (files) {
  27. files.map((file) => {
  28. if (file.size > 15 * 1024 * 1024) {
  29. valid = false;
  30. }
  31. });
  32. }
  33. return valid;
  34. };
  35. const checkIfFilesAreCorrectType = (files) => {
  36. let valid = true;
  37. if (files) {
  38. files.map((file) => {
  39. if (!["image/jpeg", "image/png"].includes(file.type)) {
  40. valid = false;
  41. }
  42. });
  43. }
  44. return valid;
  45. };
  46. const evaluasiSchema = Yup.object().shape({
  47. dokumen: Yup.array().min(1, "Minimal terdapat 1 dokumen").required("Harus ada").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  48. });
  49. export class ModalPermohonan extends Component {
  50. constructor(props) {
  51. super(props);
  52. this.state = {
  53. modal1: false,
  54. files: [],
  55. };
  56. }
  57. onDrop = (files) => {
  58. this.setState({
  59. files: files.map((file) =>
  60. Object.assign(file, {
  61. preview: URL.createObjectURL(file),
  62. })
  63. ),
  64. stat: "Added " + files.length + " file(s)",
  65. });
  66. };
  67. uploadFiles = (e) => {
  68. e.preventDefault();
  69. e.stopPropagation();
  70. this.setState({
  71. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  72. });
  73. };
  74. clearFiles = (e) => {
  75. e.preventDefault();
  76. e.stopPropagation();
  77. this.setState({
  78. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  79. });
  80. this.setState({
  81. files: [],
  82. });
  83. };
  84. toggleModal1 = () => {
  85. this.props.toggleModal(false);
  86. this.setState({
  87. modal1: !this.state.modal1,
  88. });
  89. };
  90. is_pengajuan_banding_true = async () => {
  91. const { query, token } = this.props;
  92. const getTokenCsrf = await getCsrf();
  93. const _csrf = getTokenCsrf.token;
  94. await updatePT(token, query.id, { is_pengajuan_banding: true }, _csrf)
  95. }
  96. onSubmit = async (data) => {
  97. const getTokenCsrf = await getCsrf();
  98. const _csrf = getTokenCsrf.token;
  99. this.props.toggleModal()
  100. const { query, token } = this.props;
  101. const { id } = query;
  102. const formdata = new FormData();
  103. if (data.dokumen.length > 0) {
  104. data.dokumen.forEach((e) => {
  105. formdata.append("dokumen", e);
  106. });
  107. }
  108. const toastid = toast.loading("Please wait...");
  109. const added = await addBanding(token, id, formdata, _csrf);
  110. this.is_pengajuan_banding_true()
  111. // Router.push({
  112. // pathname: "/pt/jawaban-banding/detail", query: { id: id }
  113. // });
  114. if (!added) {
  115. toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  116. } else {
  117. toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  118. Router.push({
  119. pathname: "/pt/sanksi/jawaban-banding/detail", query: { id: id }
  120. });
  121. }
  122. };
  123. render() {
  124. const { files } = this.state;
  125. const removeFile = file => () => {
  126. const newFiles = [...files]
  127. newFiles.splice(newFiles.indexOf(file), 1)
  128. this.setState({
  129. files: newFiles,
  130. });
  131. }
  132. const thumbs = files.map((file, index) => (
  133. <p>
  134. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  135. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  136. </p>
  137. ));
  138. return (
  139. <>
  140. <Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
  141. <ModalHeader >Upload Dokumen Banding</ModalHeader>
  142. <Formik
  143. initialValues={{
  144. dokumen: [],
  145. }}
  146. validationSchema={evaluasiSchema}
  147. onSubmit={this.onSubmit}
  148. >{({ isSubmitting }) => (
  149. <Form className="form-horizontal">
  150. <ModalBody>
  151. <FormGroup>
  152. <label>Dalam hal mengajukan permohonan banding maka wajib mengunggah surat permohonan banding & dokumen pendukungnya</label>
  153. <div>
  154. <Field name="dokumen">
  155. {({ field, form, meta }) => (
  156. <DropzoneWrapper
  157. className=""
  158. onDrop={(e) => {
  159. this.onDrop(e);
  160. form.setFieldValue(field.name, e);
  161. }}
  162. >
  163. {({ getRootProps, getInputProps, isDragActive }) => {
  164. return (
  165. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  166. <input {...getInputProps()} />
  167. <div className="dropzone-previews flex">
  168. <div className="dropzone-style-1">
  169. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  170. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  171. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  172. </div> :
  173. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  174. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  175. </div>
  176. }
  177. </div>
  178. </div>
  179. </div>
  180. <div className="d-flex align-items-center">
  181. <small className="ml-auto">
  182. <button
  183. type="button"
  184. className="btn btn-link"
  185. onClick={(e) => {
  186. this.clearFiles(e);
  187. form.setFieldValue(field.name, []);
  188. }}
  189. >
  190. Reset dokumen
  191. </button>
  192. </small>
  193. </div>
  194. </div>
  195. );
  196. }}
  197. </DropzoneWrapper>
  198. )}
  199. </Field>
  200. <div>
  201. {thumbs}
  202. </div>
  203. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  204. <p className="mrgn-top-5 font-color-black">
  205. Ukuran setiap dokumen maksimal 15mb
  206. </p>
  207. </div>
  208. </FormGroup>
  209. </ModalBody>
  210. <ModalFooter>
  211. <Button color className="btn-login" type="submit" disabled={isSubmitting}>
  212. <span className="font-color-white">Kirim</span>
  213. </Button>
  214. </ModalFooter>
  215. </Form>
  216. )}
  217. </Formik>
  218. </Modal>
  219. </>
  220. );
  221. }
  222. }
  223. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  224. export default connect(mapStateToProps)(ModalPermohonan);