detail.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/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. import { toast } from "react-toastify";
  15. import { Formik, Form, Field, ErrorMessage } from "formik";
  16. import * as Yup from "yup";
  17. import { getCsrf } from "../../../actions/security";
  18. import { FinalisasiPerbaikan } from "../../../actions/docPerbaikan";
  19. const checkIfFilesAreTooBig = (files) => {
  20. let valid = true;
  21. if (files) {
  22. files.map((file) => {
  23. if (file.size > 15 * 1024 * 1024) {
  24. valid = false;
  25. }
  26. });
  27. }
  28. return valid;
  29. };
  30. const checkIfFilesAreCorrectType = (files) => {
  31. let valid = true;
  32. if (files) {
  33. files.map((file) => {
  34. if (!["image/jpeg", "image/png"].includes(file.type)) {
  35. valid = false;
  36. }
  37. });
  38. }
  39. return valid;
  40. };
  41. const docSchema = Yup.object().shape({
  42. dokumen: Yup.array().max(2, "Maximal 2 dokumen").required("Required").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  43. });
  44. let Dropzone = null;
  45. class DropzoneWrapper extends Component {
  46. state = {
  47. isClient: false,
  48. };
  49. componentDidMount = () => {
  50. Dropzone = require("react-dropzone").default;
  51. this.setState({ isClient: true });
  52. };
  53. render() {
  54. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  55. }
  56. }
  57. class DetailPencabutanSanksi extends Component {
  58. constructor(props) {
  59. super(props);
  60. this.state = {
  61. files: [],
  62. sanksi: {},
  63. pt: null,
  64. error: null,
  65. selectedFile: {},
  66. };
  67. }
  68. static async getInitialProps({ query }) {
  69. return { query };
  70. }
  71. componentDidMount = async () => {
  72. const { token, query } = this.props;
  73. const sanksi = await getOneSanksi(token, query.id);
  74. this.setState({ sanksi, pt: sanksi.data.laporan.pt });
  75. };
  76. onDrop = (selectedFile) => {
  77. this.setState({
  78. selectedFile: selectedFile.map((file) =>
  79. Object.assign(file, {
  80. preview: URL.createObjectURL(file),
  81. })
  82. ),
  83. stat: "Added " + selectedFile.length + " file(s)",
  84. });
  85. const selectFile = this.state.selectedFile
  86. this.setState(prevState => ({
  87. files: [...prevState.files, ...selectFile]
  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. handleKirim = async (data) => {
  108. const getToken = await getCsrf();
  109. const _csrf = getToken.token;
  110. const { user, query, token } = this.props;
  111. const formdata = new FormData();
  112. data.dokumen.forEach((e) => {
  113. formdata.append("dokumen", e);
  114. });
  115. const id = toast.loading("Please wait...");
  116. // await FinalisasiPerbaikan(token, query.id, { is_finalisasi: "true" }, _csrf)
  117. const added = await addCabutSanksi(token, query.id, formdata, _csrf);
  118. if (!added) {
  119. toast.update(id, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  120. } else {
  121. toast.update(id, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  122. Router.push({
  123. pathname: "/pt/jawaban-pencabutan-sanksi",
  124. });
  125. }
  126. };
  127. render() {
  128. const { files, sanksi, pt } = this.state;
  129. const thumbs = files.map((file, index) => (
  130. <div md={3} key={index}>
  131. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  132. <span className="text-left">{index + 1}.{file.name}</span>
  133. </div>
  134. ));
  135. const data = sanksi.data?.riwayat_pengajuan_cabut_sanksi
  136. const lastPengajuan = data?.slice(-1)[0]
  137. return (
  138. <ContentWrapper unwrap>
  139. {pt && <Header data={pt} />}
  140. <div className="p-3">
  141. <div className="content-heading">
  142. <span className="font-color-white">
  143. Permohonan Pencabutan Sanksi
  144. </span>
  145. <div className="ml-auto">
  146. <Link href="/pt/pencabutan-sanksi">
  147. <Button className="color-3e3a8e" color>
  148. <span className="font-color-white">
  149. &lt; Kembali
  150. </span>
  151. </Button>
  152. </Link>
  153. </div>
  154. </div>
  155. <Row>
  156. {sanksi.data ? (
  157. <Col xl="9">
  158. <Card className="card-default">
  159. <CardBody>
  160. <Row>
  161. <Col lg={12}>
  162. <DetailSanksi data={sanksi.data} />
  163. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  164. <Formik
  165. initialValues={{
  166. dokumen: [],
  167. }}
  168. validationSchema={docSchema}
  169. onSubmit={this.handleKirim}
  170. >
  171. {() => (
  172. <Form className="form-horizontal">
  173. <FormGroup>
  174. <label className="row-form-label">Upload Dokumen:</label>
  175. <div className=" font-color-black block">Note : Dokumen perbaikan akan diperiksa setelah surat permohonan pencabutan sanksi diunggah</div>
  176. <div className="row-md-10">
  177. <Field name="dokumen">
  178. {({ field, form }) => (
  179. <DropzoneWrapper
  180. className=""
  181. onDrop={(e) => {
  182. this.onDrop(e);
  183. form.setFieldValue(field.name, e);
  184. }}
  185. >
  186. {({ getRootProps, getInputProps, isDragActive }) => {
  187. return (
  188. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  189. <input {...getInputProps()} />
  190. <div className="dropzone-previews flex">
  191. <div className="dropzone-style-1">
  192. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  193. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  194. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  195. </div>
  196. }
  197. </div>
  198. </div> </div>
  199. <div className="d-flex align-items-center">
  200. <small className="ml-auto">
  201. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  202. Clear files
  203. </button>
  204. </small>
  205. </div>
  206. </div>
  207. );
  208. }}
  209. </DropzoneWrapper>
  210. )}
  211. </Field>
  212. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  213. </div>
  214. </FormGroup>
  215. <FormGroup>
  216. <div className="row-xl-10">
  217. <Button color className="color-3e3a8e" disabled={ sanksi.data.is_finalisasi === false || false||lastPengajuan?.index + 1 === sanksi?.data?.index_perbaikan} type="submit">
  218. <span className="font-color-white">
  219. Kirim
  220. </span>
  221. </Button>
  222. </div>
  223. </FormGroup>
  224. </Form>
  225. )}
  226. </Formik>
  227. </Col>
  228. </Row>
  229. </CardBody>
  230. </Card>
  231. </Col>
  232. ) : (
  233. <Loader />
  234. )}
  235. <Col xl="3">{pt && <DetailPT data={pt} />}</Col>
  236. </Row>
  237. {sanksi.data && (
  238. <Row>
  239. <Col>
  240. <Riwayat data={sanksi.data?.riwayat_pengajuan_cabut_sanksi } />
  241. </Col>
  242. </Row>
  243. )}
  244. </div>
  245. </ContentWrapper>
  246. );
  247. }
  248. }
  249. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  250. export default connect(mapStateToProps)(DetailPencabutanSanksi);