detail.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. let Dropzone = null;
  14. class DropzoneWrapper extends Component {
  15. state = {
  16. isClient: false,
  17. };
  18. componentDidMount = () => {
  19. Dropzone = require("react-dropzone").default;
  20. this.setState({ isClient: true });
  21. };
  22. render() {
  23. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  24. }
  25. }
  26. class DetailPencabutanSanksi extends Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. files: [],
  31. };
  32. }
  33. static async getInitialProps({ query }) {
  34. return { query };
  35. }
  36. componentDidMount = async () => {
  37. const { user } = this.props;
  38. const { noSanksi } = this.props.query;
  39. const sanksi = await getSanksi({ noSanksi, ptId: user.peran[0].organisasi.id });
  40. this.setState({ sanksi });
  41. };
  42. onDrop = (files) => {
  43. this.setState({
  44. files: files.map((file) =>
  45. Object.assign(file, {
  46. preview: URL.createObjectURL(file),
  47. })
  48. ),
  49. stat: "Added " + files.length + " file(s)",
  50. });
  51. };
  52. uploadFiles = (e) => {
  53. e.preventDefault();
  54. e.stopPropagation();
  55. this.setState({
  56. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  57. });
  58. };
  59. clearFiles = (e) => {
  60. e.preventDefault();
  61. e.stopPropagation();
  62. this.setState({
  63. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  64. });
  65. this.setState({
  66. files: [],
  67. });
  68. };
  69. handleKirim = async (e) => {
  70. e.preventDefault();
  71. const { noSanksi } = this.props.query;
  72. const { user } = this.props;
  73. const org_id = user.peran[0].organisasi.id;
  74. const formdata = new FormData();
  75. if (this.state.files.length > 0) {
  76. this.state.files.forEach((e) => {
  77. formdata.append("files", e);
  78. });
  79. const added = await addCabutSanksi({ noSanksi, ptId: org_id }, formdata);
  80. if (added) {
  81. Router.push({
  82. pathname: "/app/pt/pencabutan-sanksi",
  83. });
  84. }
  85. }
  86. };
  87. render() {
  88. const { files, sanksi } = this.state;
  89. const thumbs = files.map((file, index) => (
  90. <Col md={3} key={index}>
  91. <img className="img-fluid mb-2" src={file.preview} alt="Item" />
  92. </Col>
  93. ));
  94. return (
  95. <ContentWrapper unwrap>
  96. <Header data={this.props.pt[0]} />
  97. <div className="p-3">
  98. <div className="content-heading">
  99. <div>Permohonan Pencabutan Sanksi</div>
  100. <div className="ml-auto">
  101. <Link href="/app/pt/pencabutan-sanksi">
  102. <button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
  103. </Link>
  104. </div>
  105. </div>
  106. <Row>
  107. <Col xl="9">
  108. <Card className="card-default">
  109. <CardBody>
  110. <Row>
  111. <Col lg={12}>
  112. {sanksi?.data && <DetailSanksi data={sanksi.data[0]} />}
  113. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  114. <form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
  115. <FormGroup>
  116. <label className="row-form-label">Upload Dokumen:</label>
  117. <div className="row-md-10">
  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">
  124. {this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}
  125. </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. </div>
  138. </FormGroup>
  139. <FormGroup>
  140. {sanksi?.data && (
  141. <div className="row-xl-10">
  142. <Button color="primary" onClick={this.handleKirim} disabled={sanksi.data[0].sanksi.cabut_sanksi || false} type="submit">
  143. Kirim
  144. </Button>
  145. </div>
  146. )}
  147. </FormGroup>
  148. </form>
  149. </Col>
  150. </Row>
  151. </CardBody>
  152. </Card>
  153. </Col>
  154. <Col xl="3">{this.props.pt && <DetailPT data={this.props.pt[0]} />}</Col>
  155. </Row>
  156. <Row>
  157. <Col>{sanksi?.data && <Riwayat data={sanksi.data[0]} />}</Col>
  158. </Row>
  159. </div>
  160. </ContentWrapper>
  161. );
  162. }
  163. }
  164. const mapStateToProps = (state) => ({ user: state.user, pt: state.pt });
  165. export default connect(mapStateToProps)(DetailPencabutanSanksi);