detail.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import Link from "next/link";
  4. import { getOneSanksi } 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/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. sanksi: {},
  34. pt: null,
  35. };
  36. }
  37. static async getInitialProps({ query }) {
  38. return { query };
  39. }
  40. componentDidMount = async () => {
  41. const { token, query } = this.props;
  42. const sanksi = await getOneSanksi(token, query.id);
  43. this.setState({ sanksi, pt: sanksi.data.laporan.pt });
  44. };
  45. onDrop = (files) => {
  46. this.setState({
  47. files: files.map((file) =>
  48. Object.assign(file, {
  49. preview: URL.createObjectURL(file),
  50. })
  51. ),
  52. stat: "Added " + files.length + " file(s)",
  53. });
  54. };
  55. uploadFiles = (e) => {
  56. e.preventDefault();
  57. e.stopPropagation();
  58. this.setState({
  59. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  60. });
  61. };
  62. clearFiles = (e) => {
  63. e.preventDefault();
  64. e.stopPropagation();
  65. this.setState({
  66. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  67. });
  68. this.setState({
  69. files: [],
  70. });
  71. };
  72. handleKirim = async (e) => {
  73. e.preventDefault();
  74. const { noSanksi } = this.props.query;
  75. const { user } = this.props;
  76. const org_id = user.peran[0].organisasi.id;
  77. const formdata = new FormData();
  78. if (this.state.files.length > 0) {
  79. this.state.files.forEach((e) => {
  80. formdata.append("files", e);
  81. });
  82. const id = toast.loading("Please wait...");
  83. const added = await addCabutSanksi({ noSanksi, ptId: org_id }, formdata);
  84. if (added) {
  85. toast.update(id, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  86. Router.push({
  87. pathname: "/app/pt/pencabutan-sanksi",
  88. });
  89. }
  90. }
  91. };
  92. render() {
  93. const { files, sanksi, pt } = this.state;
  94. const thumbs = files.map((file, index) => (
  95. <Col md={3} key={index}>
  96. <img className="img-fluid mb-2" src={file.preview} alt="Item" />
  97. </Col>
  98. ));
  99. return (
  100. <ContentWrapper unwrap>
  101. {pt && <Header data={pt} />}
  102. <div className="p-3">
  103. <div className="content-heading">
  104. <div>Permohonan Pencabutan Sanksi</div>
  105. <div className="ml-auto">
  106. <Link href="/pt/pencabutan-sanksi">
  107. <button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
  108. </Link>
  109. </div>
  110. </div>
  111. <Row>
  112. {sanksi.data ? (
  113. <Col xl="9">
  114. <Card className="card-default">
  115. <CardBody>
  116. <Row>
  117. <Col lg={12}>
  118. <DetailSanksi data={sanksi.data} />
  119. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  120. <form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
  121. <FormGroup>
  122. <label className="row-form-label">Upload Dokumen:</label>
  123. <div className="row-md-10">
  124. <DropzoneWrapper className="" onDrop={this.onDrop}>
  125. {({ getRootProps, getInputProps, isDragActive }) => {
  126. return (
  127. <div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
  128. <input {...getInputProps()} />
  129. <div className="dropzone-previews flex">
  130. {this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}
  131. </div>
  132. <div className="d-flex align-items-center">
  133. <small className="ml-auto">
  134. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  135. Clear files
  136. </button>
  137. </small>
  138. </div>
  139. </div>
  140. );
  141. }}
  142. </DropzoneWrapper>
  143. </div>
  144. </FormGroup>
  145. <FormGroup>
  146. <div className="row-xl-10">
  147. <Button color="primary" onClick={this.handleKirim} disabled={sanksi.data?.pengajuan?.cabut_sanksi ? sanksi.data.pengajuan.cabut_sanksi : false} type="submit">
  148. Kirim
  149. </Button>
  150. </div>
  151. </FormGroup>
  152. </form>
  153. </Col>
  154. </Row>
  155. </CardBody>
  156. </Card>
  157. </Col>
  158. ) : (
  159. <Loader />
  160. )}
  161. <Col xl="3">{pt && <DetailPT data={pt} />}</Col>
  162. </Row>
  163. {sanksi?.data && (
  164. <Row>
  165. <Col>
  166. <Riwayat data={sanksi.data?.pengajuan?.cabut_sanksi ? sanksi.data.pengajuan.cabut_sanksi : []} />
  167. </Col>
  168. </Row>
  169. )}
  170. </div>
  171. </ContentWrapper>
  172. );
  173. }
  174. }
  175. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  176. export default connect(mapStateToProps)(DetailPencabutanSanksi);