calendar.view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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>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. <CardHeader>
  184. <CardTitle tag="h4">Status Pelaporan</CardTitle>
  185. </CardHeader>
  186. <CardBody>
  187. <Select value={selectedOption} onChange={this.handleChangeSelect} options={this.getStatus()} required />
  188. <Button onClick={this.handleSimpan} className="mt-2" color="primary" block disabled={laporan.data?.evaluasi.length}>
  189. Simpan
  190. </Button>
  191. </CardBody>
  192. </Card>
  193. {selectedOption?.value === this.getStatus()[2].value || selectedOption?.value === this.getStatus()[1].value ? (
  194. ""
  195. ) : (
  196. <>
  197. <Card className="card-default" title="">
  198. <CardHeader>
  199. <CardTitle tag="h4">Input Jadwal</CardTitle>
  200. </CardHeader>
  201. <CardBody>
  202. <Formik
  203. enableReinitialize={true}
  204. initialValues={{
  205. judul: laporan.data?.jadwal?.judul ? laporan.data.jadwal.judul.split("- ")[1] : "",
  206. dari_tanggal: laporan.data?.jadwal?.dari_tanggal ? moment(laporan.data.jadwal.dari_tanggal) : "",
  207. sampai_tanggal: laporan.data?.jadwal?.sampai_tanggal ? moment(laporan.data.jadwal.sampai_tanggal) : "",
  208. }}
  209. validationSchema={jadwalSchema}
  210. onSubmit={this.handleEventCalendar}
  211. >
  212. {() => (
  213. <Form>
  214. <FormGroup>
  215. <label className="col-form-label">Warna</label>
  216. <div className="badge d-block" style={{ backgroundColor: this.state.color, color: "white" }}>
  217. {this.state.color}
  218. </div>
  219. </FormGroup>
  220. <FormGroup>
  221. <label className="col-form-label">Judul</label>
  222. <Field name="judul">{({ field, form }) => <Input type="text" placeholder="judul" {...field} />}</Field>
  223. <ErrorMessage name="judul" component="div" className="form-text text-danger" />
  224. </FormGroup>
  225. <FormGroup>
  226. <label className="col-form-label">Dari Tanggal</label>
  227. <Field name="dari_tanggal">
  228. {({ field, form }) => (
  229. <Datetime
  230. timeFormat={false}
  231. inputProps={{ className: "form-control" }}
  232. value={field.value}
  233. onChange={(e) => {
  234. form.setFieldValue(field.name, e);
  235. }}
  236. />
  237. )}
  238. </Field>
  239. <ErrorMessage name="dari_tanggal" component="div" className="form-text text-danger" />
  240. </FormGroup>
  241. <FormGroup>
  242. <label className="col-form-label">Sampai Tanggal</label>
  243. <Field name="sampai_tanggal">
  244. {({ field, form }) => (
  245. <Datetime
  246. timeFormat={false}
  247. inputProps={{ className: "form-control" }}
  248. value={field.value}
  249. onChange={(e) => {
  250. form.setFieldValue(field.name, e);
  251. }}
  252. />
  253. )}
  254. </Field>
  255. <ErrorMessage name="sampai_tanggal" component="div" className="form-text text-danger" />
  256. </FormGroup>
  257. <FormGroup row>
  258. <div className="col-xl-12">
  259. <Button color="primary" block type="submit" disabled={laporan.data?.evaluasi.length}>
  260. Simpan Jadwal
  261. </Button>
  262. </div>
  263. </FormGroup>
  264. </Form>
  265. )}
  266. </Formik>
  267. </CardBody>
  268. </Card>
  269. </>
  270. )}
  271. <div className="mb-3">
  272. {selectedEvent && (
  273. <div>
  274. <p>Selected:</p>
  275. <div className="box-placeholder">{JSON.stringify(selectedEvent)}</div>
  276. </div>
  277. )}
  278. {!selectedEvent && (
  279. <div>
  280. <p>Click calendar to show information</p>
  281. </div>
  282. )}
  283. </div>
  284. </div>
  285. </div>
  286. </div>
  287. <div className="col-xl-8 col-lg-7">
  288. <Card className="card-default">
  289. <CardBody>
  290. {/* START calendar */}
  291. <FullCalendar
  292. defaultView={this.dayGridMonth}
  293. plugins={this.calendarPlugins}
  294. events={this.state.dataEvent}
  295. themeSystem={"bootstrap"}
  296. header={this.calendarHeader}
  297. deepChangeDetection={true}
  298. eventClick={this.eventClick}
  299. ></FullCalendar>
  300. </CardBody>
  301. </Card>
  302. </div>
  303. </div>
  304. </div>
  305. </ContentWrapper>
  306. );
  307. }
  308. }
  309. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  310. export default connect(mapStateToProps)(Calendar);