Skip to content

Commit

Permalink
fix(schedules): include current week in active assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 3, 2024
1 parent 7e2e23b commit 114ed37
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/features/meetings/my_assignments/useAssignments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const useMyAssignments = () => {
(record) =>
(record.assignment.person === userUID ||
delegateMembers.includes(record.assignment.person)) &&
new Date(record.weekOf).toISOString() >= now.toISOString() &&
new Date(record.weekOf).toISOString() <= maxDate.toISOString()
formatDate(new Date(record.weekOf), 'yyyy/MM/dd') >=
formatDate(now, 'yyyy/MM/dd') &&
formatDate(new Date(record.weekOf), 'yyyy/MM/dd') <=
formatDate(maxDate, 'yyyy/MM/dd')
);

if (exactDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo, useState } from 'react';
import { useRecoilValue } from 'recoil';
import { useAppTranslation, useIntersectionObserver } from '@hooks/index';
import { schedulesState } from '@states/schedules';
import { getWeekDate } from '@utils/date';
import { addMonths, getWeekDate } from '@utils/date';
import { formatDate } from '@services/dateformat';
import {
midweekMeetingClassCountState,
Expand Down Expand Up @@ -48,11 +48,17 @@ const useMidweekMeeting = () => {
return schedules.length === 0;
}, [schedules]);

const filteredSchedules = useMemo(() => {
const minDate = formatDate(addMonths(new Date(), -2), 'yyyy/MM/dd');

return schedules.filter((record) => record.weekOf >= minDate);
}, [schedules]);

const week = useMemo(() => {
if (typeof value === 'boolean') return null;

return schedules.at(value)?.weekOf || null;
}, [value, schedules]);
return filteredSchedules.at(value)?.weekOf || null;
}, [value, filteredSchedules]);

const schedule = useMemo(() => {
return schedules.find((record) => record.weekOf === week);
Expand Down Expand Up @@ -177,7 +183,9 @@ const useMidweekMeeting = () => {
const now = getWeekDate();
const weekOf = formatDate(now, 'yyyy/MM/dd');

const index = schedules.findIndex((record) => record.weekOf === weekOf);
const index = filteredSchedules.findIndex(
(record) => record.weekOf === weekOf
);

setValue(index);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@ const useWeekSelector = ({ onChange, value }: WeekSelectorProps) => {

const [currentTab, setCurrentTab] = useState<number | boolean>(false);

const filteredSources = useMemo(() => {
const minDate = formatDate(addMonths(new Date(), -2), 'yyyy/MM/dd');

return sources.filter((record) => record.weekOf >= minDate);
}, [sources]);

const defaultValue = useMemo(() => {
const now = getWeekDate();
const weekOf = formatDate(now, 'yyyy/MM/dd');

return sources.findIndex((record) => record.weekOf === weekOf);
}, [sources]);
return filteredSources.findIndex((record) => record.weekOf === weekOf);
}, [filteredSources]);

const weeksTab = useMemo(() => {
let filteredSources = structuredClone(sources);

const minDate = formatDate(addMonths(new Date(), 2), 'yyyy/MM/dd');

filteredSources = sources.filter((record) => record.weekOf >= minDate);
let weeksList = structuredClone(filteredSources);

if (scheduleType === 'midweek') {
filteredSources = filteredSources.filter(
weeksList = weeksList.filter(
(record) => record.midweek_meeting.week_date_locale[lang]?.length > 0
);
}

return filteredSources.map((source, index) => {
return weeksList.map((source, index) => {
const [, month, date] = source.weekOf.split('/');
const monthName = months[+month - 1];

Expand All @@ -52,15 +54,15 @@ const useWeekSelector = ({ onChange, value }: WeekSelectorProps) => {
Component: <></>,
};
});
}, [sources, months, t, defaultValue, scheduleType, lang]);
}, [months, t, defaultValue, scheduleType, lang, filteredSources]);

const handleWeekChange = (value: number) => {
setCurrentTab(value);
onChange?.(value);
};

useEffect(() => {
if (!value) {
if (value === false) {
setCurrentTab(defaultValue);
onChange?.(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo, useState } from 'react';
import { useRecoilValue } from 'recoil';
import { useAppTranslation, useIntersectionObserver } from '@hooks/index';
import { schedulesState } from '@states/schedules';
import { getWeekDate, timeAddMinutes } from '@utils/date';
import { addMonths, getWeekDate, timeAddMinutes } from '@utils/date';
import { formatDate } from '@services/dateformat';
import {
userDataViewState,
Expand Down Expand Up @@ -50,11 +50,17 @@ const useWeekendMeeting = () => {
return schedules.length === 0;
}, [schedules]);

const filteredSchedules = useMemo(() => {
const minDate = formatDate(addMonths(new Date(), -2), 'yyyy/MM/dd');

return schedules.filter((record) => record.weekOf >= minDate);
}, [schedules]);

const week = useMemo(() => {
if (typeof value === 'boolean') return null;

return schedules.at(value)?.weekOf || null;
}, [value, schedules]);
return filteredSchedules.at(value)?.weekOf || null;
}, [value, filteredSchedules]);

const schedule = useMemo(() => {
return schedules.find((record) => record.weekOf === week);
Expand Down Expand Up @@ -194,7 +200,9 @@ const useWeekendMeeting = () => {
const now = getWeekDate();
const weekOf = formatDate(now, 'yyyy/MM/dd');

const index = schedules.findIndex((record) => record.weekOf === weekOf);
const index = filteredSchedules.findIndex(
(record) => record.weekOf === weekOf
);

setValue(index);
};
Expand Down
5 changes: 3 additions & 2 deletions src/pages/dashboard/useDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isMyAssignmentOpenState } from '@states/app';
import { assignmentsHistoryState } from '@states/schedules';
import { getWeekDate } from '@utils/date';
import { isDemo } from '@constants/index';
import { formatDate } from '@services/dateformat';

const useDashboard = () => {
const setIsMyAssignmentOpen = useSetRecoilState(isMyAssignmentOpenState);
Expand All @@ -31,12 +32,12 @@ const useDashboard = () => {
const [newCongSnack, setNewCongSnack] = useState(initialSnackValue);

const countFutureAssignments = useMemo(() => {
const now = getWeekDate().toISOString();
const now = formatDate(getWeekDate(), 'yyyy/MM/dd');

const personAssignments = assignmentsHistory.filter(
(record) =>
record.assignment.person === userUID &&
new Date(record.weekOf).toISOString() >= now
formatDate(new Date(record.weekOf), 'yyyy/MM/dd') >= now
);

return personAssignments.length;
Expand Down

0 comments on commit 114ed37

Please sign in to comment.