detail.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import Link from "next/link";
  4. import { getSanksi } from "@/actions/sanksi";
  5. import Header from "@/components/Main/Header";
  6. import DetailPT from "@/components/Main/DetailPT";
  7. import DetailSanksi from "@/components/Main/DetailSanksi";
  8. import Riwayat from "@/components/PT/CabutSanksi/Riwayat";
  9. import ContentWrapper from "@/components/Layout/ContentWrapper";
  10. import { Row, Col, Card, CardBody, FormGroup, Button } from "reactstrap";
  11. import { addCabutSanksi } from "@/actions/cabutSanksi";
  12. import { connect } from "react-redux";
  13. import Loader from "@/components/Common/Loader";
  14. import { toast } from "react-toastify";
  15. let Dropzone = null;
  16. class DropzoneWrapper extends Component {
  17. state = {
  18. isClient: false,
  19. };
  20. componentDidMount = () => {
  21. Dropzone = require("react-dropzone").default;
  22. this.setState({ isClient: true });
  23. };
  24. render() {
  25. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  26. }
  27. }
  28. class DetailPencabutanSanksi extends Component {
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. files: [],
  33. selectedFile: {},
  34. };
  35. }
  36. static async getInitialProps({ query }) {
  37. return { query };
  38. }
  39. componentDidMount = async () => {
  40. const { user } = this.props;
  41. const { noSanksi } = this.props.query;
  42. const sanksi = await getSanksi({ noSanksi, ptId: user.peran[0].organisasi.id });
  43. this.setState({ sanksi });
  44. };
  45. onDrop = (selectedFile) => {
  46. this.setState({
  47. selectedFile: selectedFile.map((file) =>
  48. Object.assign(file, {
  49. preview: URL.createObjectURL(file),
  50. })
  51. ),
  52. stat: "Added " + selectedFile.length + " file(s)",
  53. });
  54. const selectFile = this.state.selectedFile
  55. this.setState(prevState => ({
  56. files: [...prevState.files, ...selectFile]
  57. }))
  58. };
  59. uploadFiles = (e) => {
  60. e.preventDefault();
  61. e.stopPropagation();
  62. this.setState({
  63. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  64. });
  65. };
  66. clearFiles = (e) => {
  67. e.preventDefault();
  68. e.stopPropagation();
  69. this.setState({
  70. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  71. });
  72. this.setState({
  73. files: [],
  74. });
  75. };
  76. handleKirim = async (e) => {
  77. e.preventDefault();
  78. const { noSanksi } = this.props.query;
  79. const { user } = this.props;
  80. const org_id = user.peran[0].organisasi.id;
  81. const formdata = new FormData();
  82. if (this.state.files.length > 0) {
  83. this.state.files.forEach((e) => {
  84. formdata.append("files", e);
  85. });
  86. const id = toast.loading("Please wait...");
  87. const added = await addCabutSanksi({ noSanksi, ptId: org_id }, formdata);
  88. if (added) {
  89. toast.update(id, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  90. Router.push({
  91. pathname: "/app/pt/pencabutan-sanksi",
  92. });
  93. }
  94. }
  95. };
  96. render() {
  97. const { files, sanksi } = this.state;
  98. const thumbs = files.map((file, index) => (
  99. <div md={3} key={index}>
  100. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  101. <span className="text-center">{index + 1}.{file.name}</span>
  102. </div>
  103. ));
  104. return (
  105. <ContentWrapper unwrap>
  106. <Header data={this.props.pt[0]} />
  107. <div className="p-3">
  108. <div className="content-heading">
  109. <div>Permohonan Pencabutan Sanksi</div>
  110. <div className="ml-auto">
  111. <Link href="/app/pt/pencabutan-sanksi">
  112. <button className="btn btn-sm btn-secondary text-sm">&lt; kembali</button>
  113. </Link>
  114. </div>
  115. </div>
  116. <Row>
  117. {sanksi?.data ? (
  118. <Col xl="9">
  119. <Card className="card-default">
  120. <CardBody>
  121. <Row>
  122. <Col lg={12}>
  123. <DetailSanksi data={sanksi.data[0]} />
  124. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  125. <form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
  126. <FormGroup>
  127. <label className="row-form-label">Upload Dokumen:</label>
  128. <div className="row-md-10">
  129. <DropzoneWrapper className="" onDrop={this.onDrop}>
  130. {({ getRootProps, getInputProps, isDragActive }) => {
  131. return (
  132. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  133. <input {...getInputProps()} />
  134. <div className="dropzone-style-1">
  135. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  136. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  137. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  138. </div>
  139. }
  140. </div>
  141. </div>
  142. <div className="d-flex align-items-center">
  143. <small className="ml-auto">
  144. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  145. Clear files
  146. </button>
  147. </small>
  148. </div>
  149. </div>
  150. );
  151. }}
  152. </DropzoneWrapper>
  153. </div>
  154. </FormGroup>
  155. <FormGroup>
  156. <div className="row-xl-10">
  157. <Button color="primary" onClick={this.handleKirim} disabled={sanksi.data[0].sanksi.cabut_sanksi || false} type="submit">
  158. Kirim
  159. </Button>
  160. </div>
  161. </FormGroup>
  162. </form>
  163. </Col>
  164. </Row>
  165. </CardBody>
  166. </Card>
  167. </Col>
  168. ) : (
  169. <Loader />
  170. )}
  171. <Col xl="3">{this.props.pt && <DetailPT data={this.props.pt[0]} />}</Col>
  172. </Row>
  173. {sanksi?.data && (
  174. <Row>
  175. <Col>
  176. <Riwayat data={sanksi.data[0]} />
  177. </Col>
  178. </Row>
  179. )}
  180. </div>
  181. </ContentWrapper>
  182. );
  183. }
  184. }
  185. const mapStateToProps = (state) => ({ user: state.user, pt: state.pt });
  186. export default connect(mapStateToProps)(DetailPencabutanSanksi);