detail.js 6.5 KB

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