detail.js 8.0 KB

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