newfile.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import { connect } from "react-redux";
  4. import ContentWrapper from "@/components/Layout/ContentWrapper";
  5. import { Row, Col, Card, CardHeader, CardBody, Button, FormGroup, Popover, PopoverHeader } from "reactstrap";
  6. // import { Editor } from 'react-draft-wysiwyg';
  7. import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
  8. import { EditorState, ContentState, convertFromHTML, convertToRaw, convertFromRaw } from 'draft-js';
  9. // import 'draft-js/dist/Draft.css';
  10. import dynamic from 'next/dynamic';
  11. const Editor = dynamic(
  12. () => import('react-draft-wysiwyg').then(mod => mod.Editor),
  13. { ssr: false })
  14. import draftToHtml from 'draftjs-to-html';
  15. import pdfMake from "pdfmake/build/pdfmake";
  16. import StateToPdfMake from "draft-js-export-pdfmake";
  17. import pdfFonts from "pdfmake/build/vfs_fonts";
  18. import { toast } from "react-toastify";
  19. import { addCatatan } from "../../../actions/catatan";
  20. import { getCsrf } from "../../../actions/security";
  21. pdfMake.vfs = pdfFonts.pdfMake.vfs;
  22. import SummerNote from "../../../components/Extras/summernote";
  23. class NewFile extends Component {
  24. constructor(props) {
  25. super(props);
  26. this.editor = React.createRef();
  27. this.state = {
  28. editorState: "<p>dfgergergr</p><p>hgrfhfghf</p><p>trhrhrt</p>",
  29. copiedd: false,
  30. judul: "",
  31. simpan: false,
  32. menu: "",
  33. };
  34. }
  35. static getInitialProps = async ({ query }) => {
  36. return { query };
  37. };
  38. componentDidMount() {
  39. const { query } = this.props;
  40. const { id } = query;
  41. }
  42. // onEditorStateChange = (editorState) => {
  43. // this.setState({
  44. // editorState,
  45. // });
  46. // };
  47. handelSimpan = async (data) => {
  48. if (this.props?.user?.role.id === 2071) {
  49. Swal.fire({
  50. icon: 'error',
  51. title: 'Oops...',
  52. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  53. confirmButtonColor: "#3e3a8e",
  54. confirmButtonText: 'Oke'
  55. })
  56. } else {
  57. const getToken = await getCsrf();
  58. const _csrf = getToken.token;
  59. const { token, query } = this.props;
  60. const { id } = query;
  61. // const isi = ("editorState", draftToHtml(convertToRaw(this.state.editorState.getCurrentContent())))
  62. const toastid = toast.loading("Please wait...");
  63. const added = await addCatatan(token, id, {
  64. "judul": this.state.judul,
  65. "isi": this.state.editorState,
  66. "menu": "Perpanjangan Sanksi"
  67. }, _csrf);
  68. if (!added) {
  69. toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  70. } else {
  71. toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  72. Router.push(`/app/perpanjangan-sanksi/detail?id=${id}`);
  73. // this.setState({ simpan: true })
  74. }
  75. }
  76. };
  77. handleGeneratePDF = () => {
  78. const rawContent = convertToRaw(this.state.editorState.getCurrentContent());
  79. console.log(rawContent)
  80. const stateToPdfMake = new StateToPdfMake(rawContent);
  81. console.log(stateToPdfMake.generate());
  82. pdfMake.createPdf(stateToPdfMake.generate()).download();
  83. };
  84. setHandleJudul = (e) => {
  85. this.setState({ judul: e.target.value });
  86. };
  87. CloseCopiedd = () => {
  88. setTimeout(() => {
  89. this.setState({
  90. copiedd: !this.state.copiedd
  91. });
  92. }, 1000);
  93. }
  94. Copiedd = () => this.setState({
  95. copiedd: !this.state.copiedd
  96. })
  97. // convertFileToBase64(file) {
  98. // return new Promise((resolve, reject) => {
  99. // const reader = new FileReader()
  100. // reader.readAsDataURL(file)
  101. // reader.onload = () => resolve(reader.result)
  102. // reader.onerror = () => reject(console.log('FileReader Error: ', error))
  103. // })
  104. // }
  105. // onImageUpload = (f, cb, e) => {
  106. // let file = f
  107. // if (file.length) {
  108. // file = f[0]
  109. // }
  110. // this.convertFileToBase64(file).then(src => {
  111. // const $image = $('<img>').attr('src', src).wrap('div')
  112. // this.editor.current.insertNode($image[0])
  113. // })
  114. // }
  115. render() {
  116. const { editorState } = this.state;
  117. return (
  118. <ContentWrapper>
  119. <meta charset="UTF-8" />
  120. <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  121. <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet" />
  122. <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>
  123. <Row>
  124. <Col lg={12}>
  125. <Card body className="card-default">
  126. <FormGroup row>
  127. <label className="col-md-1 col-form-label">Judul Catatan</label>
  128. <div className="col-md-11">
  129. <input type="text" name="article-title" placeholder="Judul Catatan" className="mb-3 form-control form-control-lg" onChange={this.setHandleJudul} />
  130. </div>
  131. </FormGroup>
  132. <SummerNote
  133. id='editor1'
  134. destroy={false}
  135. // value={editorState}
  136. options={{
  137. lang: "eng",
  138. height: 800,
  139. dialogsInBody: true,
  140. toolbar: [
  141. ["style", ["style", "customStyle", "copyFormatting"]],
  142. ["font", ["bold", "italic", "underline", "strikethrough", "superscript", "subscript", "clear", "customCleaner"]],
  143. ["fontname", ["fontname", "customFont"]],
  144. ["fontsize", ["fontsizeInput"]],
  145. ['color', ['forecolor', 'backcolor']],
  146. ["para", ["ul", "ol", "listStyles", "paragraph"]],
  147. ["table", ["table", "jMerge", "jBackcolor", "jBorderColor", "jAlign", "jTableInfo", "jWidthHeightReset"]],
  148. ["tableRow", ["jRowHeight"]],
  149. ["tableCol", ["jColWidth"]],
  150. ["insert", ["pasteHTML", "link", "unlink", "picture", "imageMap", "video", "customSpecialChar"]],
  151. ["anchor", ["anchor", "toc", "markAnchor", "editAnchor"]],
  152. ["comment", ["editPopover", "removePopover"]],
  153. // ["view", ["fullscreen", "codeview", "help"]],
  154. ],
  155. canViewClasslist: true,
  156. tableClassName: 'jtable table-bordered color-black',
  157. customFont: {
  158. fontNames: [
  159. 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New', 'Helvetica', 'Impact', 'Tahoma', 'Times New Roman', 'Verdana',
  160. { name: '新細明體', value: '新細明體, serif' },
  161. { name: '微軟正黑體', value: '微軟正黑體, sans-serif' },
  162. { name: '標楷體', value: '標楷體, DFKai-SB, BiauKaiTC' }
  163. ]
  164. },
  165. }}
  166. onChange={(editorState) => {
  167. this.setState({
  168. editorState,
  169. });
  170. }}
  171. // onImageUpload={this.onImageUpload}
  172. // ref={this.editor}
  173. />
  174. <br />
  175. <Row>
  176. <Col>
  177. {this.state.simpan === true ?
  178. (
  179. <div>
  180. <span className="btn-radius">
  181. <Button color id="Popover1" className="ml-0 mb-2 btn-v1-outline-purple" onClick={() => {
  182. this.CloseCopiedd()
  183. }} ><span><em className="fas fa-project-diagram float-left mt-1 mr-1" />&nbsp;Link Dokumen Berita Acara</span>
  184. {/* <h5 className="p-0 mt-3 float-right"> <em className="fas fa-project-diagram float-left mt-1" /> &nbsp;Link Dokumen Berita Acara</h5> */}
  185. </Button>
  186. </span>
  187. <Popover placement="bottom" isOpen={this.state.copiedd} target="Popover1" toggle={this.Copiedd}>
  188. <PopoverHeader>Link Berhasil Disalin</PopoverHeader>
  189. </Popover>
  190. </div>
  191. ) :
  192. (
  193. <div>
  194. <span className=" text-danger">
  195. *Klik tombol &#39;Simpan&#39; untuk mendapatkan link presensi
  196. </span>
  197. </div>
  198. )
  199. }
  200. </Col>
  201. <Col>
  202. <div>
  203. <Button color className="btn-login float-right" onClick={this.handelSimpan} disabled={this.state.judul === ""}>
  204. <span className="font-color-white">
  205. Simpan Catatan
  206. </span>
  207. </Button>
  208. </div>
  209. </Col>
  210. </Row>
  211. </Card>
  212. </Col>
  213. </Row>
  214. </ContentWrapper>
  215. );
  216. }
  217. }
  218. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  219. export default connect(mapStateToProps)(NewFile);