calendar.view.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import React, { Component } from "react";
  2. import ContentWrapper from "@/components/Layout/ContentWrapper";
  3. import { Card, CardBody, CardHeader, CardTitle, Button, Row, Col, FormGroup, Input } from "reactstrap";
  4. import { getPelaporan, getOneLaporan, updateLaporan } from "@/actions/pelaporan";
  5. import { updateJadwal } from "@/actions/penjadwalan";
  6. import DetailLaporan from "@/components/Penjadwalan/DetailLaporan";
  7. import Link from "next/link";
  8. import FullCalendar from "@fullcalendar/react";
  9. import dayGridPlugin from "@fullcalendar/daygrid";
  10. import timeGridPlugin from "@fullcalendar/timegrid";
  11. import interactionPlugin, { Draggable } from "@fullcalendar/interaction";
  12. import listPlugin from "@fullcalendar/list";
  13. import bootstrapPlugin from "@fullcalendar/bootstrap";
  14. import Select from "react-select";
  15. import moment from "moment-timezone";
  16. import { connect } from "react-redux";
  17. import Loader from "@/components/Common/Loader";
  18. import Router from "next/router";
  19. import { ToastContainer, toast } from "react-toastify";
  20. import { Formik, Form, Field, ErrorMessage } from "formik";
  21. import * as Yup from "yup";
  22. import Datetime from "react-datetime";
  23. import 'moment/locale/id';
  24. import { getCsrf } from "../../actions/security";
  25. import { createLog } from "../../actions/log";
  26. moment.locale('id')
  27. import Swal from "sweetalert2";
  28. const status = [
  29. { value: "Ditindaklanjuti DIKTI", label: "Ditindaklanjuti DIKTI", className: "State-ACT" },
  30. { value: "Delegasi ke LLDIKTI", label: "Delegasi ke LLDIKTI", className: "State-ACT" },
  31. { value: "Ditutup", label: "Ditutup", className: "State-ACT" },
  32. ];
  33. const statusLLDIKTI = [
  34. { value: "Ditindaklanjuti LLDIKTI", label: "Ditindaklanjuti LLDIKTI", className: "State-ACT" },
  35. { value: "Delegasi ke DIKTI", label: "Delegasi ke DIKTI", className: "State-ACT" },
  36. { value: "Ditutup", label: "Ditutup", className: "State-ACT" },
  37. ];
  38. const jadwalSchema = Yup.object().shape({
  39. judul: Yup.string().required("Wajib diisi"),
  40. dari_tanggal: Yup.date().required("Wajib diisi"),
  41. sampai_tanggal: Yup.date().required("Wajib diisi"),
  42. });
  43. const laporanSchema = Yup.object().shape({
  44. keterangan: Yup.string().required("Harus diisi"),
  45. });
  46. class Calendar extends Component {
  47. calendarPlugins = [interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin, bootstrapPlugin];
  48. calendarHeader = {
  49. left: "prev,next today",
  50. center: "title",
  51. right: "dayGridMonth,timeGridWeek,timeGridDay",
  52. };
  53. constructor(props) {
  54. super(props);
  55. this.state = {
  56. selectedEvent: null,
  57. evRemoveOnDrop: true,
  58. evNewName: "",
  59. externalEvents: [],
  60. dataLaporan: [],
  61. dataEvent: [],
  62. laporan: {},
  63. selectedOption: null,
  64. color: "",
  65. disabled: true,
  66. };
  67. }
  68. static getInitialProps = ({ query }) => ({ query });
  69. async componentDidMount() {
  70. const { token, query } = this.props;
  71. const laporan = await getOneLaporan(token, query.id);
  72. const dataLaporan = await getPelaporan(token, { jadwal: true });
  73. this.setState({ dataLaporan });
  74. this.getDataEvent();
  75. this.defaultStatus();
  76. this.setState({ laporan });
  77. let color = "#" + Math.random().toString(16).slice(2, 8);
  78. if (laporan.data.jadwal) {
  79. color = laporan.data.jadwal.warna;
  80. }
  81. this.setState({ color });
  82. }
  83. componentShouldUpdate(nextProps, nextState) {
  84. return nextState.dataLaporan !== this.state.dataLaporan;
  85. }
  86. getStatus = () => (this.props.user?.role.id === 2021 ? statusLLDIKTI : status);
  87. getDataEvent = () => {
  88. const dataEvent = this.state.dataLaporan.data.map((e) => ({
  89. id: e._id,
  90. title: e.jadwal.judul,
  91. start: moment(e.jadwal.dari_tanggal).format("YYYY-MM-DD"),
  92. end: moment(e.jadwal.sampai_tanggal).add(1, "d").format("YYYY-MM-DD"),
  93. backgroundColor: e.jadwal.warna,
  94. borderColor: e.jadwal.warna,
  95. }));
  96. this.setState({ dataEvent });
  97. };
  98. eventClick = (info) => {
  99. const data = {
  100. title: info.event.title,
  101. start: moment(info.event.start).format("DD MMMM YYYY"),
  102. end: moment(info.event.end - 1).format("DD MMMM YYYY"),
  103. };
  104. this.setState({ selectedEvent: data });
  105. };
  106. handleEventReceive = (info) => {
  107. var styles = getComputedStyle(info.draggedEl);
  108. info.event.setProp("backgroundColor", styles.backgroundColor);
  109. info.event.setProp("borderColor", styles.borderColor);
  110. this.handleEventCalendar(info);
  111. };
  112. logSuccessUpdateJadwal = async () => {
  113. const { query, token } = this.props;
  114. const { id } = query;
  115. const getToken = await getCsrf();
  116. const _csrf = getToken.token;
  117. await createLog(token, { aktivitas: `Berhasil menetapkan jadwal, id: ${id}`, _csrf: _csrf });
  118. }
  119. handleEventCalendar = async (data) => {
  120. if (this.props.user.role.id === 2024) {
  121. Swal.fire({
  122. icon: 'error',
  123. title: 'Oops...',
  124. text: 'Maaf anda tidak memiliki akses untuk menyelesaikan proses ini.',
  125. confirmButtonColor: "#3e3a8e",
  126. confirmButtonText: 'Kembali'
  127. // footer: '<a href="">Why do I have this issue?</a>'
  128. })
  129. } else {
  130. const { query, token } = this.props;
  131. const { id } = query;
  132. const { color, laporan } = this.state;
  133. const getToken = await getCsrf();
  134. const _csrf = getToken.token;
  135. await toast.promise(
  136. updateJadwal(token, id, {
  137. judul: "No.Laporan " + laporan.data.no_laporan + " - " + data.judul,
  138. dari_tanggal: data.dari_tanggal,
  139. sampai_tanggal: data.sampai_tanggal,
  140. warna: color,
  141. }, _csrf),
  142. {
  143. pending: "Loading",
  144. success: "Success",
  145. error: "Error",
  146. }
  147. );
  148. Router.push("/app/penjadwalan");
  149. const dataLaporan = await getPelaporan(token, { jadwal: true });
  150. this.setState({ dataLaporan });
  151. this.getDataEvent();
  152. }
  153. };
  154. defaultStatus = async () => {
  155. const status = this.getStatus();
  156. return this.setState({ selectedOption: status[0] });
  157. };
  158. handleChangeSelect = (selectedOption) => this.setState({ selectedOption });
  159. logUpdateLaporanSuccess = async () => {
  160. const getToken = await getCsrf();
  161. const _csrf = getToken.token;
  162. const { token, query } = this.props;
  163. const { id } = query;
  164. await createLog(token, { aktivitas: `Berhasil mengubah data laporan, id: ${id}`, _csrf: _csrf });
  165. }
  166. logUpdateLaporanError = async () => {
  167. const getToken = await getCsrf();
  168. const _csrf = getToken.token;
  169. const { token, query } = this.props;
  170. const { id } = query;
  171. await createLog(token, { aktivitas: `Berhasil mengubah data laporan, id: ${id}`, _csrf: _csrf });
  172. }
  173. handleSimpan = async (value) => {
  174. if (this.props.user.role.id === 2024) {
  175. Swal.fire({
  176. icon: 'error',
  177. title: 'Oops...',
  178. text: 'Maaf anda tidak memiliki akses untuk menyelesaikan proses ini.',
  179. confirmButtonColor: "#3e3a8e",
  180. confirmButtonText: 'Kembali'
  181. // footer: '<a href="">Why do I have this issue?</a>'
  182. })
  183. } else {
  184. const getToken = await getCsrf();
  185. const _csrf = getToken.token;
  186. const { token, query } = this.props;
  187. const { id } = query;
  188. let update = null;
  189. if (value.status.value === this.getStatus()[1].value || value.status.value === this.getStatus()[2].value) {
  190. const toastid = toast.loading("Please wait...");
  191. const data = { keterangan: value.keterangan };
  192. if (value.status.value === this.getStatus()[1].value) {
  193. data.change_role = "true";
  194. update = await updateLaporan(token, id, data, _csrf);
  195. Router.push("/app/penjadwalan");
  196. } else if (value.status.value === this.getStatus()[2].value) {
  197. data.aktif = "false";
  198. update = await updateLaporan(token, id, data, _csrf);
  199. Router.push("/app/penjadwalan");
  200. }
  201. if (!update) {
  202. toast.update(toastid, { render: "Gagal simpan jadwal", type: "error", isLoading: false, autoClose: true, closeButton: true });
  203. this.logUpdateLaporanError()
  204. } else {
  205. toast.update(toastid, { render: "Input jadwal berhasil", type: "success", isLoading: false, autoClose: true, closeButton: true });
  206. this.logUpdateLaporanSuccess()
  207. Router.push("/app/penjadwalan");
  208. }
  209. Router.push("/app/penjadwalan");
  210. }
  211. Router.push("/app/penjadwalan");
  212. }
  213. };
  214. render() {
  215. const { externalEvents, laporan, selectedOption, selectedEvent } = this.state;
  216. return (
  217. <ContentWrapper>
  218. <div className="content-heading">
  219. <span className="font-color-white">
  220. Membuat Jadwal Pemeriksaan
  221. </span>
  222. <div className="ml-auto">
  223. <Link href="/app/penjadwalan">
  224. <Button className="color-3e3a8e" color>
  225. <span className="font-color-white">
  226. &lt; Kembali
  227. </span>
  228. </Button>
  229. </Link>
  230. </div>
  231. </div>
  232. <div className="calendar-app">
  233. {laporan.data ? (
  234. <Row>
  235. <Col>
  236. <Card className="card-default">
  237. <CardBody>
  238. <DetailLaporan noStatus data={laporan.data} />
  239. </CardBody>
  240. </Card>
  241. </Col>
  242. </Row>
  243. ) : (
  244. <Row className="mb-4">
  245. <Col>
  246. <Loader />
  247. </Col>
  248. </Row>
  249. )}
  250. <div className="row">
  251. <div className="col-xl-4 col-lg-5">
  252. <div className="row">
  253. <div className="col-lg-12 col-md-6 col-12">
  254. <Card className="card-default">
  255. <div class="header-penjadwalan">
  256. <h2 class="card-title-1">Status Pelaporan</h2>
  257. </div>
  258. <CardBody>
  259. <Formik
  260. enableReinitialize={true}
  261. initialValues={{
  262. status: this.getStatus()[0],
  263. keterangan: "",
  264. }}
  265. validationSchema={selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? laporanSchema : null}
  266. onSubmit={this.handleSimpan}
  267. >
  268. {({ isSubmitting }) => (
  269. <Form>
  270. <FormGroup>
  271. <label className="col-form-label">Status Laporan</label>
  272. <Field name="status">
  273. {({ field, form }) => (
  274. <Select
  275. value={field.value}
  276. onChange={(e) => {
  277. form.setFieldValue(field.name, e);
  278. this.handleChangeSelect(e);
  279. }}
  280. options={this.getStatus()}
  281. required
  282. isDisabled={laporan.data?.sanksi}
  283. />
  284. )}
  285. </Field>
  286. <ErrorMessage name="status" component="div" className="form-text text-danger" />
  287. </FormGroup>
  288. {selectedOption?.value === this.getStatus()[0].value ? (
  289. ""
  290. ) : (
  291. <FormGroup>
  292. <label className="col-form-label">Keterangan<span className=" text-danger">*</span></label>
  293. <Field name="keterangan">{({ field, form }) => <Input type="text" placeholder="Keterangan" {...field} />}</Field>
  294. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  295. </FormGroup>
  296. )}
  297. <div className="btn-simpanjadwal">
  298. <Button className="text-btn-penjadwalan-1 btn-colorpenjadwalan color-3e3a8e" color block disabled={laporan.data?.sanksi || isSubmitting} >
  299. <h4 className="text-btn-penjadwalan-1 font-color-white">Simpan</h4>
  300. </Button>
  301. </div>
  302. </Form>
  303. )}
  304. </Formik>
  305. </CardBody>
  306. </Card>
  307. {selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? (
  308. ""
  309. ) : (
  310. <>
  311. <Card className="card-default" title="">
  312. <div class="header-penjadwalan">
  313. <h2 className="card-title-1">Input Jadwal</h2>
  314. </div>
  315. <CardBody>
  316. <Formik
  317. enableReinitialize={true}
  318. initialValues={{
  319. judul: laporan.data?.jadwal?.judul ? laporan.data.jadwal.judul.split("- ")[1] : "",
  320. dari_tanggal: laporan.data?.jadwal?.dari_tanggal ? moment(laporan.data.jadwal.dari_tanggal).format("DD MMMM YYYY") : "",
  321. sampai_tanggal: laporan.data?.jadwal?.sampai_tanggal ? moment(laporan.data.jadwal.sampai_tanggal).format("DD MMMM YYYY") : "",
  322. }}
  323. validationSchema={jadwalSchema}
  324. onSubmit={this.handleEventCalendar}
  325. >
  326. {({ isSubmitting }) => (
  327. <Form>
  328. <FormGroup>
  329. <label className="col-form-label">Warna</label>
  330. <div className="badge d-block" style={{ backgroundColor: this.state.color, color: "white" }}>
  331. {this.state.color}
  332. </div>
  333. {/* <div className="warna-penjadwalan-block" style={{ backgroundColor: this.state.color, color: "white" }}></div> */}
  334. </FormGroup>
  335. <FormGroup>
  336. <label className="col-form-label">Judul<span className=" text-danger">*</span></label>
  337. <Field name="judul">{({ field, form }) => <Input disabled={laporan.data?.sanksi} type="text" placeholder="judul" {...field} />}</Field>
  338. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  339. </FormGroup>
  340. <Row>
  341. <Col>
  342. <FormGroup>
  343. <label className="col-form-label">Dari Tanggal</label>
  344. <Field name="dari_tanggal">
  345. {({ field, form }) => (
  346. <Datetime
  347. timeFormat={false}
  348. locale="id"
  349. inputProps={{ className: "form-control" }}
  350. value={field.value}
  351. dateFormat="DD MMMM YYYY"
  352. closeOnSelect={true}
  353. onChange={(e) => {
  354. form.setFieldValue(field.name, e);
  355. }}
  356. />
  357. )}
  358. </Field>
  359. <ErrorMessage name="dari_tanggal" component="div" className="form-text text-danger" />
  360. </FormGroup>
  361. </Col>
  362. <Col>
  363. <FormGroup>
  364. <label className="col-form-label">Sampai Tanggal</label>
  365. <Field name="sampai_tanggal">
  366. {({ field, form }) => (
  367. <Datetime
  368. timeFormat={false}
  369. locale="id"
  370. dateFormat="DD MMMM YYYY"
  371. inputProps={{ className: "form-control" }}
  372. value={field.value}
  373. closeOnSelect={true}
  374. onChange={(e) => {
  375. form.setFieldValue(field.name, e);
  376. }}
  377. />
  378. )}
  379. </Field>
  380. <ErrorMessage name="sampai_tanggal" component="div" className="form-text text-danger" />
  381. </FormGroup>
  382. </Col>
  383. </Row>
  384. <FormGroup>
  385. <div className="btn-simpanpenjadwalan">
  386. <Button className="btn-colorpenjadwalan" color block type="submit" disabled={laporan.data?.sanksi || isSubmitting}>
  387. <h4 className="text-btn-penjadwalan-1 font-color-white">Simpan</h4>
  388. </Button>
  389. </div>
  390. </FormGroup>
  391. </Form>
  392. )}
  393. </Formik>
  394. </CardBody>
  395. </Card>
  396. </>
  397. )}
  398. <div className="mb-3">
  399. {selectedEvent && (
  400. <div>
  401. <p>Selected:</p>
  402. <div className="box-placeholder">{JSON.stringify(selectedEvent)}</div>
  403. </div>
  404. )}
  405. {!selectedEvent && (
  406. <div>
  407. <p>Click calendar to show information</p>
  408. </div>
  409. )}
  410. </div>
  411. </div>
  412. </div>
  413. </div>
  414. <div className="col-xl-8 col-lg-7">
  415. <Card className="card-default">
  416. <CardBody>
  417. {/* START calendar */}
  418. <FullCalendar
  419. defaultView={this.dayGridMonth}
  420. plugins={this.calendarPlugins}
  421. events={this.state.dataEvent}
  422. themeSystem={"bootstrap"}
  423. header={this.calendarHeader}
  424. deepChangeDetection={true}
  425. eventClick={this.eventClick}
  426. ></FullCalendar>
  427. </CardBody>
  428. </Card>
  429. </div>
  430. </div>
  431. </div>
  432. </ContentWrapper>
  433. );
  434. }
  435. }
  436. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  437. export default connect(mapStateToProps)(Calendar);