detail.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React, { Component } from "react";
  2. import { Row, Col, Card, CardBody, FormGroup, Input, Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
  3. import Router from "next/router";
  4. import classnames from "classnames";
  5. import ContentWrapper from "@/components/Layout/ContentWrapper";
  6. import DetailSanksi from "@/components/RekomendasiDelegasi/DetailSanksi";
  7. import TableRiwayat from "@/components/RekomendasiDelegasi/TableRiwayat";
  8. import Header from "@/components/Main/Header";
  9. import Link from "next/link";
  10. import DetailPT from "@/components/Main/DetailPT";
  11. import { getPelaporan } from "@/actions/pelaporan";
  12. import { getOneSanksi } from "@/actions/sanksi";
  13. import Loader from "@/components/Common/Loader";
  14. import { connect } from "react-redux";
  15. import { Formik, Form, Field, ErrorMessage } from "formik";
  16. import * as Yup from "yup";
  17. import { createLog } from "@/actions/log";
  18. const stepNavitemStyle = {
  19. backgroundColor: "#fcfcfc",
  20. };
  21. let Dropzone = null;
  22. class DropzoneWrapper extends Component {
  23. state = {
  24. isClient: false,
  25. };
  26. componentDidMount = () => {
  27. Dropzone = require("react-dropzone").default;
  28. this.setState({ isClient: true });
  29. };
  30. render() {
  31. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  32. }
  33. }
  34. const selectInstanceId = 1;
  35. const checkIfFilesAreTooBig = (files) => {
  36. let valid = true;
  37. if (files) {
  38. files.map((file) => {
  39. if (file.size > 15 * 1024 * 1024) {
  40. valid = false;
  41. }
  42. });
  43. }
  44. return valid;
  45. };
  46. const checkIfFilesAreCorrectType = (files) => {
  47. let valid = true;
  48. if (files) {
  49. files.map((file) => {
  50. if (!["image/jpeg", "image/png"].includes(file.type)) {
  51. valid = false;
  52. }
  53. });
  54. }
  55. return valid;
  56. };
  57. const rekomendasiSchema = Yup.object().shape({
  58. dokumen: Yup.array().notRequired().test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  59. });
  60. class Detail extends Component {
  61. constructor(props) {
  62. super(props);
  63. this.state = {
  64. sanksi: {},
  65. files: [],
  66. pt: {},
  67. };
  68. }
  69. static getInitialProps = async ({ query }) => {
  70. return { query };
  71. };
  72. componentDidMount = async () => {
  73. const { query, token } = this.props;
  74. const idSanksi = query.id;
  75. const sanksi = await getOneSanksi(token, idSanksi, { all: true });
  76. const pt = sanksi.data.laporan.pt;
  77. this.setState({ sanksi, pt });
  78. console.log(this.state.sanksi)
  79. };
  80. onDrop = (files) => {
  81. this.setState({
  82. files: files.map((file) =>
  83. Object.assign(file, {
  84. preview: URL.createObjectURL(file),
  85. })
  86. ),
  87. stat: "Added " + files.length + " file(s)",
  88. });
  89. };
  90. uploadFiles = (e) => {
  91. e.preventDefault();
  92. e.stopPropagation();
  93. this.setState({
  94. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  95. });
  96. };
  97. clearFiles = (e) => {
  98. e.preventDefault();
  99. e.stopPropagation();
  100. this.setState({
  101. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  102. });
  103. this.setState({
  104. files: [],
  105. });
  106. };
  107. handelSimpan = async () => {
  108. const { data } = this.state;
  109. const { query, token } = this.props;
  110. const { id } = query;
  111. const formdata = new FormData();
  112. data.dokumen.forEach((e) => {
  113. formdata.append("dokumen", e);
  114. });
  115. const toastid = toast.loading("Please wait...");
  116. const added = await addJawabanKeberatan(token, id, formdata);
  117. if (!added) {
  118. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  119. } else {
  120. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  121. Router.push({
  122. pathname: "/app/rekomendasi-delegasi",
  123. });
  124. }
  125. };
  126. render() {
  127. const { files, sanksi, pt } = this.state;
  128. const thumbs = files.map((file, index) => (
  129. <div md={3} key={index}>
  130. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  131. <span className="text-left">{index + 1}.{file.name}</span>
  132. </div>
  133. ));
  134. return (
  135. <ContentWrapper unwrap>
  136. {/* <Header /> */}
  137. <div className="p-3">
  138. <div className="content-heading">
  139. <span className="font-color-white">
  140. Rekomendasi Delegasi
  141. </span>
  142. <div className="ml-auto">
  143. <Link href="/app/sanksi">
  144. <Button className="color-3e3a8e" color>
  145. <span className="font-color-white">
  146. &lt; Kembali
  147. </span>
  148. </Button>
  149. </Link>
  150. </div>
  151. </div>
  152. {sanksi.data && (
  153. <Row>
  154. <Col xl={9}>
  155. <DetailSanksi data={sanksi.data} />
  156. <Card>
  157. <CardBody>
  158. <p className="lead bb">Dokumen Rekomendasi Delegasi</p>
  159. <Formik
  160. initialValues={{
  161. dokumen: [],
  162. }}
  163. validationSchema={rekomendasiSchema}
  164. onSubmit={async (data) => {
  165. this.setState({ data });
  166. await this.handelSimpan();
  167. }}
  168. >
  169. {() => (
  170. <Form className="form-horizontal">
  171. <FormGroup>
  172. <div className="row-md-10">
  173. <Field name="dokumen">
  174. {({ field, form }) => (
  175. <DropzoneWrapper
  176. className=""
  177. onDrop={(e) => {
  178. this.onDrop(e);
  179. form.setFieldValue(field.name, e);
  180. }}
  181. >
  182. {({ getRootProps, getInputProps, isDragActive }) => {
  183. return (
  184. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  185. <input {...getInputProps()} />
  186. <div className="dropzone-previews flex">
  187. <div className="dropzone-style-1">
  188. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  189. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  190. <h5 className="text-center dz-default dz-message">upload dokumen rekomendasi delegasi</h5>
  191. </div>
  192. }
  193. </div>
  194. </div>
  195. </div>
  196. <div className="d-flex align-items-center">
  197. <small className="ml-auto">
  198. <button
  199. type="button"
  200. className="btn btn-link"
  201. onClick={(e) => {
  202. this.clearFiles(e);
  203. form.setFieldValue(field.name, []);
  204. }}
  205. >
  206. Reset dokumen
  207. </button>
  208. </small>
  209. </div>
  210. </div>
  211. );
  212. }}
  213. </DropzoneWrapper>
  214. )}
  215. </Field>
  216. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  217. <p className="mrgn-top-5">Ukuran setiap dokumen maksimal 15mb</p>
  218. </div>
  219. </FormGroup>
  220. {/* <FormGroup>
  221. <div className="row-xl-10"> */}
  222. <FormGroup row>
  223. <div className="col-xl-10">
  224. <Button color className="color-3e3a8e btn-login" type="submit">
  225. <span className="font-color-white">Kirim</span>
  226. </Button>
  227. </div>
  228. </FormGroup>
  229. {/* <Button color="primary" onClick={sanksi.data.jawaban?.keberatan ? this.toggleModal : this.handelSimpan}>
  230. Simpan
  231. </Button> */}
  232. {/* </div>
  233. </FormGroup> */}
  234. </Form>
  235. )}
  236. </Formik>
  237. </CardBody>
  238. </Card>
  239. </Col>
  240. <Col xl="3">{pt ? <DetailPT data={pt} /> : <Loader />}</Col>
  241. </Row>
  242. )}
  243. </div>
  244. </ContentWrapper>
  245. );
  246. }
  247. }
  248. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  249. export default connect(mapStateToProps)(Detail);