InputRekomendasi.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import React, { Component } from "react";
  2. import { insertPemeriksaan } from "@/actions/pemeriksaan";
  3. import { Row, Col, Card, CardBody, FormGroup, Input, Button, Modal, ModalHeader, ModalBody, ModalFooter, CardHeader, CardTitle } from "reactstrap";
  4. import { toast } from "react-toastify";
  5. import { Formik, Form, Field, ErrorMessage } from "formik";
  6. import * as Yup from "yup";
  7. import { connect } from "react-redux";
  8. import { getOneSanksi, addRekomendasiDelegasi } from "@/actions/sanksi";
  9. const selectInstanceId = 1;
  10. const checkIfFilesAreTooBig = (files) => {
  11. let valid = true;
  12. if (files) {
  13. files.map((file) => {
  14. if (file.size > 15 * 1024 * 1024) {
  15. valid = false;
  16. }
  17. });
  18. }
  19. return valid;
  20. };
  21. const checkIfFilesAreCorrectType = (files) => {
  22. let valid = true;
  23. if (files) {
  24. files.map((file) => {
  25. if (!["image/jpeg", "image/png"].includes(file.type)) {
  26. valid = false;
  27. }
  28. });
  29. }
  30. return valid;
  31. };
  32. const rekomendasiSchema = Yup.object().shape({
  33. dokumen: Yup.array().min(1).required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  34. });
  35. let Dropzone = null;
  36. class DropzoneWrapper extends Component {
  37. state = {
  38. isClient: false,
  39. };
  40. componentDidMount = () => {
  41. Dropzone = require("react-dropzone").default;
  42. this.setState({ isClient: true });
  43. };
  44. render() {
  45. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  46. }
  47. }
  48. class InputRekomendasi extends Component {
  49. constructor(props) {
  50. super(props);
  51. this.state = {
  52. dropdownOpen: false,
  53. splitButtonOpen: false,
  54. files: [],
  55. sanksi: {},
  56. data: {},
  57. };
  58. }
  59. toggleSplit = () => {
  60. this.setState({
  61. splitButtonOpen: !this.state.splitButtonOpen,
  62. });
  63. };
  64. toggleDropDown = () => {
  65. this.setState({
  66. dropdownOpen: !this.state.dropdownOpen,
  67. });
  68. };
  69. onDrop = (files) => {
  70. this.setState({
  71. files: files.map((file) =>
  72. Object.assign(file, {
  73. preview: URL.createObjectURL(file),
  74. })
  75. ),
  76. stat: "Added " + files.length + " file(s)",
  77. });
  78. };
  79. uploadFiles = (e) => {
  80. e.preventDefault();
  81. e.stopPropagation();
  82. this.setState({
  83. stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
  84. });
  85. };
  86. clearFiles = (e) => {
  87. e.preventDefault();
  88. e.stopPropagation();
  89. this.setState({
  90. stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
  91. });
  92. this.setState({
  93. files: [],
  94. });
  95. };
  96. static getInitialProps = async ({ query }) => {
  97. return { query };
  98. };
  99. handelSimpan = async () => {
  100. const { token, query } = this.props;
  101. const { id } = query;
  102. const formdata = new FormData();
  103. this.state.files.forEach((e) => {
  104. formdata.append("dokumen", e);
  105. });
  106. const toastid = toast.loading("Please wait...");
  107. const added = await addRekomendasiDelegasi(token, id, formdata);
  108. if (!added) {
  109. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  110. } else {
  111. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  112. const sanksi = await getOneSanksi(token, id);
  113. this.setState({ sanksi, files: [] });
  114. resetForm();
  115. // Router.push({
  116. // pathname: "/app/rekomendasi-delegasi",
  117. // });
  118. }
  119. };
  120. render() {
  121. const { files } = this.state;
  122. const thumbs = files.map((file, index) => (
  123. <div md={3} key={index}>
  124. <span className="text-left">{index + 1}.{file.name}</span>
  125. </div>
  126. ));
  127. return (
  128. <Card className="card-default">
  129. <CardBody>
  130. <p className="lead bb">Dokumen Rekomendasi Delegasi</p>
  131. <Formik
  132. initialValues={{
  133. dokumen: [],
  134. }}
  135. validationSchema={rekomendasiSchema}
  136. onSubmit={async (data) => {
  137. this.setState({ data });
  138. await this.handelSimpan();
  139. }}
  140. >
  141. {() => (
  142. <Form className="form-horizontal">
  143. <FormGroup>
  144. <div className="row-md-10">
  145. <Field name="dokumen">
  146. {({ field, form }) => (
  147. <DropzoneWrapper
  148. className=""
  149. onDrop={(e) => {
  150. this.onDrop(e);
  151. form.setFieldValue(field.name, e);
  152. }}
  153. >
  154. {({ getRootProps, getInputProps, isDragActive }) => {
  155. return (
  156. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  157. <input {...getInputProps()} />
  158. <div className="dropzone-previews flex">
  159. <div className="dropzone-style-1">
  160. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ? <Row><span className="text-left">{thumbs}</span></Row> :
  161. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  162. <h5 className="text-center dz-default dz-message">upload dokumen rekomendasi delegasi</h5>
  163. </div>
  164. }
  165. </div>
  166. </div>
  167. </div>
  168. <div className="d-flex align-items-center">
  169. <small className="ml-auto">
  170. <button
  171. type="button"
  172. className="btn btn-link"
  173. onClick={(e) => {
  174. this.clearFiles(e);
  175. form.setFieldValue(field.name, []);
  176. }}
  177. >
  178. Reset dokumen
  179. </button>
  180. </small>
  181. </div>
  182. </div>
  183. );
  184. }}
  185. </DropzoneWrapper>
  186. )}
  187. </Field>
  188. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  189. <p className="mrgn-top-5">Ukuran setiap dokumen maksimal 15mb</p>
  190. </div>
  191. </FormGroup>
  192. <FormGroup row>
  193. <div className="col-xl-10">
  194. <Button color className="color-3e3a8e btn-login" type="submit">
  195. <span className="font-color-white">Kirim</span>
  196. </Button>
  197. </div>
  198. </FormGroup>
  199. </Form>
  200. )}
  201. </Formik>
  202. </CardBody>
  203. </Card>
  204. );
  205. }
  206. }
  207. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  208. export default connect(mapStateToProps)(InputRekomendasi);