Skip to content

Commit

Permalink
we should not plan the same task twice (#3244)
Browse files Browse the repository at this point in the history
* we should not plan the same task twice

* fix : fix build errors
  • Loading branch information
CREDO23 authored Nov 8, 2024
1 parent 3740f52 commit 728f961
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
13 changes: 10 additions & 3 deletions apps/web/app/hooks/features/useDailyPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions apps/web/app/utils/remove-duplicate-item.ts
Original file line number Diff line number Diff line change
@@ -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 = <TItem extends { id: string }>(items?: TItem[]): TItem[] => {
const seenIds = new Set<string>();

return items
? items.filter((item) => {
if (seenIds.has(item.id)) {
return false;
} else {
seenIds.add(item.id);
return true;
}
})
: [];
};
14 changes: 10 additions & 4 deletions apps/web/components/ui/dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,29 @@ DropdownMenuSubTrigger.displayName =

const DropdownMenuSubContent = React.forwardRef<
React.ElementRef<typeof DropdownMenuPrimitive.SubContent>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent>
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent> & {
className?: string;
children?: React.ReactNode
}
>(({ className, ...props }, ref) => (
<DropdownMenuPrimitive.SubContent
ref={ref}
className={cn(
"z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
)}
{...props}
{...(props as any)}
/>
))
DropdownMenuSubContent.displayName =
DropdownMenuPrimitive.SubContent.displayName

const DropdownMenuContent = React.forwardRef<
React.ElementRef<typeof DropdownMenuPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Content>
React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Content> & {
className?: string;
children?: React.ReactNode
}
>(({ className, sideOffset = 4, ...props }, ref) => (
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Content
Expand All @@ -65,7 +71,7 @@ const DropdownMenuContent = React.forwardRef<
"z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
)}
{...props}
{...(props as any)}
/>
</DropdownMenuPrimitive.Portal>
))
Expand Down
14 changes: 12 additions & 2 deletions apps/web/lib/features/daily-plan/create-daily-plan-form-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -143,6 +150,7 @@ export function CreateDailyPlanFormModal({
date={date}
setDate={setDate}
existingPlanDates={existingPlanDates}
existingTaskPlanDates={existingTaskPlanDates}
/>
</div>
)}
Expand Down Expand Up @@ -227,11 +235,13 @@ export function CreateDailyPlanFormModal({
const CustomCalendar = memo(function CustomCalendar({
date,
setDate,
existingPlanDates
existingPlanDates,
existingTaskPlanDates
}: {
date: Date;
setDate: Dispatch<SetStateAction<Date>>;
existingPlanDates: Date[];
existingTaskPlanDates: Date[];
}) {
return (
<Calendar
Expand All @@ -240,7 +250,7 @@ const CustomCalendar = memo(function CustomCalendar({
selected={date}
onSelect={(day) => 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
}}
Expand Down

0 comments on commit 728f961

Please sign in to comment.