Merge branch 'main' of https://git.itguild.info/apuc/guild_front
debag-conflict
This commit is contained in:
@ -3,9 +3,11 @@ import React, { useEffect, useState } from "react";
|
||||
import DatePicker, { registerLocale } from "react-datepicker";
|
||||
import "react-datepicker/dist/react-datepicker.css";
|
||||
import { useSelector } from "react-redux";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { Link, Navigate, useNavigate } from "react-router-dom";
|
||||
|
||||
import { getReportDate } from "@redux/reportSlice";
|
||||
import { getEditReport, getReportDate } from "@redux/reportSlice";
|
||||
import { setEditReport } from "@redux/reportSlice";
|
||||
|
||||
import { apiRequest } from "@api/request";
|
||||
|
||||
@ -35,6 +37,8 @@ const ReportForm = () => {
|
||||
}
|
||||
const navigate = useNavigate();
|
||||
const reportDate = useSelector(getReportDate);
|
||||
const editReport = useSelector(getEditReport);
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
initListeners();
|
||||
@ -43,15 +47,23 @@ const ReportForm = () => {
|
||||
const [isFetching, setIsFetching] = useState(false);
|
||||
const [reportSuccess, setReportSuccess] = useState("");
|
||||
const [startDate, setStartDate] = useState(
|
||||
reportDate ? new Date(reportDate._d) : new Date()
|
||||
reportDate || editReport
|
||||
? new Date(reportDate ? reportDate._d : editReport.created_at)
|
||||
: new Date()
|
||||
);
|
||||
const [datePickerOpen, setDatePickerOpen] = useState(false);
|
||||
|
||||
const [inputs, setInputs] = useState([
|
||||
{ task: "", hours_spent: "", minutes_spent: 0 }
|
||||
]);
|
||||
const [troublesInputValue, setTroublesInputValue] = useState("");
|
||||
const [scheduledInputValue, setScheduledInputValue] = useState("");
|
||||
const [inputs, setInputs] = useState(
|
||||
editReport
|
||||
? editReport.task
|
||||
: [{ task: "", hours_spent: "", minutes_spent: 0 }]
|
||||
);
|
||||
const [troublesInputValue, setTroublesInputValue] = useState(
|
||||
editReport ? editReport.difficulties : ""
|
||||
);
|
||||
const [scheduledInputValue, setScheduledInputValue] = useState(
|
||||
editReport ? editReport.tomorrow : ""
|
||||
);
|
||||
|
||||
const addInput = () => {
|
||||
setInputs((prev) => [
|
||||
@ -112,14 +124,49 @@ const ReportForm = () => {
|
||||
setReportSuccess("");
|
||||
navigate("/profile/calendar");
|
||||
}, 1000);
|
||||
setInputs(() => []);
|
||||
setTroublesInputValue("");
|
||||
setScheduledInputValue("");
|
||||
setIsFetching(false);
|
||||
setInputs(() => [{ task: "", hours_spent: "", minutes_spent: 0 }]);
|
||||
clearParams();
|
||||
});
|
||||
};
|
||||
|
||||
const handleEditReport = () => {
|
||||
setIsFetching(true);
|
||||
for (let input of inputs) {
|
||||
if (!input.task || !input.hours_spent) {
|
||||
setReportSuccess("Заполните задачи");
|
||||
setTimeout(() => setReportSuccess(""), 2000);
|
||||
setIsFetching(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
apiRequest(`/reports/update?id=${editReport.id}`, {
|
||||
method: "PUT",
|
||||
data: {
|
||||
tasks: inputs,
|
||||
difficulties: troublesInputValue,
|
||||
tomorrow: scheduledInputValue,
|
||||
created_at: getCreatedDate(startDate),
|
||||
status: 1
|
||||
}
|
||||
}).then(() => {
|
||||
setReportSuccess("Отчет изменён");
|
||||
setTimeout(() => {
|
||||
setReportSuccess("");
|
||||
navigate("/profile/calendar");
|
||||
}, 1000);
|
||||
setIsFetching(false);
|
||||
clearParams();
|
||||
dispatch(setEditReport(""));
|
||||
});
|
||||
};
|
||||
|
||||
const clearParams = () => {
|
||||
setInputs(() => []);
|
||||
setTroublesInputValue("");
|
||||
setScheduledInputValue("");
|
||||
setInputs(() => [{ task: "", hours_spent: "", minutes_spent: 0 }]);
|
||||
};
|
||||
|
||||
return (
|
||||
<section className="report-form">
|
||||
<ProfileHeader />
|
||||
@ -133,7 +180,8 @@ const ReportForm = () => {
|
||||
]}
|
||||
/>
|
||||
<h2 className="summary__title">
|
||||
Ваши отчеты - <span>добавить отчет</span>
|
||||
Ваши отчеты -{" "}
|
||||
<span>{editReport ? "редактировать отчет" : "добавить отчет"}</span>
|
||||
</h2>
|
||||
<div>
|
||||
<div className="report__head">
|
||||
@ -147,13 +195,14 @@ const ReportForm = () => {
|
||||
<div className="report-form__content">
|
||||
<div className="report-form__block">
|
||||
<div className="report-form__block-title">
|
||||
<h2>Добавление отчета за день</h2>
|
||||
<h2>
|
||||
{editReport
|
||||
? "Редактирование отчета за день"
|
||||
: "Добавление отчета за день"}
|
||||
</h2>
|
||||
<h3>Дата заполнения отчета:</h3>
|
||||
</div>
|
||||
<div
|
||||
className="report-form__block-img"
|
||||
onClick={() => setDatePickerOpen(true)}
|
||||
>
|
||||
<div className="report-form__block-img">
|
||||
<img
|
||||
className="report-form__calendar-icon"
|
||||
src={calendarIcon}
|
||||
@ -299,9 +348,15 @@ const ReportForm = () => {
|
||||
) : (
|
||||
<button
|
||||
className="report-form__footer-btn"
|
||||
onClick={() => handler()}
|
||||
onClick={() => {
|
||||
if (editReport) {
|
||||
handleEditReport();
|
||||
} else {
|
||||
handler();
|
||||
}
|
||||
}}
|
||||
>
|
||||
Отпаравить
|
||||
{editReport ? "Редактировать" : "Отправить"}
|
||||
</button>
|
||||
)}
|
||||
<p className="report-form__footer-text">
|
||||
|
Reference in New Issue
Block a user