ModalPermohonan.js 4.5 KB

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