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