Skip to content

Commit

Permalink
add coderabit suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
CREDO23 committed Dec 1, 2024
1 parent 83c8002 commit a789175
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import * as React from 'react';
import { endOfMonth, endOfWeek, format, startOfMonth, startOfWeek, subDays } from 'date-fns';
import { endOfMonth, endOfWeek, format, isSameDay, startOfMonth, startOfWeek, subDays, subMonths } from 'date-fns';
import { Calendar as CalendarIcon } from 'lucide-react';
import { DateRange } from 'react-day-picker';
import { cn } from '@/lib/utils';
Expand Down Expand Up @@ -78,8 +78,12 @@ export function DatePickerWithRange({
</Button>
<Button
onClick={() => {
selectedDate != undefined && onChange(selectedDate);
setDate(selectedDate);
if (selectedDate?.from && selectedDate?.to) {
onChange(selectedDate);
setDate(selectedDate);
} else {
console.warn('Invalid date range selected');
}
}}
className=" grow text-xs h-8 dark:text-white"
size={'sm'}
Expand Down Expand Up @@ -127,7 +131,10 @@ const PresetDates = ({
{ label: t('common.THIS_MONTH'), range: { from: startOfMonth(new Date()), to: endOfMonth(new Date()) } },
{
label: t('common.LAST_MONTH'),
range: { from: startOfMonth(subDays(new Date(), 30)), to: endOfMonth(subDays(new Date(), 30)) }
range: {
from: startOfMonth(subMonths(new Date(), 1)),
to: endOfMonth(subMonths(new Date(), 1))
}
},
{ label: t('common.FILTER_LAST_7_DAYS'), range: { from: subDays(new Date(), 7), to: new Date() } },
{ label: t('common.LAST_TWO_WEEKS'), range: { from: subDays(new Date(), 14), to: new Date() } }
Expand All @@ -141,8 +148,10 @@ const PresetDates = ({
setSelected(
presets.find((preset) => {
return (
date?.from?.toISOString() == preset.range.from.toISOString() &&
date.to?.toISOString() == preset.range.to.toISOString()
date?.from &&
date.to &&
isSameDay(date?.from, preset.range.from) &&
isSameDay(date?.to, preset.range.to)
);
})?.range
);
Expand All @@ -155,15 +164,19 @@ const PresetDates = ({
key={preset.label}
onClick={() => setDate(preset.range)}
variant={
selected?.from?.toISOString() == preset.range.from.toISOString() &&
selected.to?.toISOString() == preset.range.to.toISOString()
selected?.from &&
selected.to &&
isSameDay(selected?.from, preset.range.from) &&
isSameDay(selected?.to, preset.range.to)
? 'default'
: 'outline'
}
className={cn(
' truncate text-left text-sm h-8 px-2 py-1 font-normal border rounded',
selected?.from?.toISOString() == preset.range.from.toISOString() &&
selected.to?.toISOString() == preset.range.to.toISOString() &&
selected?.from &&
selected.to &&
isSameDay(selected?.from, preset.range.from) &&
isSameDay(selected?.to, preset.range.to) &&
'dark:text-white'
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function ExportModeSelect(props: IProps) {
);

return (
<Select value={selected} onValueChange={handleChange}>
<Select aria-label="Select export format" value={selected} onValueChange={handleChange}>
<SelectTrigger className="w-36 overflow-hidden h-[2.2rem] text-clip border border-gray-200 dark:border-gray-700 bg-white dark:bg-dark--theme-light focus:ring-2 focus:ring-transparent">
<SelectValue placeholder="Export" />
</SelectTrigger>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ITimeLimitReport } from '@/app/interfaces/ITimeLimits';
import { DataTableWeeklyLimits } from './data-table';
import moment from 'moment';
import { DEFAULT_WORK_HOURS_PER_DAY } from '@/app/constants';

interface IProps {
report: ITimeLimitReport;
displayMode: 'Week' | 'Day';
organizationLimits: { [key: string]: number };
}

/**
* Renders a time report table displaying employee time tracking data.
*
* @component
* @param {IProps} props - The component props.
* @param {ITimeLimitReport} props.report - Data for employees' time usage reports.
* @param {'Week' | 'Day'} props.displayMode - Specifies whether to display data by week or day.
* @param {{ [key: string]: number }} props.organizationLimits - Contains organizational limits for time usage, specified by mode.
*
* @returns {JSX.Element} A formatted report table showing time usage and limits.
*/
export const TimeReportTable = ({ report, displayMode, organizationLimits }: IProps) => (
<div className="w-full p-1" key={report.date}>
<div className="h-12 px-4 bg-slate-100 dark:bg-gray-800 dark:text-white rounded-md flex border items-center">
<h4 className="text-xs font-medium">
{displayMode === 'Week' ? (
<>
<span>{report.date}</span> <span>-</span>
<span>{moment(report.date).endOf('week').format('YYYY-MM-DD')}</span>
</>
) : (
report.date
)}
</h4>
</div>
<div>
<DataTableWeeklyLimits
data={report.employees?.map((item) => {
const limit = item.limit || organizationLimits[displayMode] || DEFAULT_WORK_HOURS_PER_DAY;
const percentageUsed = (item.duration / limit) * 100;
const remaining = limit - item.duration;

return {
member: item.employee.fullName,
limit,
percentageUsed,
timeSpent: item.duration,
remaining
};
})}
/>
</div>
</div>
);
50 changes: 13 additions & 37 deletions apps/web/app/[locale]/reports/weekly-limit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { DatePickerWithRange } from './components/date-range-select';
import { MembersSelect } from './components/members-select';
import { GroupBySelect, TGroupByOption } from './components/group-by-select';
import { ExportModeSelect } from './components/export-mode-select';
import { DataTableWeeklyLimits } from './components/data-table';
import { getAccessTokenCookie, getOrganizationIdCookie, getTenantIdCookie } from '@/app/helpers';
import { useTimeLimits } from '@/app/hooks/features/useTimeLimits';
import { DateRange } from 'react-day-picker';
Expand All @@ -20,6 +19,7 @@ import { ITimeLimitReport } from '@/app/interfaces/ITimeLimits';
import { getUserOrganizationsRequest } from '@/app/services/server/requests';
import { IOrganization } from '@/app/interfaces';
import { useTranslations } from 'next-intl';
import { TimeReportTable } from './components/time-report-table';

function WeeklyLimitReport() {
const { isTrackingEnabled } = useOrganizationTeams();
Expand Down Expand Up @@ -139,48 +139,24 @@ function WeeklyLimitReport() {
if (displayMode == 'Week') {
if (moment(report.date).isSame(moment(report.date).startOf('isoWeek'), 'day')) {
return (
<div className="w-full p-1" key={report.date}>
<div className="h-12 px-4 bg-slate-100 dark:bg-gray-800 dark:text-white rounded-md flex border items-center">
<h4 className=" text-xs font-medium space-x-5 ">
<span>{report.date}</span> <span>-</span>
<span>{moment(report.date).endOf('week').format('YYYY-MM-DD')}</span>
</h4>
</div>
<div>
<DataTableWeeklyLimits
data={report.employees?.map((item) => ({
member: item.employee.fullName,
limit: item.limit || organizationLimits[displayMode],
percentageUsed:
(item.duration / organizationLimits[displayMode]) * 100,
timeSpent: item.duration,
remaining: organizationLimits[displayMode] - item.duration
}))}
/>
</div>
</div>
<TimeReportTable
organizationLimits={organizationLimits}
report={report}
displayMode={displayMode}
key={report.date}
/>
);
} else {
return null;
}
} else {
return (
<div className="w-full p-1" key={report.date}>
<div className="h-12 px-4 bg-slate-100 dark:bg-gray-800 dark:text-white rounded-md flex border items-center">
<h4 className=" text-xs font-medium ">{report.date}</h4>
</div>
<div>
<DataTableWeeklyLimits
data={report.employees?.map((item) => ({
member: item.employee.fullName,
limit: item.limit || organizationLimits[displayMode],
percentageUsed: (item.duration / organizationLimits[displayMode]) * 100,
timeSpent: item.duration,
remaining: organizationLimits[displayMode] - item.duration
}))}
/>
</div>
</div>
<TimeReportTable
organizationLimits={organizationLimits}
report={report}
displayMode={displayMode}
key={report.date}
/>
);
}
})}
Expand Down
2 changes: 2 additions & 0 deletions apps/web/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,5 @@ export const statusOptions = [
{ value: 'Pending', label: 'Pending' },
{ value: 'Rejected', label: 'Rejected' }
];

export const DEFAULT_WORK_HOURS_PER_DAY = 8;

0 comments on commit a789175

Please sign in to comment.