ModalPermohonan.js 6.4 KB

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