ModalPermohonan.js 5.1 KB

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