From 490c021ab2b36ecbd2f8d2e895a04e8c68eb54b0 Mon Sep 17 00:00:00 2001 From: Mikola Date: Fri, 6 Oct 2023 17:41:26 +0300 Subject: [PATCH] reports date range --- .../ProfileCalendarComponent.jsx | 103 ++++++++++-------- 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/src/components/ProfileCalendar/ProfileCalendarComponent.jsx b/src/components/ProfileCalendar/ProfileCalendarComponent.jsx index 33551f85..709c283f 100644 --- a/src/components/ProfileCalendar/ProfileCalendarComponent.jsx +++ b/src/components/ProfileCalendar/ProfileCalendarComponent.jsx @@ -1,6 +1,9 @@ +import ru from "date-fns/locale/ru"; import moment from "moment"; import "moment/locale/ru"; import React, { useEffect, useState } from "react"; +import DatePicker, { registerLocale } from "react-datepicker"; +import "react-datepicker/dist/react-datepicker.css"; import { useDispatch } from "react-redux"; import { Link } from "react-router-dom"; @@ -9,25 +12,25 @@ import { setRequestDate, setSendRequest, } from "@redux/reportSlice"; -import DatePicker, { registerLocale } from "react-datepicker"; -import ru from "date-fns/locale/ru"; -import "react-datepicker/dist/react-datepicker.css"; + +import { getCorrectYYMMDD } from "@utils/helper"; + +import { apiRequest } from "@api/request"; import "@components/Calendar/calendarComponent.scss"; import { - getCorrectDate, calendarHelper, currentMonthAndDay, + getCorrectDate, getReports, hourOfNum, } from "@components/Calendar/calendarHelper"; -import { getCorrectYYMMDD } from "@utils/helper"; import ShortReport from "@components/ShortReport/ShortReport"; import arrow from "assets/icons/arrows/arrowCalendar.png"; import calendarIcon from "assets/icons/calendar.svg"; import rectangle from "assets/images/rectangle__calendar.png"; -import { apiRequest } from "@api/request"; + registerLocale("ru", ru); // eslint-disable-next-line react/display-name @@ -42,8 +45,7 @@ export const ProfileCalendarComponent = React.memo( const [startDate, setStartDate] = useState(new Date()); const [endDate, setEndDate] = useState(null); const [datePickerOpen, setDatePickerOpen] = useState(false); - const [totalRangeHours, setTotalRangeHours] = useState(0) - + const [totalRangeHours, setTotalRangeHours] = useState(0); useEffect(() => { setCalendar(calendarHelper(value)); @@ -102,22 +104,24 @@ export const ProfileCalendarComponent = React.memo( } function reportsByDate(start, end) { - const requestDates = `fromDate=${getCorrectYYMMDD(start)}&toDate=${getCorrectYYMMDD(end)}` - apiRequest( - `/reports/reports-by-date?${requestDates}&user_card_id=${localStorage.getItem( - "cardId" - )}` - ).then((reports) => { - let spendTime = 0; - for (const report of reports) { - report.task.map((task) => { - if (task.hours_spent) { - spendTime += Number(task.hours_spent); - } - }); + const requestDates = `fromDate=${getCorrectYYMMDD( + start + )}&toDate=${getCorrectYYMMDD(end)}`; + apiRequest( + `/reports/reports-by-date?${requestDates}&user_card_id=${localStorage.getItem( + "cardId" + )}` + ).then((reports) => { + let spendTime = 0; + for (const report of reports) { + report.task.map((task) => { + if (task.hours_spent) { + spendTime += Number(task.hours_spent); } - setTotalRangeHours(spendTime) - }) + }); + } + setTotalRangeHours(spendTime); + }); } return ( @@ -201,27 +205,38 @@ export const ProfileCalendarComponent = React.memo( )} -
- {setDatePickerOpen(!datePickerOpen)}}> - {endDate ? `${getCorrectDate(startDate)} - ${getCorrectDate(endDate)}` : 'Выбрать диапазон'} - - { - const [start, end] = dates; - setStartDate(start); - setEndDate(end); - if (end) { - setDatePickerOpen(false) - reportsByDate(start, end) - } - }} - selectsRange - /> - {totalRangeHours ? `${totalRangeHours} ${hourOfNum(totalRangeHours)}` : '0 часов'} +
+ { + setDatePickerOpen(!datePickerOpen); + }} + > + {endDate + ? `${getCorrectDate(startDate)} - ${getCorrectDate(endDate)}` + : "Выбрать диапазон"} + + { + const [start, end] = dates; + setStartDate(start); + setEndDate(end); + if (end) { + setDatePickerOpen(false); + reportsByDate(start, end); + } + }} + selectsRange + /> + + {totalRangeHours + ? `${totalRangeHours} ${hourOfNum(totalRangeHours)}` + : "0 часов"} +
{shortReport && }