fixing the days of the week
This commit is contained in:
+29
-2
@@ -29,6 +29,31 @@ function isSameLocalDate(firstDate: Date, secondDate: Date): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
function getWeekStart(date: Date): Date {
|
||||
const weekStart = new Date(date);
|
||||
weekStart.setHours(0, 0, 0, 0);
|
||||
weekStart.setDate(weekStart.getDate() - weekStart.getDay());
|
||||
return weekStart;
|
||||
}
|
||||
|
||||
function addDays(date: Date, days: number): Date {
|
||||
const nextDate = new Date(date);
|
||||
nextDate.setDate(nextDate.getDate() + days);
|
||||
return nextDate;
|
||||
}
|
||||
|
||||
function isScheduleInWeek(schedule: Schedule, weekStart: Date, weekEndExclusive: Date): boolean {
|
||||
if (!schedule.enter_time) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const enterDate = new Date(schedule.enter_time);
|
||||
|
||||
return (
|
||||
!Number.isNaN(enterDate.getTime()) && enterDate >= weekStart && enterDate < weekEndExclusive
|
||||
);
|
||||
}
|
||||
|
||||
function getTodaySchedules(schedules: Schedule[], today: Date): Schedule[] {
|
||||
return schedules.filter((schedule) => {
|
||||
if (!schedule.enter_time) {
|
||||
@@ -132,6 +157,8 @@ export function UsersPage({ backendPreference }: UsersPageProps) {
|
||||
|
||||
const selectedUser = users.find((user) => user.id === selectedUserId) ?? null;
|
||||
const today = new Date();
|
||||
const currentWeekStart = getWeekStart(today);
|
||||
const currentWeekEndExclusive = addDays(currentWeekStart, 7);
|
||||
|
||||
const schedulesByUser = useMemo(() => {
|
||||
return schedules.reduce<Record<string, Schedule[]>>((accumulator, schedule) => {
|
||||
@@ -406,8 +433,8 @@ export function UsersPage({ backendPreference }: UsersPageProps) {
|
||||
const company = companies.find((item) => item.id === user.company_id);
|
||||
const daysWorked = new Set(
|
||||
userSchedules
|
||||
.filter((schedule) => schedule.enter_time)
|
||||
.map((schedule) => schedule.day_of_week),
|
||||
.filter((schedule) => isScheduleInWeek(schedule, currentWeekStart, currentWeekEndExclusive))
|
||||
.map((schedule) => new Date(schedule.enter_time as string).getDay()),
|
||||
).size;
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user