detail.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import { getSanksi } from "@/actions/sanksi";
  4. import { addDocPerbaikan } from "@/actions/docPerbaikan";
  5. import Link from "next/link";
  6. import Header from "@/components/Main/Header";
  7. import DetailPT from "@/components/Main/DetailPT";
  8. import DetailSanksi from "@/components/Main/DetailSanksi";
  9. import Riwayat from "@/components/PT/DocPerbaikan/Riwayat";
  10. import ContentWrapper from "@/components/Layout/ContentWrapper";
  11. import { Row, Col, Card, CardBody, FormGroup, Button, Input } from "reactstrap";
  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 DetailPerbaikanDoc extends Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. files: [],
  31. sanksi: {},
  32. keterangan: "",
  33. };
  34. }
  35. static getInitialProps = ({ query }) => ({ query });
  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 { keterangan } = this.state;
  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. formdata.append("description", keterangan);
  77. if (this.state.files.length > 0) {
  78. this.state.files.forEach((e) => {
  79. formdata.append("files", e);
  80. });
  81. const added = await addDocPerbaikan({ noSanksi, ptId: org_id }, formdata);
  82. if (added) {
  83. Router.push({
  84. pathname: "/app/pt/dokumen-perbaikan",
  85. });
  86. }
  87. }
  88. };
  89. render() {
  90. const { files, sanksi } = this.state;
  91. const thumbs = files.map((file, index) => (
  92. <Col md={3} key={index}>
  93. <img className="img-fluid mb-2" src={file.preview} alt="Item" />
  94. </Col>
  95. ));
  96. return (
  97. <ContentWrapper unwrap>
  98. <Header data={this.props.pt[0]} />
  99. <div className="p-3">
  100. <div className="content-heading">
  101. <div>Dokumen Perbaikan</div>
  102. <div className="ml-auto">
  103. <Link href="/app/pt/dokumen-perbaikan">
  104. <button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
  105. </Link>
  106. </div>
  107. </div>
  108. <Row>
  109. <Col xl="9">
  110. <Card className="card-default">
  111. <CardBody>
  112. <Row>
  113. <Col lg={12}>
  114. {sanksi?.data && <DetailSanksi data={sanksi.data[0]} />}
  115. <p className="lead bb">Dokumen Perbaikan</p>
  116. <form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
  117. <FormGroup>
  118. <label className="row-form-label">Keterangan:</label>
  119. <div className="row-md-10">
  120. <Input type="textarea" value={this.state.keterangan} onChange={(e) => this.setState({ keterangan: e.target.value })} required />
  121. {/* <span className="form-text">Deskripsi pelaporan minimum karakter 50 maksimum 200 karakter</span> */}
  122. </div>
  123. </FormGroup>
  124. <FormGroup>
  125. <label className="row-form-label">Upload Dokumen:</label>
  126. <div className="row-md-10">
  127. <DropzoneWrapper className="" onDrop={this.onDrop}>
  128. {({ getRootProps, getInputProps, isDragActive }) => {
  129. return (
  130. <div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
  131. <input {...getInputProps()} />
  132. <div className="dropzone-previews flex">
  133. {this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}
  134. </div>
  135. <div className="d-flex align-items-center">
  136. <small className="ml-auto">
  137. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  138. Clear files
  139. </button>
  140. </small>
  141. </div>
  142. </div>
  143. );
  144. }}
  145. </DropzoneWrapper>
  146. </div>
  147. </FormGroup>
  148. <FormGroup>
  149. <div className="row-xl-10">
  150. <Button color="primary" onClick={this.handleKirim} type="submit">
  151. Kirim
  152. </Button>
  153. </div>
  154. </FormGroup>
  155. </form>
  156. </Col>
  157. </Row>
  158. </CardBody>
  159. </Card>
  160. </Col>
  161. <Col xl="3">{this.props.pt && <DetailPT data={this.props.pt[0]} />}</Col>
  162. </Row>
  163. <Row>
  164. <Col>
  165. <Riwayat data={sanksi?.data && sanksi.data[0].sanksi.doc_perbaikan} />
  166. </Col>
  167. </Row>
  168. </div>
  169. </ContentWrapper>
  170. );
  171. }
  172. }
  173. const mapStateToProps = (state) => ({ user: state.user, pt: state.pt });
  174. export default connect(mapStateToProps)(DetailPerbaikanDoc);