ModalPermohonan.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { addKeberatan } from "@/actions/keberatan";
  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 Link from "next/link";
  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("Required").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.setState({ error: null });
  86. this.props.toggleModal(false);
  87. this.setState({
  88. modal1: !this.state.modal1,
  89. });
  90. };
  91. onSubmit = async (data) => {
  92. const getToken = await getCsrf();
  93. const _csrf = getToken.token;
  94. this.setState({
  95. modal1: !this.state.modal1,
  96. });
  97. const { query, token } = this.props;
  98. const { id } = query;
  99. const formdata = new FormData();
  100. if (data.dokumen.length > 0) {
  101. data.dokumen.forEach((e) => {
  102. formdata.append("dokumen", e);
  103. });
  104. }
  105. const tostid = toast.loading("Please wait...");
  106. const success = await addKeberatan(token, id, formdata, _csrf);
  107. if (!success) {
  108. toast.update(tostid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  109. } else {
  110. toast.update(tostid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  111. Router.push({
  112. pathname: "/pt/sanksi/jawaban-keberatan/detail", query: { id: id }
  113. });
  114. }
  115. };
  116. render() {
  117. const { files } = this.state;
  118. const { sanksi } = this.props
  119. const removeFile = file => () => {
  120. const newFiles = [...files]
  121. newFiles.splice(newFiles.indexOf(file), 1)
  122. this.setState({
  123. files: newFiles,
  124. });
  125. }
  126. const thumbs = files.map((file, index) => (
  127. <p>
  128. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  129. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  130. </p>
  131. ));
  132. return (
  133. <>
  134. <Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
  135. <ModalBody>Apakah anda akan mengajukan permohonan keberatan atas pengenaan sanksi?</ModalBody>
  136. <ModalFooter>
  137. <Button color className="btn-login" onClick={this.toggleModal1}>
  138. <span className="font-color-white">Ya</span>
  139. </Button>
  140. <Button color className="btn-v2" onClick={this.props.toggleModal}>
  141. Tidak
  142. </Button>
  143. </ModalFooter>
  144. </Modal>
  145. <Modal isOpen={this.state.modal1} toggle={this.toggleModal1}>
  146. <ModalHeader toggle={this.toggleModal1}>Unggah Dokumen Permohonan Keberatan</ModalHeader>
  147. <Formik
  148. initialValues={{
  149. dokumen: [],
  150. }}
  151. validationSchema={evaluasiSchema}
  152. onSubmit={this.onSubmit}
  153. >
  154. {({ isSubmitting }) => (
  155. <Form className="form-horizontal">
  156. <ModalBody>
  157. <FormGroup>
  158. <label>Dalam hal mengajukan permohonan keberatan maka wajib mengunggah surat permohonan keberatan & dokumen pendukungnya</label>
  159. <div>
  160. <Field name="dokumen">
  161. {({ field, form, meta }) => (
  162. <DropzoneWrapper
  163. className=""
  164. onDrop={(e) => {
  165. this.onDrop(e);
  166. form.setFieldValue(field.name, e);
  167. }}
  168. >
  169. {({ getRootProps, getInputProps, isDragActive }) => {
  170. return (
  171. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  172. <input {...getInputProps()} />
  173. <div className="dropzone-previews flex">
  174. <div className="dropzone-style-1">
  175. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  176. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  177. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  178. </div> :
  179. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  180. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  181. </div>
  182. }
  183. </div>
  184. </div>
  185. </div>
  186. <div className="d-flex align-items-center">
  187. <small className="ml-auto">
  188. <button
  189. type="button"
  190. className="btn btn-link"
  191. onClick={(e) => {
  192. this.clearFiles(e);
  193. form.setFieldValue(field.name, []);
  194. }}
  195. >
  196. Reset dokumen
  197. </button>
  198. </small>
  199. </div>
  200. </div>
  201. );
  202. }}
  203. </DropzoneWrapper>
  204. )}
  205. </Field>
  206. <div>
  207. {thumbs}
  208. </div>
  209. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  210. <p className="mrgn-top-5 font-color-black">
  211. Ukuran setiap dokumen maksimal 15mb
  212. </p>
  213. </div>
  214. </FormGroup>
  215. </ModalBody>
  216. <ModalFooter>
  217. <Button color className="btn-login" type="submit" disabled={isSubmitting}>
  218. <span className="font-color-white">Kirim</span>
  219. </Button>
  220. </ModalFooter>
  221. </Form>
  222. )}
  223. </Formik>
  224. </Modal>
  225. </>
  226. );
  227. }
  228. }
  229. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  230. export default connect(mapStateToProps)(ModalPermohonan);