guild_front/src/components/ProfileCalendar/ProfileCalendarComponent.js

182 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-05-11 19:54:15 +03:00
import React, { useState, useEffect } from "react";
import arrow from "../../images/arrowCalendar.png";
import rectangle from "../../images/rectangle__calendar.png";
import calendarIcon from "../../images/calendar_icon.png";
import moment from "moment";
import {
calendarHelper,
currentMonthAndDay,
getReports,
hourOfNum,
} from "../Calendar/calendarHelper";
import {
setReportDate,
setRequestDate,
setSendRequest,
} from "../../redux/reportSlice";
import { useDispatch } from "react-redux";
import { Link } from "react-router-dom";
import ShortReport from "../ShortReport/ShortReport";
import "moment/locale/ru";
import "./../Calendar/calendarComponent.scss";
export const ProfileCalendarComponent = React.memo(
({ value, setValueHandler, reports, totalHours }) => {
2022-12-26 15:12:01 +03:00
const dispatch = useDispatch();
2023-05-11 19:54:15 +03:00
const [currentDay] = useState(moment());
const [calendar, setCalendar] = useState([]);
const [month, setMonth] = useState("");
const [shortReport, setShortReport] = useState(false);
const [dayTest, setDayTest] = useState("");
2022-12-26 15:12:01 +03:00
useEffect(() => {
2023-05-11 19:54:15 +03:00
setCalendar(calendarHelper(value));
}, [value]);
2022-12-26 15:12:01 +03:00
2023-02-17 15:19:49 +03:00
useEffect(() => {
2023-05-11 19:54:15 +03:00
setMonth(value.format("MMMM"));
2023-02-17 15:19:49 +03:00
}, [month]);
2022-12-26 15:12:01 +03:00
function isToday(day) {
2023-05-11 19:54:15 +03:00
return day.isSame(new Date(), "day");
2022-12-26 15:12:01 +03:00
}
2023-01-16 16:28:56 +03:00
function correctDay(day) {
2023-05-11 19:54:15 +03:00
if (day < 10) {
return `0${day}`;
}
return day;
2023-01-16 16:28:56 +03:00
}
2022-12-26 15:12:01 +03:00
function dayStyles(day) {
2023-05-11 19:54:15 +03:00
if (currentDay < day) return `block`;
for (const date of reports) {
if (
`${new Date(day).getFullYear()}-${correctDay(
new Date(day).getMonth() + 1
)}-${correctDay(new Date(day).getDate())}` === date.created_at
) {
return `before`;
2022-12-26 15:12:01 +03:00
}
2023-05-11 19:54:15 +03:00
}
if (day.day() === 6 || day.day() === 0) return `selected`;
if (isToday(day)) return `today`;
return "pass";
2022-12-26 15:12:01 +03:00
}
2023-01-16 16:28:56 +03:00
function correctRoute(day) {
2023-05-11 19:54:15 +03:00
for (const date of reports) {
if (
`${new Date(day).getFullYear()}-${correctDay(
new Date(day).getMonth() + 1
)}-${correctDay(new Date(day).getDate())}` === date.created_at
) {
return `../view/${day}`;
2023-01-16 16:28:56 +03:00
}
2023-05-11 19:54:15 +03:00
}
return "../../report";
2023-01-16 16:28:56 +03:00
}
2023-02-09 20:46:02 +03:00
function prevMonth() {
2023-05-11 19:54:15 +03:00
return value.clone().subtract(1, "month");
2023-02-09 20:46:02 +03:00
}
function nextMonth() {
2023-05-11 19:54:15 +03:00
return value.clone().add(1, "month");
2023-02-09 20:46:02 +03:00
}
2022-12-26 15:12:01 +03:00
return (
2023-05-11 19:54:15 +03:00
<div className="calendar-component">
<div className="calendar-component__header">
<div className="calendar-component__header-info">
<h3>Мои отчеты:</h3>
<p className="calendar__hours">
{month}&nbsp;
<span>
{totalHours} {hourOfNum(totalHours)}{" "}
</span>
</p>
</div>
<div className="calendar-component__header-switcher">
<div
className="calendar-component__header-box"
onClick={() => {
setValueHandler(prevMonth());
dispatch(setRequestDate(getReports(prevMonth())));
}}
>
<img src={arrow} alt="" />
<span>{prevMonth().format("MMMM")}</span>
2022-12-26 15:12:01 +03:00
</div>
2023-05-11 19:54:15 +03:00
<div className="calendar-component__header-box">
<span>{value.format("YYYY")}</span>
2022-12-26 15:12:01 +03:00
</div>
2023-05-11 19:54:15 +03:00
<div
className="calendar-component__header-box"
onClick={() => {
setValueHandler(nextMonth());
dispatch(setRequestDate(getReports(nextMonth())));
}}
>
<span>{nextMonth().format("MMMM")}</span>
<img src={arrow} alt="" />
2022-12-26 15:12:01 +03:00
</div>
2023-05-11 19:54:15 +03:00
</div>
</div>
<div className="calendar-component__rectangle">
<img src={rectangle} alt="" />
2022-12-26 15:12:01 +03:00
</div>
2023-05-11 19:54:15 +03:00
<div className="calendar-component__body">
<div>
<p>Пн</p>
<p>Вт</p>
<p>Ср</p>
<p>Чт</p>
<p>Пт</p>
<p>Сб</p>
<p>Вс</p>
</div>
<div className="calendar-component__form">
{calendar.map((week) =>
week.map((day) => (
<button
onClick={() => {
dispatch(setReportDate(day));
dispatch(setSendRequest(true));
setShortReport(true);
setDayTest(day);
}}
key={day}
className={dayStyles(day)}
name={day.format("dddd")}
id="btn"
>
{/* <Link to={correctRoute(day)}>
<img
className={"calendar__icon"}
src={calendarIcon}
alt=""
/>
{currentMonthAndDay(day)}
</Link> */}
<img className={"calendar__icon"} src={calendarIcon} alt="" />
{currentMonthAndDay(day)}
</button>
))
)}
</div>
</div>
<Link to={correctRoute(dayTest)}>
<button className="calendar__btn">Посмотреть подробнее</button>
</Link>
{shortReport && <ShortReport />}
</div>
);
}
);