calendar.view.js 14 KB

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