ModalPermohonan.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 { 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. 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("Harus ada").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.props.toggleModal(false);
  84. this.setState({
  85. modal1: !this.state.modal1,
  86. });
  87. };
  88. onSubmit = async (data) => {
  89. const { query, token } = this.props;
  90. const { id } = query;
  91. const formdata = new FormData();
  92. if (data.dokumen.length > 0) {
  93. data.dokumen.forEach((e) => {
  94. formdata.append("dokumen", e);
  95. });
  96. }
  97. this.setState({
  98. modal1: !this.state.modal1,
  99. });
  100. const toastid = toast.loading("Please wait...");
  101. const added = await addBanding(token, id, formdata);
  102. if (!added) {
  103. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  104. } else {
  105. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  106. Router.push({
  107. pathname: "/pt/jawaban-banding",
  108. });
  109. }
  110. };
  111. render() {
  112. const { files } = this.state;
  113. const thumbs = files.map((file, index) => (
  114. <div md={3} key={index}>
  115. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  116. <span className="text-center">{file.name}</span>
  117. </div>
  118. ));
  119. return (
  120. <>
  121. <Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
  122. <ModalBody>Apakah anda akan mengajukan banding?</ModalBody>
  123. <ModalFooter>
  124. <Button color="primary" onClick={this.toggleModal1}>
  125. Ya
  126. </Button>{" "}
  127. <Button color="secondary" onClick={this.props.toggleModal}>
  128. Tidak
  129. </Button>
  130. </ModalFooter>
  131. </Modal>
  132. <Modal isOpen={this.state.modal1} toggle={this.toggleModal1}>
  133. <ModalHeader toggle={this.toggleModal1}>Upload Dokumen Banding</ModalHeader>
  134. <Formik
  135. initialValues={{
  136. dokumen: [],
  137. }}
  138. validationSchema={evaluasiSchema}
  139. onSubmit={this.onSubmit}
  140. >
  141. <Form className="form-horizontal">
  142. <ModalBody>
  143. <FormGroup>
  144. <label>Dalam hal mengajukan permohonan banding maka wajib mengunggah surat permohonan banding & dokumen pendukungnya</label>
  145. <div>
  146. <Field name="dokumen">
  147. {({ field, form, meta }) => (
  148. <DropzoneWrapper
  149. className=""
  150. onDrop={(e) => {
  151. this.onDrop(e);
  152. form.setFieldValue(field.name, e);
  153. }}
  154. >
  155. {({ getRootProps, getInputProps, isDragActive }) => {
  156. return (
  157. <div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
  158. <input {...getInputProps()} />
  159. <div className="dropzone-previews flex">
  160. {this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Klik untuk upload dokumen</div>}
  161. </div>
  162. <div className="d-flex align-items-center">
  163. <small className="ml-auto">
  164. <button
  165. type="button"
  166. className="btn btn-link"
  167. onClick={(e) => {
  168. this.clearFiles(e);
  169. form.setFieldValue(field.name, []);
  170. }}
  171. >
  172. Reset dokumen
  173. </button>
  174. </small>
  175. </div>
  176. </div>
  177. );
  178. }}
  179. </DropzoneWrapper>
  180. )}
  181. </Field>
  182. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  183. <p className="mrgn-top-5">
  184. Ukuran setiap dokumen maksimal 15mb
  185. </p>
  186. </div>
  187. </FormGroup>
  188. </ModalBody>
  189. <ModalFooter>
  190. <Button color="primary" type="submit">
  191. Kirim
  192. </Button>
  193. </ModalFooter>
  194. </Form>
  195. </Formik>
  196. </Modal>
  197. </>
  198. );
  199. }
  200. }
  201. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  202. export default connect(mapStateToProps)(ModalPermohonan);