From 4c4508bb86831ddcbd24877f395bdaeee95e8b8b Mon Sep 17 00:00:00 2001 From: "Thierry CH." Date: Wed, 30 Oct 2024 22:41:05 +0200 Subject: [PATCH] show notifications acordingly to selected team (#3181) * show notifications acordingly to selected team * add requested changes --- .../team/team-outstanding-notifications.tsx | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/apps/web/lib/features/team/team-outstanding-notifications.tsx b/apps/web/lib/features/team/team-outstanding-notifications.tsx index d86c24d98..d3f7f14f6 100644 --- a/apps/web/lib/features/team/team-outstanding-notifications.tsx +++ b/apps/web/lib/features/team/team-outstanding-notifications.tsx @@ -1,11 +1,11 @@ 'use client'; -import { useAuthenticateUser, useDailyPlan } from '@app/hooks'; +import { useAuthenticateUser, useDailyPlan, useOrganizationTeams } from '@app/hooks'; import { IDailyPlan, IEmployee, IUser } from '@app/interfaces'; import { Cross2Icon, EyeOpenIcon } from '@radix-ui/react-icons'; import { Tooltip } from 'lib/components'; import { useTranslations } from 'next-intl'; import Link from 'next/link'; -import { memo, useEffect, useState } from 'react'; +import { memo, useEffect, useMemo, useState } from 'react'; import { estimatedTotalTime } from '../task/daily-plan'; import { HAS_VISITED_OUTSTANDING_TASKS } from '@app/constants'; import moment from 'moment'; @@ -16,14 +16,13 @@ interface IEmployeeWithOutstanding { } export function TeamOutstandingNotifications() { - const { dailyPlan, getEmployeeDayPlans, outstandingPlans } = useDailyPlan(); - + const { dailyPlan, getAllDayPlans, outstandingPlans } = useDailyPlan(); + const { activeTeam } = useOrganizationTeams(); const { isTeamManager, user } = useAuthenticateUser(); useEffect(() => { - // getAllDayPlans(); - getEmployeeDayPlans(user?.employee.id || ''); - }, [getEmployeeDayPlans, user?.employee.id]); + getAllDayPlans(); + }, [activeTeam, getAllDayPlans]); return (
@@ -53,9 +52,10 @@ const UserOutstandingNotification = memo(function UserOutstandingNotification({ const [visible, setVisible] = useState(false); - const outStandingTasksCount = estimatedTotalTime( - outstandingPlans.map((plan) => plan.tasks?.map((task) => task)) - ).totalTasks; + const outStandingTasksCount = useMemo( + () => estimatedTotalTime(outstandingPlans.map((plan) => plan.tasks?.map((task) => task))).totalTasks, + [outstandingPlans] + ); const lastVisited = window?.localStorage.getItem(HAS_VISITED_OUTSTANDING_TASKS); @@ -131,22 +131,23 @@ const ManagerOutstandingUsersNotification = memo(function ManagerOutstandingUser const [visible, setVisible] = useState(false); - const employeeWithOutstanding = outstandingTasks - .filter((plan) => plan.employeeId !== user?.employee.id) - .filter((plan) => !plan.date?.toString()?.startsWith(new Date()?.toISOString().split('T')[0])) - - .filter((plan) => { - const planDate = new Date(plan.date); - const today = new Date(); - today.setHours(23, 59, 59, 0); - return planDate.getTime() <= today.getTime(); - }) - .map((plan) => ({ - ...plan, - tasks: plan.tasks?.filter((task) => task.status !== 'completed') - })) - .filter((plan) => plan.tasks?.length && plan.tasks.length > 0) - .map((plan) => ({ employeeId: plan.employeeId, employee: plan.employee })); + const employeeWithOutstanding = useMemo( + () => + outstandingTasks + + .filter((plan) => { + if (plan.employeeId === user?.employee.id) return false; + if (!plan.date) return false; + + const isTodayOrBefore = moment(plan.date).isSameOrBefore(moment().endOf('day')); + if (!isTodayOrBefore) return false; + + const hasIncompleteTasks = plan.tasks?.some((task) => task.status !== 'completed'); + return hasIncompleteTasks; + }) + .map((plan) => ({ employeeId: plan.employeeId, employee: plan.employee })), + [outstandingTasks, user?.employee.id] + ); const uniqueEmployees: IEmployeeWithOutstanding[] = employeeWithOutstanding.reduce( (acc: IEmployeeWithOutstanding[], current) => {