detail.js 7.0 KB

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