import { RecordLeaveInput, RecordTimesheetInput, } from "@/app/api/timesheets/actions"; import { manhourFormatter, shortDateFormatter } from "@/app/utils/formatUtil"; import { ArrowBack, Check } from "@mui/icons-material"; import { Box, Button, Card, CardActionArea, CardContent, Stack, Typography, } from "@mui/material"; import dayjs from "dayjs"; import React, { useCallback, useState } from "react"; import { useTranslation } from "react-i18next"; import { DAILY_NORMAL_MAX_HOURS, LEAVE_DAILY_MAX_HOURS, TIMESHEET_DAILY_MAX_HOURS, } from "@/app/api/timesheets/utils"; import { HolidaysResult } from "@/app/api/holidays"; import { getHolidayForDate } from "@/app/utils/holidayUtils"; interface Props { days: string[]; companyHolidays: HolidaysResult[]; leaveEntries: RecordLeaveInput; timesheetEntries: RecordTimesheetInput; EntryComponent: React.FunctionComponent< EntryComponentProps & { date: string } >; entryComponentProps: EntryComponentProps; errorComponent?: React.ReactNode; } function DateHoursList({ days, leaveEntries, timesheetEntries, EntryComponent, entryComponentProps, companyHolidays, errorComponent, }: Props) { const { t, i18n: { language }, } = useTranslation("home"); const [selectedDate, setSelectedDate] = useState(""); const isDateSelected = selectedDate !== ""; const makeSelectDate = useCallback( (date: string) => () => { setSelectedDate(date); }, [], ); const onDateDone = useCallback>( (e) => { setSelectedDate(""); e.preventDefault(); }, [], ); return ( <> {isDateSelected ? ( ) : ( {days.map((day, index) => { const dayJsObj = dayjs(day); const holiday = getHolidayForDate(day, companyHolidays); const isHoliday = holiday || dayJsObj.day() === 0 || dayJsObj.day() === 6; const leaves = leaveEntries[day]; const leaveHours = leaves?.reduce((acc, entry) => acc + entry.inputHours, 0) || 0; const timesheet = timesheetEntries[day]; const timesheetNormalHours = timesheet?.reduce( (acc, entry) => acc + (entry.inputHours || 0), 0, ) || 0; const timesheetOtHours = timesheet?.reduce( (acc, entry) => acc + (entry.otHours || 0), 0, ) || 0; const timesheetHours = timesheetNormalHours + timesheetOtHours; const dailyTotal = leaveHours + timesheetHours; const normalHoursExceeded = timesheetNormalHours > DAILY_NORMAL_MAX_HOURS; const leaveExceeded = leaveHours > LEAVE_DAILY_MAX_HOURS; const dailyTotalExceeded = dailyTotal > TIMESHEET_DAILY_MAX_HOURS; return ( {shortDateFormatter(language).format(dayJsObj.toDate())} {holiday && ( {`(${holiday.title})`} )} {t("Timesheet Hours")} {manhourFormatter.format(timesheetHours)} {normalHoursExceeded && ( {t( "The daily normal hours cannot be more than {{DAILY_NORMAL_MAX_HOURS}}. Please use other hours for exceeding hours.", { DAILY_NORMAL_MAX_HOURS, }, )} )} {t("Leave Hours")} {manhourFormatter.format(leaveHours)} {leaveExceeded && ( {t("Leave hours cannot be more than {{hours}}", { hours: LEAVE_DAILY_MAX_HOURS, })} )} {t("Daily Total Hours")} {manhourFormatter.format(timesheetHours + leaveHours)} {dailyTotalExceeded && ( {t( "The daily total hours cannot be more than {{TIMESHEET_DAILY_MAX_HOURS}}", { TIMESHEET_DAILY_MAX_HOURS, }, )} )} ); })} )} {errorComponent} {isDateSelected ? ( ) : ( )} ); } export default DateHoursList;