ModalPermohonan.js 4.1 KB

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