calendar.view.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. 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. await toast.promise(
  114. updateJadwal(token, id, {
  115. judul: "No.Laporan " + laporan.data.no_laporan + " - " + data.judul,
  116. dari_tanggal: data.dari_tanggal,
  117. sampai_tanggal: data.sampai_tanggal,
  118. warna: color,
  119. }),
  120. {
  121. pending: "Loading",
  122. success: "Success",
  123. error: "Error",
  124. }
  125. );
  126. const dataLaporan = await getPelaporan(token, { jadwal: true });
  127. this.setState({ dataLaporan });
  128. this.getDataEvent();
  129. };
  130. defaultStatus = async () => {
  131. const status = this.getStatus();
  132. return this.setState({ selectedOption: status[0] });
  133. };
  134. handleChangeSelect = (selectedOption) => this.setState({ selectedOption });
  135. handleSimpan = async (value) => {
  136. const { token, query } = this.props;
  137. const { id } = query;
  138. let update = null;
  139. if (value.status.value === this.getStatus()[1].value || value.status.value === this.getStatus()[2].value) {
  140. const toastid = toast.loading("Please wait...");
  141. const data = { keterangan: value.keterangan };
  142. if (value.status.value === this.getStatus()[1].value) {
  143. data.change_role = "true";
  144. update = await updateLaporan(token, id, data);
  145. } else if (value.status.value === this.getStatus()[2].value) {
  146. data.aktif = "false";
  147. update = await updateLaporan(token, id, data);
  148. }
  149. if (!update) {
  150. toast.update(toastid, { render: "Gagal simpan jadwal", type: "error", isLoading: false, autoClose: true, closeButton: true });
  151. } else {
  152. toast.update(toastid, { render: "Input jadwal berhasil", type: "success", isLoading: false, autoClose: true, closeButton: true });
  153. Router.push("/app/penjadwalan");
  154. }
  155. }
  156. };
  157. render() {
  158. const { externalEvents, laporan, selectedOption, selectedEvent } = this.state;
  159. return (
  160. <ContentWrapper>
  161. <div className="content-heading">
  162. <span className="font-color-white">
  163. Membuat Jadwal Pemeriksaan
  164. </span>
  165. <div className="ml-auto">
  166. <Link href="/app/penjadwalan">
  167. <Button className="color-3e3a8e" color>
  168. <span className="font-color-white">
  169. &lt; Kembali
  170. </span>
  171. </Button>
  172. </Link>
  173. </div>
  174. </div>
  175. <div className="calendar-app">
  176. {laporan.data ? (
  177. <Row>
  178. <Col>
  179. <Card className="card-default">
  180. <CardBody>
  181. <DetailLaporan noStatus data={laporan.data} />
  182. </CardBody>
  183. </Card>
  184. </Col>
  185. </Row>
  186. ) : (
  187. <Row className="mb-4">
  188. <Col>
  189. <Loader />
  190. </Col>
  191. </Row>
  192. )}
  193. <div className="row">
  194. <div className="col-xl-4 col-lg-5">
  195. <div className="row">
  196. <div className="col-lg-12 col-md-6 col-12">
  197. <Card className="card-default">
  198. <div class="header-penjadwalan">
  199. <h2 class="card-title-1">Status Pelaporan</h2>
  200. </div>
  201. <CardBody>
  202. <Formik
  203. enableReinitialize={true}
  204. initialValues={{
  205. status: this.getStatus()[0],
  206. keterangan: "",
  207. }}
  208. validationSchema={selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? laporanSchema : null}
  209. onSubmit={this.handleSimpan}
  210. >
  211. {() => (
  212. <Form>
  213. <FormGroup>
  214. <label className="col-form-label">Status Laporan</label>
  215. <Field name="status">
  216. {({ field, form }) => (
  217. <Select
  218. value={field.value}
  219. onChange={(e) => {
  220. form.setFieldValue(field.name, e);
  221. this.handleChangeSelect(e);
  222. }}
  223. options={this.getStatus()}
  224. required
  225. isDisabled={laporan.data?.sanksi}
  226. />
  227. )}
  228. </Field>
  229. <ErrorMessage name="status" component="div" className="form-text text-danger" />
  230. </FormGroup>
  231. {selectedOption?.value === this.getStatus()[0].value ? (
  232. ""
  233. ) : (
  234. <FormGroup>
  235. <label className="col-form-label">Keterangan<span className=" text-danger">*</span></label>
  236. <Field name="keterangan">{({ field, form }) => <Input type="text" placeholder="Keterangan" {...field} />}</Field>
  237. <ErrorMessage name="keterangan" component="div" className="form-text text-danger" />
  238. </FormGroup>
  239. )}
  240. <div className="btn-simpanjadwal">
  241. <Button className="text-btn-penjadwalan-1 btn-colorpenjadwalan color-3e3a8e" color block disabled={laporan.data?.sanksi}>
  242. <h4 className="text-btn-penjadwalan-1 font-color-white">Simpan</h4>
  243. </Button>
  244. </div>
  245. </Form>
  246. )}
  247. </Formik>
  248. </CardBody>
  249. </Card>
  250. {selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? (
  251. ""
  252. ) : (
  253. <>
  254. <Card className="card-default" title="">
  255. <div class="header-penjadwalan">
  256. <h2 className="card-title-1">Input Jadwal</h2>
  257. </div>
  258. <CardBody>
  259. <Formik
  260. enableReinitialize={true}
  261. initialValues={{
  262. judul: laporan.data?.jadwal?.judul ? laporan.data.jadwal.judul.split("- ")[1] : "",
  263. dari_tanggal: laporan.data?.jadwal?.dari_tanggal ? moment(laporan.data.jadwal.dari_tanggal).format("DD MMMM YYYY") : "",
  264. sampai_tanggal: laporan.data?.jadwal?.sampai_tanggal ? moment(laporan.data.jadwal.sampai_tanggal).format("DD MMMM YYYY") : "",
  265. }}
  266. validationSchema={jadwalSchema}
  267. onSubmit={this.handleEventCalendar}
  268. >
  269. {() => (
  270. <Form>
  271. <FormGroup>
  272. <label className="col-form-label">Warna</label>
  273. <div className="badge d-block" style={{ backgroundColor: this.state.color, color: "white" }}>
  274. {this.state.color}
  275. </div>
  276. {/* <div className="warna-penjadwalan-block" style={{ backgroundColor: this.state.color, color: "white" }}></div> */}
  277. </FormGroup>
  278. <FormGroup>
  279. <label className="col-form-label">Judul<span className=" text-danger">*</span></label>
  280. <Field name="judul">{({ field, form }) => <Input disabled={laporan.data?.sanksi} type="text" placeholder="judul" {...field} />}</Field>
  281. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  282. </FormGroup>
  283. <Row>
  284. <Col>
  285. <FormGroup>
  286. <label className="col-form-label">Dari Tanggal</label>
  287. <Field name="dari_tanggal">
  288. {({ field, form }) => (
  289. <Datetime
  290. timeFormat={false}
  291. locale="en-gb"
  292. inputProps={{ className: "form-control" }}
  293. value={field.value}
  294. onChange={(e) => {
  295. form.setFieldValue(field.name, e.format("DD MMMM YYYY"));
  296. }}
  297. />
  298. )}
  299. </Field>
  300. <ErrorMessage name="dari_tanggal" component="div" className="form-text text-danger" />
  301. </FormGroup>
  302. </Col>
  303. <Col>
  304. <FormGroup>
  305. <label className="col-form-label">Sampai Tanggal</label>
  306. <Field name="sampai_tanggal">
  307. {({ field, form }) => (
  308. <Datetime
  309. timeFormat={false}
  310. locale="en-gb"
  311. inputProps={{ className: "form-control" }}
  312. value={field.value}
  313. onChange={(e) => {
  314. form.setFieldValue(field.name, e.format("DD MMMM YYYY"));
  315. }}
  316. />
  317. )}
  318. </Field>
  319. <ErrorMessage name="sampai_tanggal" component="div" className="form-text text-danger" />
  320. </FormGroup>
  321. </Col>
  322. </Row>
  323. <FormGroup>
  324. <div className="btn-simpanpenjadwalan">
  325. <Button className="btn-colorpenjadwalan" color block type="submit" disabled={laporan.data?.sanksi}>
  326. <h4 className="text-btn-penjadwalan-1 font-color-white">Simpan</h4>
  327. </Button>
  328. </div>
  329. </FormGroup>
  330. </Form>
  331. )}
  332. </Formik>
  333. </CardBody>
  334. </Card>
  335. </>
  336. )}
  337. <div className="mb-3">
  338. {selectedEvent && (
  339. <div>
  340. <p>Selected:</p>
  341. <div className="box-placeholder">{JSON.stringify(selectedEvent)}</div>
  342. </div>
  343. )}
  344. {!selectedEvent && (
  345. <div>
  346. <p>Click calendar to show information</p>
  347. </div>
  348. )}
  349. </div>
  350. </div>
  351. </div>
  352. </div>
  353. <div className="col-xl-8 col-lg-7">
  354. <Card className="card-default">
  355. <CardBody>
  356. {/* START calendar */}
  357. <FullCalendar
  358. defaultView={this.dayGridMonth}
  359. plugins={this.calendarPlugins}
  360. events={this.state.dataEvent}
  361. themeSystem={"bootstrap"}
  362. header={this.calendarHeader}
  363. deepChangeDetection={true}
  364. eventClick={this.eventClick}
  365. ></FullCalendar>
  366. </CardBody>
  367. </Card>
  368. </div>
  369. </div>
  370. </div>
  371. </ContentWrapper>
  372. );
  373. }
  374. }
  375. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  376. export default connect(mapStateToProps)(Calendar);