Merge pull request #133 from apuc/tracker-fix

Tracker fix
This commit is contained in:
NikoM1k 2023-10-12 12:50:51 +03:00 committed by GitHub
commit 45833e555a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 15 deletions

View File

@ -37,11 +37,16 @@ export const ProfileCalendar = () => {
const [totalHours, setTotalHours] = useState(0); const [totalHours, setTotalHours] = useState(0);
const [loader, setLoader] = useState(true); const [loader, setLoader] = useState(true);
const [startRangeDays, setStartRangeDays] = useState(false); const [startRangeDays, setStartRangeDays] = useState(false);
const [startDate, setStartDate] = useState(null);
function setValueHandler(value) { function setValueHandler(value) {
setValue(value); setValue(value);
} }
function setStartDateRange(date) {
setStartDate(date);
}
function toggleStartRangeDays() { function toggleStartRangeDays() {
setStartRangeDays(!startRangeDays); setStartRangeDays(!startRangeDays);
} }
@ -122,6 +127,8 @@ export const ProfileCalendar = () => {
totalHours={totalHours} totalHours={totalHours}
startRangeDays={startRangeDays} startRangeDays={startRangeDays}
toggleRangeDays={toggleStartRangeDays} toggleRangeDays={toggleStartRangeDays}
startDate={startDate}
setStartDateRange={setStartDateRange}
/> />
</div> </div>
</div> </div>

View File

@ -38,6 +38,8 @@ export const ProfileCalendarComponent = React.memo(
totalHours, totalHours,
startRangeDays, startRangeDays,
toggleRangeDays, toggleRangeDays,
startDate,
setStartDateRange,
}) => { }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -45,7 +47,6 @@ export const ProfileCalendarComponent = React.memo(
const [calendar, setCalendar] = useState([]); const [calendar, setCalendar] = useState([]);
const [month, setMonth] = useState(""); const [month, setMonth] = useState("");
const [shortReport, setShortReport] = useState(false); const [shortReport, setShortReport] = useState(false);
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null); const [endDate, setEndDate] = useState(null);
const [totalRangeHours, setTotalRangeHours] = useState(0); const [totalRangeHours, setTotalRangeHours] = useState(0);
const [selectedRangeDays, setSelectedRangeDays] = useState({}); const [selectedRangeDays, setSelectedRangeDays] = useState({});
@ -58,6 +59,9 @@ export const ProfileCalendarComponent = React.memo(
[array[0]]: false, [array[0]]: false,
})); }));
}); });
if (endDate) {
resetRangeDays();
}
}, [value]); }, [value]);
useEffect(() => { useEffect(() => {
@ -113,9 +117,14 @@ export const ProfileCalendarComponent = React.memo(
} }
function reportsByDate(endDay) { function reportsByDate(endDay) {
const requestDates = `fromDate=${getCorrectYYMMDD( const requestDates =
startDate < endDay
? `fromDate=${getCorrectYYMMDD(
startDate._d startDate._d
)}&toDate=${getCorrectYYMMDD(endDay._d)}`; )}&toDate=${getCorrectYYMMDD(endDay._d)}`
: `fromDate=${getCorrectYYMMDD(endDay._d)}&toDate=${getCorrectYYMMDD(
startDate._d
)}`;
apiRequest( apiRequest(
`/reports/reports-by-date?${requestDates}&user_card_id=${localStorage.getItem( `/reports/reports-by-date?${requestDates}&user_card_id=${localStorage.getItem(
"cardId" "cardId"
@ -135,7 +144,7 @@ export const ProfileCalendarComponent = React.memo(
function rangeDays(day) { function rangeDays(day) {
if (!startDate) { if (!startDate) {
setStartDate(day); setStartDateRange(day);
} else { } else {
setEndDate(day); setEndDate(day);
reportsByDate(day); reportsByDate(day);
@ -145,6 +154,7 @@ export const ProfileCalendarComponent = React.memo(
function onMouseRangeDays(day) { function onMouseRangeDays(day) {
let selectRange = {}; let selectRange = {};
for (let curDay in selectedRangeDays) { for (let curDay in selectedRangeDays) {
if (day > startDate) {
if ( if (
day > startDate && day > startDate &&
new Date(curDay) > startDate && new Date(curDay) > startDate &&
@ -154,12 +164,23 @@ export const ProfileCalendarComponent = React.memo(
} else { } else {
selectRange[curDay] = false; selectRange[curDay] = false;
} }
} else {
if (
day < startDate &&
new Date(curDay) < startDate &&
new Date(curDay) > day
) {
selectRange[curDay] = true;
} else {
selectRange[curDay] = false;
}
}
} }
setSelectedRangeDays(selectRange); setSelectedRangeDays(selectRange);
} }
function resetRangeDays() { function resetRangeDays() {
setStartDate(null); setStartDateRange(null);
setEndDate(null); setEndDate(null);
setTotalRangeHours(0); setTotalRangeHours(0);
calendarHelper(value).map((array) => { calendarHelper(value).map((array) => {
@ -246,7 +267,8 @@ export const ProfileCalendarComponent = React.memo(
className={ className={
startRangeDays startRangeDays
? `selectDays ${ ? `selectDays ${
startDate === day || endDate === day String(startDate?._d) === String(day._d) ||
endDate === day
? "selectedDay" ? "selectedDay"
: "" : ""
} ${endDate ? "disable" : ""} ${ } ${endDate ? "disable" : ""} ${
@ -279,7 +301,9 @@ export const ProfileCalendarComponent = React.memo(
}} }}
> >
{endDate {endDate
? startDate < endDate
? `${getCorrectDate(startDate)} - ${getCorrectDate(endDate)}` ? `${getCorrectDate(startDate)} - ${getCorrectDate(endDate)}`
: `${getCorrectDate(endDate)} - ${getCorrectDate(startDate)}`
: "Выбрать диапазон"} : "Выбрать диапазон"}
</span> </span>
<span> <span>