diff --git a/apps/web/app/hooks/features/useDailyPlan.ts b/apps/web/app/hooks/features/useDailyPlan.ts index f7b1d4d7d..6d1a52a31 100644 --- a/apps/web/app/hooks/features/useDailyPlan.ts +++ b/apps/web/app/hooks/features/useDailyPlan.ts @@ -27,6 +27,7 @@ import { import { ICreateDailyPlan, IDailyPlanTasksUpdate, IRemoveTaskFromManyPlans, IUpdateDailyPlan } from '@app/interfaces'; import { useFirstLoad } from '../useFirstLoad'; import { useAuthenticateUser } from './useAuthenticateUser'; +import { removeDuplicateItems } from '@/app/utils/remove-duplicate-item'; export type FilterTabs = 'Today Tasks' | 'Future Tasks' | 'Past Tasks' | 'All Tasks' | 'Outstanding'; @@ -120,7 +121,7 @@ export function useDailyPlan() { if (isPlanExist) { const updatedPlans = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].map((plan) => { if (plan.date?.toString()?.startsWith(new Date(data.date)?.toISOString().split('T')[0])) { - return res.data; + return { ...res.data, tasks: removeDuplicateItems(res.data.tasks) }; } return plan; @@ -133,11 +134,17 @@ export function useDailyPlan() { } else { setProfileDailyPlans({ total: profileDailyPlans.total + 1, - items: [...(profileDailyPlans.items ? profileDailyPlans.items : []), res.data] + items: [ + ...(profileDailyPlans.items ? profileDailyPlans.items : []), + { ...res.data, tasks: removeDuplicateItems(res.data.tasks) } + ] }); } - setEmployeePlans([...(employeePlans ? employeePlans : []), res.data]); + setEmployeePlans([ + ...(employeePlans ? employeePlans : []), + { ...res.data, tasks: removeDuplicateItems(res.data.tasks) } + ]); getMyDailyPlans(); return res; } diff --git a/apps/web/app/utils/remove-duplicate-item.ts b/apps/web/app/utils/remove-duplicate-item.ts new file mode 100644 index 000000000..e4dcad84b --- /dev/null +++ b/apps/web/app/utils/remove-duplicate-item.ts @@ -0,0 +1,22 @@ +/** + * A dynamic function that removes duplicates from an array of items. + * + * @param {any[]} items - The items to check against. + * + * @returns {any[]} - The array of items without duplicates. + */ + +export const removeDuplicateItems = (items?: TItem[]): TItem[] => { + const seenIds = new Set(); + + return items + ? items.filter((item) => { + if (seenIds.has(item.id)) { + return false; + } else { + seenIds.add(item.id); + return true; + } + }) + : []; +}; diff --git a/apps/web/components/ui/dropdown-menu.tsx b/apps/web/components/ui/dropdown-menu.tsx index 3c35f65e9..37d828a57 100644 --- a/apps/web/components/ui/dropdown-menu.tsx +++ b/apps/web/components/ui/dropdown-menu.tsx @@ -39,7 +39,10 @@ DropdownMenuSubTrigger.displayName = const DropdownMenuSubContent = React.forwardRef< React.ElementRef, - React.ComponentPropsWithoutRef + React.ComponentPropsWithoutRef & { + className?: string; + children?: React.ReactNode + } >(({ className, ...props }, ref) => ( )) DropdownMenuSubContent.displayName = @@ -55,7 +58,10 @@ DropdownMenuSubContent.displayName = const DropdownMenuContent = React.forwardRef< React.ElementRef, - React.ComponentPropsWithoutRef + React.ComponentPropsWithoutRef & { + className?: string; + children?: React.ReactNode + } >(({ className, sideOffset = 4, ...props }, ref) => ( )) diff --git a/apps/web/lib/features/daily-plan/create-daily-plan-form-modal.tsx b/apps/web/lib/features/daily-plan/create-daily-plan-form-modal.tsx index ee5dc11c9..2baacd152 100644 --- a/apps/web/lib/features/daily-plan/create-daily-plan-form-modal.tsx +++ b/apps/web/lib/features/daily-plan/create-daily-plan-form-modal.tsx @@ -45,6 +45,13 @@ export function CreateDailyPlanFormModal({ () => profileDailyPlans?.items?.map((plan) => new Date(plan.date)), [profileDailyPlans.items] ); + const existingTaskPlanDates = useMemo( + () => + profileDailyPlans?.items + ?.filter((plan) => plan.tasks?.some((task) => task.id === taskId)) + .map((plan) => new Date(plan.date)), + [profileDailyPlans.items, taskId] + ); const isManagerConnectedUser = useMemo( () => activeTeamManagers.find((member) => member.employee?.user?.id === user?.id), @@ -143,6 +150,7 @@ export function CreateDailyPlanFormModal({ date={date} setDate={setDate} existingPlanDates={existingPlanDates} + existingTaskPlanDates={existingTaskPlanDates} /> )} @@ -227,11 +235,13 @@ export function CreateDailyPlanFormModal({ const CustomCalendar = memo(function CustomCalendar({ date, setDate, - existingPlanDates + existingPlanDates, + existingTaskPlanDates }: { date: Date; setDate: Dispatch>; existingPlanDates: Date[]; + existingTaskPlanDates: Date[]; }) { return ( setDate(day ? day : new Date(tomorrowDate))} initialFocus - disabled={[{ from: new Date(1970, 1, 1), to: yesterdayDate }]} + disabled={[{ from: new Date(1970, 1, 1), to: yesterdayDate }, ...existingTaskPlanDates]} modifiers={{ booked: existingPlanDates }}