calendar.view.js 12 KB

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