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