detail.js 5.4 KB

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