calendar.view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 events from "./calendar.events";
  15. import Select from "react-select";
  16. import moment from "moment";
  17. import { connect } from "react-redux";
  18. import Loader from "@/components/Common/Loader";
  19. import Router from "next/router";
  20. import { ToastContainer, toast } from "react-toastify";
  21. import { Formik, Form, Field, ErrorMessage } from "formik";
  22. import * as Yup from "yup";
  23. import Datetime from "react-datetime";
  24. const status = [
  25. { value: "Ditindaklanjuti DIKTI", label: "Ditindaklanjuti DIKTI", className: "State-ACT" },
  26. { value: "Delegasi ke LLDIKTI", label: "Delegasi ke LLDIKTI", className: "State-ACT" },
  27. { value: "Ditutup", label: "Ditutup", className: "State-ACT" },
  28. ];
  29. const statusLLDIKTI = [
  30. { value: "Ditindaklanjuti LLDIKTI", label: "Ditindaklanjuti LLDIKTI", className: "State-ACT" },
  31. { value: "Delegasi ke DIKTI", label: "Delegasi ke DIKTI", className: "State-ACT" },
  32. { value: "Ditutup", label: "Ditutup", className: "State-ACT" },
  33. ];
  34. const jadwalSchema = Yup.object().shape({
  35. judul: Yup.string().required("Required"),
  36. dari_tanggal: Yup.date().required("Required"),
  37. sampai_tanggal: Yup.date().required("Required"),
  38. });
  39. class Calendar extends Component {
  40. calendarPlugins = [interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin, bootstrapPlugin];
  41. calendarHeader = {
  42. left: "prev,next today",
  43. center: "title",
  44. right: "dayGridMonth,timeGridWeek,timeGridDay",
  45. };
  46. constructor(props) {
  47. super(props);
  48. this.state = {
  49. selectedEvent: null,
  50. evRemoveOnDrop: true,
  51. evNewName: "",
  52. externalEvents: [],
  53. dataLaporan: [],
  54. dataEvent: [],
  55. laporan: {},
  56. selectedOption: null,
  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({ externalEvents: [{ id: query.id, color, name: `Jadwal Pemeriksaan - No.Laporan : ${laporan.data.no_laporan} - ${laporan.data.pt.nama}`, allDay: true }] });
  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 { laporan } = this.state;
  107. const warna = "#" + Math.floor(Math.random() * 16777215).toString(16);
  108. const d = {
  109. judul: data.judul,
  110. dari_tanggal: data.dari_tanggal,
  111. sampai_tanggal: data.sampai_tanggal,
  112. warna,
  113. };
  114. if (laporan.data.jadwal) {
  115. d.warna = laporan.data.jadwal.warna;
  116. }
  117. await toast.promise(updateJadwal(token, id, d), {
  118. pending: "Loading",
  119. success: "Success",
  120. error: "Error",
  121. });
  122. const dataLaporan = await getPelaporan(token, { jadwal: true });
  123. this.setState({ dataLaporan });
  124. this.getDataEvent();
  125. };
  126. defaultStatus = async () => {
  127. const status = this.getStatus();
  128. return this.setState({ selectedOption: status[0] });
  129. };
  130. handleChangeSelect = (selectedOption) => this.setState({ selectedOption });
  131. handleSimpan = async (e) => {
  132. const toastid = toast.loading("Please wait...");
  133. try {
  134. const { selectedOption } = this.state;
  135. const { user, token, query } = this.props;
  136. const { id } = query;
  137. this.setState({ selectedOption });
  138. if (selectedOption.value === this.getStatus()[1].value) {
  139. await updateLaporan(token, id, { role_data: user.role.id === 2020 ? "lldikti" : "dikti" });
  140. Router.push("/app/penjadwalan");
  141. }
  142. if (selectedOption.value === this.getStatus()[2].value) {
  143. await updateLaporan(token, id, { aktif: "false" });
  144. Router.push("/app/penjadwalan");
  145. }
  146. toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
  147. } catch (error) {
  148. toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
  149. }
  150. };
  151. render() {
  152. const { externalEvents, laporan, selectedOption, selectedEvent } = this.state;
  153. console.log(this.state.dataLaporan);
  154. return (
  155. <ContentWrapper>
  156. <div className="content-heading">
  157. <div>Jadwal Pemeriksaan</div>
  158. <div className="ml-auto">
  159. <Link href="/app/penjadwalan">
  160. <button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
  161. </Link>
  162. </div>
  163. </div>
  164. <div className="calendar-app">
  165. {laporan.data ? (
  166. <Row>
  167. <Col>
  168. <Card className="card-default">
  169. <CardBody>
  170. <DetailLaporan noStatus data={laporan.data} />
  171. </CardBody>
  172. </Card>
  173. </Col>
  174. </Row>
  175. ) : (
  176. <Row className="mb-4">
  177. <Col>
  178. <Loader />
  179. </Col>
  180. </Row>
  181. )}
  182. <div className="row">
  183. <div className="col-xl-4 col-lg-5">
  184. <div className="row">
  185. <div className="col-lg-12 col-md-6 col-12">
  186. <Card className="card-default">
  187. <CardHeader>
  188. <CardTitle tag="h4">Status Pelaporan</CardTitle>
  189. </CardHeader>
  190. <CardBody>
  191. <Select value={selectedOption} onChange={this.handleChangeSelect} options={this.getStatus()} required />
  192. <Button onClick={this.handleSimpan} className="mt-2" color="primary" block>
  193. Simpan
  194. </Button>
  195. </CardBody>
  196. </Card>
  197. {selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? (
  198. ""
  199. ) : (
  200. <>
  201. <Card className="card-default" title="">
  202. <CardHeader>
  203. <CardTitle tag="h4">Input Jadwal</CardTitle>
  204. </CardHeader>
  205. <CardBody>
  206. <Formik
  207. enableReinitialize={true}
  208. initialValues={{
  209. judul: laporan.data?.jadwal?.judul || "",
  210. dari_tanggal: moment(laporan.data?.jadwal?.dari_tanggal) || "",
  211. sampai_tanggal: moment(laporan.data?.jadwal?.sampai_tanggal) || "",
  212. }}
  213. validationSchema={jadwalSchema}
  214. onSubmit={this.handleEventCalendar}
  215. >
  216. {() => (
  217. <Form>
  218. <FormGroup>
  219. <label className="col-form-label">Judul</label>
  220. <Field name="judul">{({ field, form }) => <Input type="text" placeholder="judul" {...field} />}</Field>
  221. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  222. </FormGroup>
  223. <FormGroup>
  224. <label className="col-form-label">Dari Tanggal</label>
  225. <Field name="dari_tanggal">
  226. {({ field, form }) => (
  227. <Datetime
  228. timeFormat={false}
  229. inputProps={{ className: "form-control" }}
  230. value={field.value}
  231. onChange={(e) => {
  232. form.setFieldValue(field.name, e);
  233. }}
  234. />
  235. )}
  236. </Field>
  237. <ErrorMessage name="dari_tanggal" component="div" className="form-text text-danger" />
  238. </FormGroup>
  239. <FormGroup>
  240. <label className="col-form-label">Sampai Tanggal</label>
  241. <Field name="sampai_tanggal">
  242. {({ field, form }) => (
  243. <Datetime
  244. timeFormat={false}
  245. inputProps={{ className: "form-control" }}
  246. value={field.value}
  247. onChange={(e) => {
  248. form.setFieldValue(field.name, e);
  249. }}
  250. />
  251. )}
  252. </Field>
  253. <ErrorMessage name="sampai_tanggal" component="div" className="form-text text-danger" />
  254. </FormGroup>
  255. <FormGroup row>
  256. <div className="col-xl-12">
  257. <Button color="primary" block type="submit">
  258. Simpan Jadwal
  259. </Button>
  260. </div>
  261. </FormGroup>
  262. </Form>
  263. )}
  264. </Formik>
  265. </CardBody>
  266. </Card>
  267. </>
  268. )}
  269. <div className="mb-3">
  270. {selectedEvent && (
  271. <div>
  272. <p>Selected:</p>
  273. <div className="box-placeholder">{JSON.stringify(selectedEvent)}</div>
  274. </div>
  275. )}
  276. {!selectedEvent && (
  277. <div>
  278. <p>Click calendar to show information</p>
  279. </div>
  280. )}
  281. </div>
  282. </div>
  283. </div>
  284. </div>
  285. <div className="col-xl-8 col-lg-7">
  286. <Card className="card-default">
  287. <CardBody>
  288. {/* START calendar */}
  289. <FullCalendar
  290. defaultView={this.dayGridMonth}
  291. plugins={this.calendarPlugins}
  292. events={this.state.dataEvent}
  293. themeSystem={"bootstrap"}
  294. header={this.calendarHeader}
  295. deepChangeDetection={true}
  296. eventClick={this.eventClick}
  297. ></FullCalendar>
  298. </CardBody>
  299. </Card>
  300. </div>
  301. </div>
  302. </div>
  303. </ContentWrapper>
  304. );
  305. }
  306. }
  307. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  308. export default connect(mapStateToProps)(Calendar);