detail.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. <div md={3} key={index}>
  121. {/* <img className="img-fluid mb-2" src={file.preview} alt="Item" /> */}
  122. <span className="text-center">{file.name}</span>
  123. </div>
  124. ));
  125. return (
  126. <ContentWrapper unwrap>
  127. {pt && <Header data={pt} />}
  128. <div className="p-3">
  129. <div className="content-heading">
  130. <div>Permohonan Pencabutan Sanksi</div>
  131. <div className="ml-auto">
  132. <Link href="/pt/pencabutan-sanksi">
  133. <button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
  134. </Link>
  135. </div>
  136. </div>
  137. <Row>
  138. {sanksi.data ? (
  139. <Col xl="9">
  140. <Card className="card-default">
  141. <CardBody>
  142. <Row>
  143. <Col lg={12}>
  144. <DetailSanksi data={sanksi.data} />
  145. <p className="lead bb">Permohonan Pencabutan Sanksi</p>
  146. <Formik
  147. initialValues={{
  148. dokumen: [],
  149. }}
  150. validationSchema={docSchema}
  151. onSubmit={this.handleKirim}
  152. >
  153. {() => (
  154. <Form className="form-horizontal">
  155. <FormGroup>
  156. <label className="row-form-label">Upload Dokumen:</label>
  157. <div className="row-md-10">
  158. <Field name="dokumen">
  159. {({ field, form }) => (
  160. <DropzoneWrapper
  161. className=""
  162. onDrop={(e) => {
  163. this.onDrop(e);
  164. form.setFieldValue(field.name, e);
  165. }}
  166. >
  167. {({ getRootProps, getInputProps, isDragActive }) => {
  168. return (
  169. <div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
  170. <input {...getInputProps()} />
  171. <div className="dropzone-previews flex">
  172. {this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Klik untuk upload dokumen</div>}
  173. </div>
  174. <div className="d-flex align-items-center">
  175. <small className="ml-auto">
  176. <button type="button" className="btn btn-link" onClick={this.clearFiles}>
  177. Clear files
  178. </button>
  179. </small>
  180. </div>
  181. </div>
  182. );
  183. }}
  184. </DropzoneWrapper>
  185. )}
  186. </Field>
  187. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  188. </div>
  189. </FormGroup>
  190. <FormGroup>
  191. <div className="row-xl-10">
  192. <Button color="primary" disabled={sanksi.data?.pengajuan?.cabut_sanksi || false} type="submit">
  193. Kirim
  194. </Button>
  195. </div>
  196. </FormGroup>
  197. </Form>
  198. )}
  199. </Formik>
  200. </Col>
  201. </Row>
  202. </CardBody>
  203. </Card>
  204. </Col>
  205. ) : (
  206. <Loader />
  207. )}
  208. <Col xl="3">{pt && <DetailPT data={pt} />}</Col>
  209. </Row>
  210. {sanksi.data && (
  211. <Row>
  212. <Col>
  213. <Riwayat data={sanksi.data?.pengajuan?.cabut_sanksi || null} />
  214. </Col>
  215. </Row>
  216. )}
  217. </div>
  218. </ContentWrapper>
  219. );
  220. }
  221. }
  222. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  223. export default connect(mapStateToProps)(DetailPencabutanSanksi);