ModalPermohonan.js 7.4 KB

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