calendar.view.js 11 KB

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