calendar.view.js 14 KB

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