ModalPermohonan.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. let Dropzone = null;
  11. class DropzoneWrapper extends Component {
  12. state = {
  13. isClient: false,
  14. };
  15. componentDidMount = () => {
  16. Dropzone = require("react-dropzone").default;
  17. this.setState({ isClient: true });
  18. };
  19. render() {
  20. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  21. }
  22. }
  23. const checkIfFilesAreTooBig = (files) => {
  24. let valid = true;
  25. if (files) {
  26. files.map((file) => {
  27. if (file.size > 15 * 1024 * 1024) {
  28. valid = false;
  29. }
  30. });
  31. }
  32. return valid;
  33. };
  34. const checkIfFilesAreCorrectType = (files) => {
  35. let valid = true;
  36. if (files) {
  37. files.map((file) => {
  38. if (!["image/jpeg", "image/png"].includes(file.type)) {
  39. valid = false;
  40. }
  41. });
  42. }
  43. return valid;
  44. };
  45. const evaluasiSchema = Yup.object().shape({
  46. dokumen: Yup.array().min(1, "Minimal terdapat 1 dokumen").required("Harus ada").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  47. });
  48. export class ModalPermohonan extends Component {
  49. constructor(props) {
  50. super(props);
  51. this.state = {
  52. modal1: false,
  53. files: [],
  54. };
  55. }
  56. onDrop = (files) => {
  57. this.setState({
  58. files: files.map((file) =>
  59. Object.assign(file, {
  60. preview: URL.createObjectURL(file),
  61. })
  62. ),
  63. stat: "Added " + files.length + " file(s)",
  64. });
  65. };
  66. uploadFiles = (e) => {
  67. e.preventDefault();
  68. e.stopPropagation();
  69. this.setState({
  70. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  71. });
  72. };
  73. clearFiles = (e) => {
  74. e.preventDefault();
  75. e.stopPropagation();
  76. this.setState({
  77. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  78. });
  79. this.setState({
  80. files: [],
  81. });
  82. };
  83. toggleModal1 = () => {
  84. this.props.toggleModal(false);
  85. this.setState({
  86. modal1: !this.state.modal1,
  87. });
  88. };
  89. onSubmit = async (data) => {
  90. const { query, token } = this.props;
  91. const { id } = query;
  92. const formdata = new FormData();
  93. if (data.dokumen.length > 0) {
  94. data.dokumen.forEach((e) => {
  95. formdata.append("dokumen", e);
  96. });
  97. }
  98. this.setState({
  99. modal1: !this.state.modal1,
  100. });
  101. const toastid = toast.loading("Please wait...");
  102. const added = await addBanding(token, id, formdata);
  103. await updatePT(token, query.id, { is_pengajuan_banding: true })
  104. Router.push({
  105. pathname: "/pt/jawaban-banding/detail", query: { id: id }
  106. });
  107. if (!added) {
  108. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  109. } else {
  110. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  111. Router.push({
  112. pathname: "/pt/jawaban-banding/detail", query: { id: id }
  113. });
  114. }
  115. };
  116. render() {
  117. const { files } = this.state;
  118. const removeFile = file => () => {
  119. const newFiles = [...files]
  120. newFiles.splice(newFiles.indexOf(file), 1)
  121. this.setState({
  122. files: newFiles,
  123. });
  124. }
  125. const thumbs = files.map((file, index) => (
  126. <p>
  127. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  128. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  129. </p>
  130. ));
  131. return (
  132. <>
  133. <Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
  134. <ModalHeader toggle={this.toggleModal1}>Upload Dokumen Banding</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 ?
  163. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  164. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  165. </div> :
  166. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  167. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  168. </div>
  169. }
  170. </div>
  171. </div>
  172. </div>
  173. <div className="d-flex align-items-center">
  174. <small className="ml-auto">
  175. <button
  176. type="button"
  177. className="btn btn-link"
  178. onClick={(e) => {
  179. this.clearFiles(e);
  180. form.setFieldValue(field.name, []);
  181. }}
  182. >
  183. Reset dokumen
  184. </button>
  185. </small>
  186. </div>
  187. </div>
  188. );
  189. }}
  190. </DropzoneWrapper>
  191. )}
  192. </Field>
  193. <div>
  194. {thumbs}
  195. </div>
  196. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  197. <p className="mrgn-top-5 font-color-black">
  198. Ukuran setiap dokumen maksimal 15mb
  199. </p>
  200. </div>
  201. </FormGroup>
  202. </ModalBody>
  203. <ModalFooter>
  204. <Button color className="btn-login" type="submit">
  205. <span className="font-color-white">Kirim</span>
  206. </Button>
  207. </ModalFooter>
  208. </Form>
  209. </Formik>
  210. </Modal>
  211. </>
  212. );
  213. }
  214. }
  215. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  216. export default connect(mapStateToProps)(ModalPermohonan);