calendar.view.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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";
  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: new Date(e.jadwal.dari_tanggal),
  83. end: new Date(e.jadwal.sampai_tanggal),
  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 toastid = toast.loading("Please wait...");
  131. try {
  132. const { selectedOption } = this.state;
  133. const { token, query } = this.props;
  134. const { id } = query;
  135. this.setState({ selectedOption });
  136. if (selectedOption.value === this.getStatus()[1].value) {
  137. await updateLaporan(token, id, { change_role: "true" });
  138. }
  139. if (selectedOption.value === this.getStatus()[2].value) {
  140. await updateLaporan(token, id, { aktif: "false" });
  141. }
  142. Router.push("/app/penjadwalan");
  143. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  144. } catch (error) {
  145. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  146. }
  147. };
  148. render() {
  149. const { externalEvents, laporan, selectedOption, selectedEvent } = this.state;
  150. return (
  151. <ContentWrapper>
  152. <div className="content-heading">
  153. <div><h3>Jadwal Pemeriksaan</h3></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. {/* <CardHeader>
  184. <CardTitle tag="h4">Status Pelaporan</CardTitle>
  185. </CardHeader> */}
  186. <div class="header-penjadwalan">
  187. <h2 class="card-title-1">Status Pelaporan</h2>
  188. </div>
  189. <CardBody>
  190. <Select value={selectedOption} onChange={this.handleChangeSelect} options={this.getStatus()} required />
  191. <div className="btn-simpanjadwal">
  192. <Button onClick={this.handleSimpan} className="btn-colorpenjadwalan" color="info" block disabled={laporan.data?.evaluasi.length}>
  193. <h4 className="text-btn-penjadwalan-1">Simpan</h4>
  194. </Button>
  195. </div>
  196. </CardBody>
  197. </Card>
  198. {selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? (
  199. ""
  200. ) : (
  201. <>
  202. <Card className="card-default" title="">
  203. {/* <CardHeader>
  204. <CardTitle tag="h4">Input Jadwal</CardTitle>
  205. </CardHeader> */}
  206. <div class="header-penjadwalan">
  207. <h2 class="card-title-1">Input Jadwal</h2>
  208. </div>
  209. <CardBody>
  210. <Formik
  211. enableReinitialize={true}
  212. initialValues={{
  213. judul: laporan.data?.jadwal?.judul ? laporan.data.jadwal.judul.split("- ")[1] : "",
  214. dari_tanggal: laporan.data?.jadwal?.dari_tanggal ? moment(laporan.data.jadwal.dari_tanggal) : "",
  215. sampai_tanggal: laporan.data?.jadwal?.sampai_tanggal ? moment(laporan.data.jadwal.sampai_tanggal) : "",
  216. }}
  217. validationSchema={jadwalSchema}
  218. onSubmit={this.handleEventCalendar}
  219. >
  220. {() => (
  221. <Form>
  222. <FormGroup>
  223. {/* <label className="col-form-label">Warna</label> */}
  224. <div className="warna-penjadwalan-block" style={{ backgroundColor: this.state.color, color: "white" }}>
  225. {/* {this.state.color} */}
  226. </div>
  227. </FormGroup>
  228. <FormGroup>
  229. <label className="col-form-label">Judul</label>
  230. <Field name="judul">{({ field, form }) => <Input type="text" placeholder="judul" {...field} />}</Field>
  231. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  232. </FormGroup>
  233. <FormGroup className="uk-form-tanggal-1">
  234. {/* <div className="uk-form-tanggal-1"> */}
  235. <label className="col-form-label">Dari Tanggal</label>
  236. <Field name="dari_tanggal">
  237. {({ field, form }) => (
  238. <Datetime
  239. timeFormat={false}
  240. inputProps={{ className: "form-control" }}
  241. value={field.value}
  242. onChange={(e) => {
  243. form.setFieldValue(field.name, e);
  244. }}
  245. />
  246. )}
  247. </Field>
  248. <ErrorMessage name="dari_tanggal" component="div" className="form-text text-danger" />
  249. {/* </div> */}
  250. </FormGroup>
  251. <FormGroup className="uk-form-tanggal-2">
  252. {/* <div className="uk-form-tanggal-2"> */}
  253. <label className="col-form-label">Sampai Tanggal</label>
  254. <Field name="sampai_tanggal">
  255. {({ field, form }) => (
  256. <Datetime
  257. timeFormat={false}
  258. inputProps={{ className: "form-control" }}
  259. value={field.value}
  260. onChange={(e) => {
  261. form.setFieldValue(field.name, e);
  262. }}
  263. />
  264. )}
  265. </Field>
  266. <ErrorMessage name="sampai_tanggal" component="div" className="form-text text-danger" />
  267. {/* </div> */}
  268. </FormGroup>
  269. <FormGroup>
  270. <div className="btn-simpanpenjadwalan">
  271. <Button className= "btn-colorpenjadwalan" color= "info" block type="submit" disabled={laporan.data?.evaluasi.length}>
  272. <h4 className="text-btn-penjadwalan-1">Simpan</h4>
  273. </Button>
  274. </div>
  275. </FormGroup>
  276. </Form>
  277. )}
  278. </Formik>
  279. </CardBody>
  280. </Card>
  281. </>
  282. )}
  283. <div className="mb-3">
  284. {selectedEvent && (
  285. <div>
  286. <p>Selected:</p>
  287. <div className="box-placeholder">{JSON.stringify(selectedEvent)}</div>
  288. </div>
  289. )}
  290. {!selectedEvent && (
  291. <div>
  292. <p>Click calendar to show information</p>
  293. </div>
  294. )}
  295. </div>
  296. </div>
  297. </div>
  298. </div>
  299. <div className="col-xl-8 col-lg-7">
  300. <Card className="card-default">
  301. <CardBody>
  302. {/* START calendar */}
  303. <FullCalendar
  304. defaultView={this.dayGridMonth}
  305. plugins={this.calendarPlugins}
  306. events={this.state.dataEvent}
  307. themeSystem={"bootstrap"}
  308. header={this.calendarHeader}
  309. deepChangeDetection={true}
  310. eventClick={this.eventClick}
  311. ></FullCalendar>
  312. </CardBody>
  313. </Card>
  314. </div>
  315. </div>
  316. </div>
  317. </ContentWrapper>
  318. );
  319. }
  320. }
  321. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  322. export default connect(mapStateToProps)(Calendar);