guild_front/src/pages/ViewReport/ViewReport.jsx

226 lines
7.5 KiB
React
Raw Normal View History

2023-05-11 19:54:15 +03:00
import React, { useEffect, useState } from "react";
2023-05-18 17:42:29 +03:00
import { Link, Navigate, useParams } from "react-router-dom";
2023-05-11 19:54:15 +03:00
import { Loader } from "../../components/Loader/Loader";
import { ProfileHeader } from "../../components/ProfileHeader/ProfileHeader";
import { ProfileBreadcrumbs } from "../../components/ProfileBreadcrumbs/ProfileBreadcrumbs";
import { Footer } from "../../components/Footer/Footer";
2023-05-18 18:21:20 +03:00
import { Navigation } from "../../components/Navigation/Navigation";
2023-01-23 14:59:57 +03:00
2023-05-11 19:54:15 +03:00
import { apiRequest } from "../../api/request";
import {
getCorrectDate,
getCreatedDate,
hourOfNum,
} from "../../components/Calendar/calendarHelper";
2023-01-23 14:59:57 +03:00
2023-05-25 16:42:37 +03:00
import arrow from "../../assets/icons/arrows/left-arrow.png";
import arrowSwitchDate from "../../assets/icons/arrows/arrowViewReport.png";
2023-04-18 13:58:36 +03:00
2023-05-11 19:54:15 +03:00
import "./viewReport.scss";
2023-01-23 14:59:57 +03:00
export const ViewReport = () => {
2023-05-11 19:54:15 +03:00
if (localStorage.getItem("role_status") === "18") {
return <Navigate to="/profile" replace />;
}
2023-05-18 17:42:29 +03:00
const dateReport = useParams();
2023-05-18 18:21:20 +03:00
const [previousReportDay] = useState(new Date(dateReport.id));
const [nextReportDay] = useState(new Date(dateReport.id));
2023-01-23 14:59:57 +03:00
2023-05-11 19:54:15 +03:00
const [taskText, setTaskText] = useState([]);
const [difficulties, setDifficulties] = useState([]);
const [tomorrowTask, setTomorrowTask] = useState([]);
const [totalHours, setTotalHours] = useState(0);
const [currentDay] = useState(new Date());
const [loader, setLoader] = useState(false);
2023-01-23 23:51:18 +03:00
2023-05-11 19:54:15 +03:00
function getReportFromDate(day) {
setLoader(true);
setTaskText([]);
setDifficulties([]);
setTomorrowTask([]);
apiRequest(
`reports/find-by-date?user_card_id=${localStorage.getItem(
"cardId"
)}&date=${day}`
).then((res) => {
let spendTime = 0;
for (const item of res) {
if (item.difficulties) {
setDifficulties((prevArray) => [...prevArray, item.difficulties]);
}
if (item.tomorrow) {
setTomorrowTask((prevArray) => [...prevArray, item.tomorrow]);
}
item.task.map((task) => {
const taskInfo = {
hours: task.hours_spent,
task: task.task,
id: task.id,
};
if (task.hours_spent) {
spendTime += Number(task.hours_spent);
}
setTaskText((prevArray) => [...prevArray, taskInfo]);
});
}
setTotalHours(spendTime);
setLoader(false);
});
2023-05-18 18:21:20 +03:00
previousReportDay.setDate(previousReportDay.getDate() - 1);
nextReportDay.setDate(nextReportDay.getDate() + 1);
2023-05-11 19:54:15 +03:00
}
2023-01-23 23:51:18 +03:00
2023-05-11 19:54:15 +03:00
function nextDay() {
2023-05-18 18:21:20 +03:00
getReportFromDate(getCreatedDate(nextReportDay));
previousReportDay.setDate(previousReportDay.getDate() + 2);
2023-05-11 19:54:15 +03:00
}
2023-01-23 23:51:18 +03:00
2023-05-11 19:54:15 +03:00
function previousDay() {
2023-05-18 18:21:20 +03:00
getReportFromDate(getCreatedDate(previousReportDay));
nextReportDay.setDate(nextReportDay.getDate() - 2);
2023-05-11 19:54:15 +03:00
}
2023-01-23 23:51:18 +03:00
2023-05-11 19:54:15 +03:00
useEffect(() => {
2023-05-18 17:42:29 +03:00
getReportFromDate(dateReport.id);
2023-05-11 19:54:15 +03:00
}, []);
2023-05-18 17:42:29 +03:00
2023-05-11 19:54:15 +03:00
return (
<div className="viewReport">
<ProfileHeader />
<Navigation />
<div className="container">
<div className="viewReport__info">
<ProfileBreadcrumbs
links={[
{ name: "Главная", link: "/profile" },
{ name: "Ваша отчетность", link: "/profile/calendar" },
{ name: "Просмотр отчета за день", link: "/profile/view" },
]}
/>
<h2 className="viewReport__title">
Ваши отчеты - <span>просмотр отчета за день</span>
</h2>
<Link className="viewReport__back" to={`/profile/calendar`}>
2023-05-24 19:23:24 +03:00
<img src={arrow} alt="#" />
2023-05-11 19:54:15 +03:00
<p>Вернуться</p>
</Link>
<div className="viewReport__bar">
<h3 className="viewReport__bar__date">
2023-05-18 17:42:29 +03:00
{getCorrectDate(dateReport.id)}
2023-05-11 19:54:15 +03:00
</h3>
<p className="viewReport__bar__hours">
Вами потрачено на работу :{" "}
<span>
{totalHours} {hourOfNum(totalHours)}
</span>
</p>
</div>
</div>
<div className="viewReport__switchDate">
<div
2023-05-18 17:42:29 +03:00
onClick={() => {
previousDay();
}}
2023-05-11 19:54:15 +03:00
>
2023-05-18 18:21:20 +03:00
<Link to={`../view/${getCreatedDate(previousReportDay)}`}>
2023-05-18 17:42:29 +03:00
<div className="viewReport__switchDate__prev switchDate">
<img src={arrowSwitchDate} alt="arrow" />
</div>
</Link>
2023-05-11 19:54:15 +03:00
</div>
2023-05-18 17:42:29 +03:00
<p>{getCorrectDate(dateReport.id)}</p>
2023-05-18 18:38:02 +03:00
<div
onClick={() => nextDay()}
className={`${
getCreatedDate(currentDay) === dateReport.id ? "disable" : ""
}`}
>
2023-05-18 18:21:20 +03:00
<Link to={`../view/${getCreatedDate(nextReportDay)}`}>
2023-05-18 18:38:02 +03:00
<div className={`viewReport__switchDate__next switchDate`}>
2023-05-18 17:42:29 +03:00
<img src={arrowSwitchDate} alt="arrow" />
</div>
</Link>
2023-05-11 19:54:15 +03:00
</div>
</div>
{loader && <Loader width={75} height={75} />}
{Boolean(taskText.length) && (
<div className="viewReport__content">
<div className="table__container">
<table className="viewReport__done">
<thead>
<tr>
<th>
<p>Какие задачи были выполнены?</p>
</th>
<th>
<p>Время</p>
</th>
</tr>
</thead>
<tbody>
{taskText.length &&
taskText.map((task, index) => {
return (
<tr key={task.id}>
<td>
<p>
{index + 1}. {task.task}
</p>
</td>
<td>
<div className="viewReport__done__hours__item">
<span>{task.hours}</span>
<p className="hours">
{hourOfNum(task.hours)} на задачу
</p>
2023-01-23 23:51:18 +03:00
</div>
2023-05-11 19:54:15 +03:00
</td>
</tr>
);
})}
<tr>
<td></td>
<td>
<span>
Всего: {totalHours} {hourOfNum(totalHours)}
</span>
</td>
</tr>
</tbody>
</table>
2023-01-23 14:59:57 +03:00
</div>
2023-05-11 19:54:15 +03:00
{Boolean(difficulties.length) && (
<div className="viewReport__item">
<h3>Какие сложности возникли?</h3>
{difficulties.map((item, index) => {
return <p key={index}>{item}</p>;
})}
</div>
)}
{Boolean(tomorrowTask.length) && (
<div className="viewReport__item">
<h3>Что планируется сделать завтра?</h3>
{tomorrowTask.map((item, index) => {
return <p key={index}>{item}</p>;
})}
</div>
)}
</div>
)}
{!Boolean(taskText.length) && !loader && (
<div className="viewReport__noTask">
<p>
В этот день вы <span>не заполняли</span> отчет
</p>
</div>
)}
<Footer />
</div>
</div>
);
2023-01-23 14:59:57 +03:00
};