guild_front/src/pages/ViewReport/ViewReport.jsx

261 lines
8.6 KiB
React
Raw Normal View History

2023-05-11 19:54:15 +03:00
import React, { useEffect, useState } from "react";
import { Link, useParams } from "react-router-dom";
2023-05-30 10:54:47 +03:00
import { apiRequest } from "@api/request";
2023-05-31 08:36:15 +03:00
2023-05-11 19:54:15 +03:00
import {
getCorrectDate,
getCreatedDate,
2023-12-19 17:36:30 +03:00
hourOfNum
2023-05-30 10:54:47 +03:00
} from "@components/Calendar/calendarHelper";
2023-05-31 08:36:15 +03:00
import { Footer } from "@components/Common/Footer/Footer";
import { Loader } from "@components/Common/Loader/Loader";
import { Navigation } from "@components/Navigation/Navigation";
import { ProfileBreadcrumbs } from "@components/ProfileBreadcrumbs/ProfileBreadcrumbs";
import { ProfileHeader } from "@components/ProfileHeader/ProfileHeader";
2023-01-23 14:59:57 +03:00
2023-05-30 10:54:47 +03:00
import arrowSwitchDate from "assets/icons/arrows/arrowViewReport.png";
2023-05-31 08:36:15 +03:00
import arrow from "assets/icons/arrows/left-arrow.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 = () => {
2024-02-02 18:43:52 +03:00
const params = useParams();
const [previousReportDay] = useState(new Date(params.date));
const [nextReportDay] = useState(new Date(params.date));
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 [taskId, setTaskId] = useState("");
2023-05-11 19:54:15 +03:00
const [totalHours, setTotalHours] = useState(0);
const [currentDay] = useState(new Date());
const [loader, setLoader] = useState(false);
const [deleteLoader, setDeleteLoader] = 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([]);
2024-02-02 18:44:11 +03:00
apiRequest(`reports/find-by-date?user_id=${params.id}&date=${day}`).then(
(res) => {
let spendTime = 0;
setTaskId(res[0]?.id);
2024-02-02 18:44:11 +03:00
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
};
spendTime += Math.floor(task.hours_spent);
2023-12-22 15:18:13 +03:00
2024-02-02 18:44:11 +03:00
setTaskText((prevArray) => [...prevArray, taskInfo]);
});
}
setTotalHours(Math.floor(spendTime));
setLoader(false);
2023-05-11 19:54:15 +03:00
}
2024-02-02 18:44:11 +03:00
);
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
function deleteReport() {
setDeleteLoader(true);
apiRequest(`reports/delete?id=${taskId}`, {
method: "DELETE"
}).then((res) => {
setDeleteLoader(false);
if (res) {
window.location.replace("/profile/calendar");
}
});
}
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(() => {
2024-02-02 18:43:52 +03:00
getReportFromDate(params.date);
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="view-report">
2023-05-11 19:54:15 +03:00
<ProfileHeader />
<Navigation />
<div className="container">
<div className="view-report__info">
2024-02-02 18:44:11 +03:00
{localStorage.getItem("role_status") !== "18" && (
2024-02-02 18:43:52 +03:00
<>
<ProfileBreadcrumbs
links={[
{ name: "Главная", link: "/profile" },
{ name: "Ваша отчетность", link: "/profile/calendar" },
{ name: "Просмотр отчета за день", link: "/profile/view" }
]}
/>
<h2 className="view-report__title">
2024-02-02 18:44:11 +03:00
Ваши отчеты - <span>просмотр отчета за день</span>
2024-02-02 18:43:52 +03:00
</h2>
</>
2024-02-02 18:44:11 +03:00
)}
<Link
className="view-report__back"
2024-02-02 18:44:11 +03:00
to={
localStorage.getItem("role_status") === "18"
? `/profile/employees/report/${params.id}`
: `/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>
{localStorage.getItem("role_status") !== "18" && (
<div className="view-report__bar">
<button className="view-report__bar__edit">Редактировать</button>
{deleteLoader ? (
<Loader style={"green"} />
) : (
<button
onClick={deleteReport}
className={
taskText.length
? "view-report__bar__delete"
: "view-report__bar__delete disable"
}
>
Удалить отчет
</button>
)}
</div>
)}
2023-05-11 19:54:15 +03:00
</div>
<div className="view-report__switch-date">
2023-12-22 15:18:13 +03:00
<div onClick={() => previousDay()}>
2024-02-02 18:44:11 +03:00
<Link
to={`/profile/calendar/view/${getCreatedDate(
previousReportDay
)}/${params.id}`}
2024-02-02 18:44:11 +03:00
>
<div className="view-report__switch-date__prev switch-date">
2023-05-18 17:42:29 +03:00
<img src={arrowSwitchDate} alt="arrow" />
</div>
</Link>
2023-05-11 19:54:15 +03:00
</div>
2023-05-18 17:42:29 +03:00
2024-02-02 18:43:52 +03:00
<p>{getCorrectDate(params.date)}</p>
2023-05-18 17:42:29 +03:00
2023-05-18 18:38:02 +03:00
<div
onClick={() => nextDay()}
className={`${
2024-02-02 18:43:52 +03:00
getCreatedDate(currentDay) === params.date ? "disable" : ""
2023-05-18 18:38:02 +03:00
}`}
>
<Link
to={`/profile/calendar/view/${getCreatedDate(nextReportDay)}/${
params.id
}`}
>
<div className={`view-report__switch-date__next switch-date`}>
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="view-report__content">
2023-05-11 19:54:15 +03:00
<div className="table__container">
<table className="view-report__done">
2023-05-11 19:54:15 +03:00
<thead>
<tr>
<th>
2023-12-22 15:18:13 +03:00
<p>Какие задачи выполнены?</p>
2023-05-11 19:54:15 +03:00
</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="view-report__done__hours__item">
2023-12-22 15:18:13 +03:00
<span>{Math.floor(task.hours)}</span>
2023-05-11 19:54:15 +03:00
<p className="hours">
2023-12-22 15:18:13 +03:00
{hourOfNum(Math.floor(task.hours))}
2023-05-11 19:54:15 +03:00
</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="view-report__item">
2023-05-11 19:54:15 +03:00
<h3>Какие сложности возникли?</h3>
{difficulties.map((item, index) => {
return <p key={index}>{item}</p>;
})}
</div>
)}
{Boolean(tomorrowTask.length) && (
<div className="view-report__item">
2023-05-11 19:54:15 +03:00
<h3>Что планируется сделать завтра?</h3>
{tomorrowTask.map((item, index) => {
return <p key={index}>{item}</p>;
})}
</div>
)}
</div>
)}
{!Boolean(taskText.length) && !loader && (
<div className="view-report__no-task">
2024-02-02 18:44:11 +03:00
{localStorage.getItem("role_status") === "4" ? (
2024-02-02 18:43:52 +03:00
<p>
В этот день вы <span>не заполняли</span> отчет
2024-02-02 18:44:11 +03:00
</p>
) : (
2024-02-02 18:43:52 +03:00
<p>Отчет за день не заполнен</p>
2024-02-02 18:44:11 +03:00
)}
2023-05-11 19:54:15 +03:00
</div>
)}
<Footer />
</div>
</div>
);
2023-01-23 14:59:57 +03:00
};