ModalPermohonan.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 { addDocPerbaikan } from "@/actions/docPerbaikan";
  6. let Dropzone = null;
  7. class DropzoneWrapper extends Component {
  8. state = {
  9. isClient: false,
  10. };
  11. componentDidMount = () => {
  12. Dropzone = require("react-dropzone").default;
  13. this.setState({ isClient: true });
  14. };
  15. render() {
  16. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  17. }
  18. }
  19. export class ModalPermohonan extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. modal1: false,
  24. files: [],
  25. };
  26. }
  27. onDrop = (files) => {
  28. this.setState({
  29. files: files.map((file) =>
  30. Object.assign(file, {
  31. preview: URL.createObjectURL(file),
  32. })
  33. ),
  34. stat: "Added " + files.length + " file(s)",
  35. });
  36. };
  37. uploadFiles = (e) => {
  38. e.preventDefault();
  39. e.stopPropagation();
  40. this.setState({
  41. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  42. });
  43. };
  44. clearFiles = (e) => {
  45. e.preventDefault();
  46. e.stopPropagation();
  47. this.setState({
  48. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  49. });
  50. this.setState({
  51. files: [],
  52. });
  53. };
  54. toggleModal1 = () => {
  55. this.props.toggleModal(false);
  56. this.setState({
  57. modal1: !this.state.modal1,
  58. });
  59. };
  60. onSubmit = async (e) => {
  61. e.preventDefault();
  62. const { noSanksi } = this.props.query;
  63. const formdata = new FormData();
  64. if (this.state.files.length > 0) {
  65. this.state.files.forEach((e) => {
  66. formdata.append("files", e);
  67. });
  68. }
  69. const added = await addKeberatan({ noSanksi, ptId: "0BCE4DB7-B207-445D-8D03-0C54B7688252" }, formdata);
  70. // formdata.append("keberatan", added.add.sanksi.keberatan._id);
  71. formdata.append("on", added.add.sanksi.keberatan._id);
  72. formdata.append("on_model", "Keberatan");
  73. await addDocPerbaikan({ noSanksi, ptId: "0BCE4DB7-B207-445D-8D03-0C54B7688252" }, formdata);
  74. // console.log(added);
  75. if (added) {
  76. Router.push({
  77. pathname: "/app/pt/keberatan",
  78. });
  79. }
  80. };
  81. handleKirim = (e) => {
  82. this.setState({
  83. modal1: !this.state.modal1,
  84. });
  85. this.onSubmit(e);
  86. };
  87. render() {
  88. const { files } = this.state;
  89. const thumbs = files.map((file, index) => (
  90. <Col md={3} key={index}>
  91. <img className="img-fluid mb-2" src={file.preview} alt="Item" />
  92. </Col>
  93. ));
  94. return (
  95. <>
  96. <Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
  97. <ModalBody>Apakah anda akan mengajukan permohonan keberatan atas pengenaan sanksi?</ModalBody>
  98. <ModalFooter>
  99. <Button color="primary" onClick={this.toggleModal1}>
  100. Ya
  101. </Button>{" "}
  102. <Button color="secondary" onClick={this.props.toggleModal}>
  103. Tidak
  104. </Button>
  105. </ModalFooter>
  106. </Modal>
  107. <Modal isOpen={this.state.modal1} toggle={this.toggleModal1}>
  108. <ModalHeader toggle={this.toggleModal1}>Unggah Dokumen Permohonan Keberatan</ModalHeader>
  109. <ModalBody>
  110. <form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
  111. <FormGroup>
  112. <label>Dalam hal mengajukan permohonan keberatan maka wajib mengunggah dokumen pendukungnya</label>
  113. <div>
  114. <DropzoneWrapper className="" onDrop={this.onDrop}>
  115. {({ getRootProps, getInputProps, isDragActive }) => {
  116. return (
  117. <div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
  118. <input {...getInputProps()} />
  119. <div className="dropzone-previews flex">{this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}</div>
  120. <div className="d-flex align-items-center">
  121. <small className="ml-auto">
  122. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  123. Clear files
  124. </button>
  125. </small>
  126. </div>
  127. </div>
  128. );
  129. }}
  130. </DropzoneWrapper>
  131. <span className="form-text">Multiple files upload</span>
  132. </div>
  133. </FormGroup>
  134. </form>
  135. </ModalBody>
  136. <ModalFooter>
  137. <Button color="primary" onClick={this.handleKirim}>
  138. Kirim
  139. </Button>
  140. </ModalFooter>
  141. </Modal>
  142. </>
  143. );
  144. }
  145. }
  146. export default ModalPermohonan;