Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SchedulePage.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React, { useState, useEffect } from "react";
  2. import DayComponent from "../../components/Schedules/DayComponent";
  3. import arrowRight from "../../assets/images/arrow_right.png";
  4. import arrowLeft from "../../assets/images/arrow_left2.png";
  5. import DayDetailsComponent from "../../components/Schedules/DayDetailsComponent";
  6. import { useDispatch, useSelector } from "react-redux";
  7. import { selectSchedule } from "../../store/selectors/scheduleSelectors";
  8. import { fetchSchedule } from "../../store/actions/schedule/scheduleActions";
  9. import {
  10. getMonthString,
  11. getDayString,
  12. getFormatedDayOrMonth,
  13. } from "../../util/helpers/dateHelpers";
  14. import PropTypes from "prop-types";
  15. const SchedulePage = ({ history }) => {
  16. const dispatch = useDispatch();
  17. const selectionProcesses = useSelector(selectSchedule);
  18. const [detailDialogShown, setDetailsDialogShown] = useState(false);
  19. const [currentMonth, setCurrentMonth] = useState(new Date().getMonth());
  20. const [currentYear, setCurrentYear] = useState(new Date().getFullYear());
  21. const [numberOfDaysInMonth, setNumberOfDaysInMonth] = useState(
  22. new Date(currentYear, currentMonth + 1, 0).getDate()
  23. );
  24. const [currentlySelectedDate, setCurrentlySelectedDate] = useState("");
  25. const [currentlySelectedDay, setCurrentlySelectedDay] = useState(0);
  26. useEffect(() => {
  27. dispatch(fetchSchedule({ month: currentMonth + 1, year: currentYear }));
  28. }, [dispatch]);
  29. // array will contain number of days of current month
  30. const makeArray = () => {
  31. let count = 0;
  32. let arr = [];
  33. while (count !== numberOfDaysInMonth) {
  34. arr.push(count);
  35. count += 1;
  36. }
  37. return arr;
  38. };
  39. const handleCloseDialog = () => {
  40. setDetailsDialogShown(false);
  41. };
  42. const handleOpenDialog = (numberOfDay) => {
  43. setCurrentlySelectedDay(numberOfDay);
  44. setCurrentlySelectedDate(
  45. getFormatedDayOrMonth(numberOfDay) +
  46. "." +
  47. getFormatedDayOrMonth(currentMonth + 1) +
  48. "." +
  49. currentYear
  50. );
  51. setDetailsDialogShown(true);
  52. };
  53. const getSelectionProcessesForSpecificDay = (day) => {
  54. return (
  55. selectionProcesses &&
  56. selectionProcesses.filter(
  57. (k) =>
  58. new Date(k.date).getDate() === day &&
  59. new Date(k.date).getFullYear() === currentYear &&
  60. new Date(k.date).getMonth() === currentMonth
  61. )
  62. );
  63. };
  64. const goBackOneMonth = () => {
  65. dispatch(
  66. fetchSchedule({
  67. month: currentMonth - 1 === -1 ? 12 : currentMonth,
  68. year: currentMonth - 1 === -1 ? currentYear - 1 : currentYear,
  69. })
  70. );
  71. setNumberOfDaysInMonth(
  72. new Date(
  73. currentMonth - 1 === -1 ? currentYear - 1 : currentYear,
  74. currentMonth - 1 === -1 ? 12 : currentMonth,
  75. 0
  76. ).getDate()
  77. );
  78. if (currentMonth - 1 === -1) {
  79. setCurrentMonth(11);
  80. setCurrentYear(currentYear - 1);
  81. } else {
  82. setCurrentMonth(currentMonth - 1);
  83. }
  84. };
  85. const goForwardOneMonth = () => {
  86. // console.log(currentMonth)
  87. dispatch(
  88. fetchSchedule({
  89. month: currentMonth - 1 === -1 ? 12 : currentMonth + 2,
  90. year: currentMonth - 1 === -1 ? currentYear - 1 : currentYear,
  91. })
  92. );
  93. setNumberOfDaysInMonth(
  94. new Date(
  95. currentMonth + 1 === 12 ? currentYear + 1 : currentYear,
  96. currentMonth + 1 === 12 ? 1 : currentMonth + 2,
  97. 0
  98. ).getDate()
  99. );
  100. if (currentMonth + 1 === 12) {
  101. setCurrentMonth(0);
  102. setCurrentYear(currentYear + 1);
  103. } else {
  104. setCurrentMonth(currentMonth + 1);
  105. }
  106. };
  107. return (
  108. <div className="schedule-page-container pl-144 pt-36px">
  109. <DayDetailsComponent
  110. onClose={handleCloseDialog}
  111. open={detailDialogShown}
  112. selectedDate={currentlySelectedDate}
  113. selectionProcesses={getSelectionProcessesForSpecificDay(
  114. currentlySelectedDay
  115. )}
  116. numberOfDaysInMonth={numberOfDaysInMonth}
  117. setCurrentlySelected={setCurrentlySelectedDate}
  118. setCurrentlySelectedDay={setCurrentlySelectedDay}
  119. currentlySelectedDay={currentlySelectedDay}
  120. history={history}
  121. />
  122. <div>
  123. <p className="schedule-page-main-header">Planer aktivnosti</p>
  124. <p className="schedule-page-header">
  125. | {getMonthString(currentMonth)} {currentYear}
  126. </p>
  127. </div>
  128. <div className="schedule-page-arrows-container">
  129. <div className="schedule-page-arrow-container" onClick={goBackOneMonth}>
  130. <img src={arrowLeft} />
  131. </div>
  132. <div
  133. className="schedule-page-arrow-container"
  134. onClick={goForwardOneMonth}
  135. >
  136. <img src={arrowRight} />
  137. </div>
  138. </div>
  139. <div className="schedule-page-content-container">
  140. {makeArray().map((numberOfDay, index) => (
  141. <DayComponent
  142. key={index}
  143. numberOfDay={numberOfDay + 1}
  144. nameOfDay={getDayString(numberOfDay % 7)
  145. .toLowerCase()
  146. .slice(0, 3)}
  147. interviews={getSelectionProcessesForSpecificDay(numberOfDay + 1)}
  148. onClick={() => handleOpenDialog(numberOfDay + 1)}
  149. />
  150. ))}
  151. </div>
  152. </div>
  153. );
  154. };
  155. SchedulePage.propTypes = {
  156. history: PropTypes.shape({
  157. replace: PropTypes.func,
  158. push: PropTypes.func,
  159. location: PropTypes.shape({
  160. pathname: PropTypes.string,
  161. }),
  162. }),
  163. };
  164. export default SchedulePage;