detail.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/PencabutanSanksi/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. };
  65. }
  66. static async getInitialProps({ query }) {
  67. return { query };
  68. }
  69. componentDidMount = async () => {
  70. const { token, query } = this.props;
  71. const sanksi = await getOneSanksi(token, query.id);
  72. this.setState({ sanksi, pt: sanksi.data.laporan.pt });
  73. };
  74. onDrop = (files) => {
  75. this.setState({
  76. files: files.map((file) =>
  77. Object.assign(file, {
  78. preview: URL.createObjectURL(file),
  79. })
  80. ),
  81. stat: "Added " + files.length + " file(s)",
  82. });
  83. };
  84. uploadFiles = (e) => {
  85. e.preventDefault();
  86. e.stopPropagation();
  87. this.setState({
  88. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  89. });
  90. };
  91. clearFiles = (e) => {
  92. e.preventDefault();
  93. e.stopPropagation();
  94. this.setState({
  95. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  96. });
  97. this.setState({
  98. files: [],
  99. });
  100. };
  101. handleKirim = async (data) => {
  102. const getToken = await getCsrf();
  103. const _csrf = getToken.token;
  104. const { user, query, token } = this.props;
  105. const formdata = new FormData();
  106. data.dokumen.forEach((e) => {
  107. formdata.append("dokumen", e);
  108. });
  109. const id = toast.loading("Please wait...");
  110. const added = await addCabutSanksi(token, query.id, formdata, _csrf);
  111. if (!added) {
  112. toast.update(id, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  113. } else {
  114. toast.update(id, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  115. Router.push({
  116. pathname: "/pt/jawaban-pencabutan-sanksi",
  117. });
  118. }
  119. };
  120. render() {
  121. const { files, sanksi, pt } = this.state;
  122. const thumbs = files.map((file, index) => (
  123. <div md={3} key={index}>
  124. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  125. <span className="text-left">{index + 1}.{file.name}</span>
  126. </div>
  127. ));
  128. return (
  129. <ContentWrapper unwrap>
  130. {pt && <Header data={pt} />}
  131. <div className="p-3">
  132. <div className="content-heading">
  133. <span className="font-color-white">
  134. Permohonan Pencabutan Sanksi
  135. </span>
  136. <div className="ml-auto">
  137. <Link href="/pt/pencabutan-sanksi">
  138. <Button className="color-3e3a8e" color>
  139. <span className="font-color-white">
  140. &lt; Kembali
  141. </span>
  142. </Button>
  143. </Link>
  144. </div>
  145. </div>
  146. <Row>
  147. {sanksi.data ? (
  148. <Col xl="9">
  149. <Card className="card-default">
  150. <CardBody>
  151. <Row>
  152. <Col lg={12}>
  153. <DetailSanksi data={sanksi.data} />
  154. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  155. <Formik
  156. initialValues={{
  157. dokumen: [],
  158. }}
  159. validationSchema={docSchema}
  160. onSubmit={this.handleKirim}
  161. >
  162. {() => (
  163. <Form className="form-horizontal">
  164. <FormGroup>
  165. <label className="row-form-label">Upload Dokumen:</label>
  166. <div className="row-md-10">
  167. <Field name="dokumen">
  168. {({ field, form }) => (
  169. <DropzoneWrapper
  170. className=""
  171. onDrop={(e) => {
  172. this.onDrop(e);
  173. form.setFieldValue(field.name, e);
  174. }}
  175. >
  176. {({ getRootProps, getInputProps, isDragActive }) => {
  177. return (
  178. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  179. <input {...getInputProps()} />
  180. <div className="dropzone-previews flex">
  181. <div className="dropzone-style-1">
  182. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  183. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  184. <h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
  185. </div>
  186. }
  187. </div>
  188. </div> </div>
  189. <div className="d-flex align-items-center">
  190. <small className="ml-auto">
  191. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  192. Clear files
  193. </button>
  194. </small>
  195. </div>
  196. </div>
  197. );
  198. }}
  199. </DropzoneWrapper>
  200. )}
  201. </Field>
  202. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  203. </div>
  204. </FormGroup>
  205. <FormGroup>
  206. <div className="row-xl-10">
  207. <Button color className="color-3e3a8e" disabled={sanksi.data?.pengajuan?.cabut_sanksi || false} type="submit">
  208. <span className="font-color-white">
  209. Kirim
  210. </span>
  211. </Button>
  212. </div>
  213. </FormGroup>
  214. </Form>
  215. )}
  216. </Formik>
  217. </Col>
  218. </Row>
  219. </CardBody>
  220. </Card>
  221. </Col>
  222. ) : (
  223. <Loader />
  224. )}
  225. <Col xl="3">{pt && <DetailPT data={pt} />}</Col>
  226. </Row>
  227. {sanksi.data && (
  228. <Row>
  229. <Col>
  230. <Riwayat data={sanksi.data?.pengajuan?.cabut_sanksi || null} />
  231. </Col>
  232. </Row>
  233. )}
  234. </div>
  235. </ContentWrapper>
  236. );
  237. }
  238. }
  239. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  240. export default connect(mapStateToProps)(DetailPencabutanSanksi);