InputRekomendasi.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React, { Component } from "react";
  2. import { insertPemeriksaan } from "@/actions/pemeriksaan";
  3. import { Row, Col, Card, CardBody, FormGroup, Input, Button, Progress } 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. import moment from "moment";
  10. import 'moment/min/locales';
  11. import { getCsrf } from "../../actions/security";
  12. moment.locale('id');
  13. const selectInstanceId = 1;
  14. const checkIfFilesAreTooBig = (files) => {
  15. let valid = true;
  16. if (files) {
  17. files.map((file) => {
  18. if (file.size > 15 * 1024 * 1024) {
  19. valid = false;
  20. }
  21. });
  22. }
  23. return valid;
  24. };
  25. const checkIfFilesAreCorrectType = (files) => {
  26. let valid = true;
  27. if (files) {
  28. files.map((file) => {
  29. if (!["image/jpeg", "image/png"].includes(file.type)) {
  30. valid = false;
  31. }
  32. });
  33. }
  34. return valid;
  35. };
  36. const rekomendasiSchema = Yup.object().shape({
  37. dokumen: Yup.array().min(1).required("Wajib diisi").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
  38. });
  39. let Dropzone = null;
  40. class DropzoneWrapper extends Component {
  41. state = {
  42. isClient: false,
  43. };
  44. componentDidMount = () => {
  45. Dropzone = require("react-dropzone").default;
  46. this.setState({ isClient: true });
  47. };
  48. render() {
  49. return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
  50. }
  51. }
  52. class InputRekomendasi extends Component {
  53. constructor(props) {
  54. super(props);
  55. this.state = {
  56. dropdownOpen: false,
  57. splitButtonOpen: false,
  58. files: [],
  59. sanksi: {},
  60. data: {},
  61. };
  62. }
  63. toggleSplit = () => {
  64. this.setState({
  65. splitButtonOpen: !this.state.splitButtonOpen,
  66. });
  67. };
  68. toggleDropDown = () => {
  69. this.setState({
  70. dropdownOpen: !this.state.dropdownOpen,
  71. });
  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. static getInitialProps = async ({ query }) => {
  101. return { query };
  102. };
  103. handelSimpan = async () => {
  104. const getToken = await getCsrf();
  105. const _csrf = getToken.token;
  106. const { token, query } = this.props;
  107. const { id } = query;
  108. const formdata = new FormData();
  109. this.state.files.forEach((e) => {
  110. formdata.append("dokumen", e);
  111. });
  112. const toastid = toast.loading("Please wait...");
  113. const added = await addRekomendasiDelegasi(token, id, formdata, _csrf);
  114. if (!added) {
  115. toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  116. } else {
  117. toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  118. const sanksi = await getOneSanksi(token, id);
  119. this.setState({ sanksi, files: [] });
  120. resetForm();
  121. // Router.push({
  122. // pathname: "/app/rekomendasi-delegasi",
  123. // });
  124. }
  125. };
  126. render() {
  127. const { files } = this.state;
  128. const removeFile = file => () => {
  129. const newFiles = [...files]
  130. newFiles.splice(newFiles.indexOf(file), 1)
  131. this.setState({
  132. files: newFiles,
  133. });
  134. }
  135. const thumbs = files.map((file, index) => (
  136. <p>
  137. <em className="far fa-file" />&nbsp;&nbsp;{file.name}
  138. <button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
  139. </p>
  140. ));
  141. return (
  142. <Card className="card-default">
  143. <CardBody>
  144. <p className="lead bb">Dokumen Rekomendasi Delegasi</p>
  145. <Formik
  146. initialValues={{
  147. dokumen: [],
  148. }}
  149. validationSchema={rekomendasiSchema}
  150. onSubmit={async (data) => {
  151. this.setState({ data });
  152. await this.handelSimpan();
  153. }}
  154. >
  155. {() => (
  156. <Form className="form-horizontal">
  157. <FormGroup>
  158. <div className="row-md-10">
  159. <Field name="dokumen">
  160. {({ field, form }) => (
  161. <DropzoneWrapper
  162. className=""
  163. onDrop={(e) => {
  164. this.onDrop(e);
  165. form.setFieldValue(field.name, e);
  166. }}
  167. >
  168. {({ getRootProps, getInputProps, isDragActive }) => {
  169. return (
  170. <div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
  171. <input {...getInputProps()} />
  172. <div className="dropzone-previews flex">
  173. <div className="dropzone-style-1">
  174. <div className="center-ver-hor dropzone-previews flex">{this.state.files.length > 0 ?
  175. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  176. <h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
  177. </div> :
  178. <div className="text-center fa-2x icon-cloud-upload mr-2 ">
  179. <h5 className="text-center dz-default dz-message">upload dokumen rekomendasi delegasi</h5>
  180. </div>
  181. }
  182. </div>
  183. </div>
  184. </div>
  185. <div className="d-flex align-items-center">
  186. <small className="ml-auto">
  187. <button
  188. type="button"
  189. className="btn btn-link"
  190. onClick={(e) => {
  191. this.clearFiles(e);
  192. form.setFieldValue(field.name, []);
  193. }}
  194. >
  195. Reset dokumen
  196. </button>
  197. </small>
  198. </div>
  199. </div>
  200. );
  201. }}
  202. </DropzoneWrapper>
  203. )}
  204. </Field>
  205. <div>
  206. {thumbs}
  207. </div>
  208. <ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
  209. <p className="mrgn-top-5 font-color-black">Ukuran setiap dokumen maksimal 15mb</p>
  210. </div>
  211. </FormGroup>
  212. <FormGroup row>
  213. <div className="col-xl-10">
  214. <Button color className="color-3e3a8e btn-login" type="submit">
  215. <span className="font-color-white">Kirim</span>
  216. </Button>
  217. </div>
  218. </FormGroup>
  219. </Form>
  220. )}
  221. </Formik>
  222. </CardBody>
  223. </Card>
  224. );
  225. }
  226. }
  227. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  228. export default connect(mapStateToProps)(InputRekomendasi);