diff --git a/apps/web/app/hooks/features/useDailyPlan.ts b/apps/web/app/hooks/features/useDailyPlan.ts index de33fb21a..7f46646fa 100644 --- a/apps/web/app/hooks/features/useDailyPlan.ts +++ b/apps/web/app/hooks/features/useDailyPlan.ts @@ -108,11 +108,11 @@ export function useDailyPlan() { user?.tenantId || '' ); //Check if there is an existing plan - const isPlanExist = profileDailyPlans.items.find((plan) => + const isPlanExist = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].find((plan) => plan.date?.toString()?.startsWith(new Date(data.date)?.toISOString().split('T')[0]) ); if (isPlanExist) { - const updatedPlans = profileDailyPlans.items.map((plan) => { + const updatedPlans = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].map((plan) => { if (plan.date?.toString()?.startsWith(new Date(data.date)?.toISOString().split('T')[0])) { return res.data; } @@ -127,11 +127,11 @@ export function useDailyPlan() { } else { setProfileDailyPlans({ total: profileDailyPlans.total + 1, - items: [...profileDailyPlans.items, res.data] + items: [...(profileDailyPlans.items ? profileDailyPlans.items : []), res.data] }); } - setEmployeePlans([...employeePlans, res.data]); + setEmployeePlans([...(employeePlans ? employeePlans : []), res.data]); getMyDailyPlans(); return res; } @@ -151,8 +151,10 @@ export function useDailyPlan() { const updateDailyPlan = useCallback( async (data: IUpdateDailyPlan, planId: string) => { const res = await updateQueryCall(data, planId); - const updated = profileDailyPlans.items.filter((plan) => plan.id != planId); - const updatedEmployee = employeePlans.filter((plan) => plan.id != planId); + const updated = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].filter( + (plan) => plan.id != planId + ); + const updatedEmployee = [...(employeePlans ? employeePlans : [])].filter((plan) => plan.id != planId); setProfileDailyPlans({ total: profileDailyPlans.total, items: [...updated, res.data] @@ -178,8 +180,10 @@ export function useDailyPlan() { const addTaskToPlan = useCallback( async (data: IDailyPlanTasksUpdate, planId: string) => { const res = await addTaskToPlanQueryCall(data, planId); - const updated = profileDailyPlans.items.filter((plan) => plan.id != planId); - const updatedEmployee = employeePlans.filter((plan) => plan.id != planId); + const updated = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].filter( + (plan) => plan.id != planId + ); + const updatedEmployee = [...(employeePlans ? employeePlans : [])].filter((plan) => plan.id != planId); setProfileDailyPlans({ total: profileDailyPlans.total, items: [...updated, res.data] @@ -201,8 +205,10 @@ export function useDailyPlan() { const removeTaskFromPlan = useCallback( async (data: IDailyPlanTasksUpdate, planId: string) => { const res = await removeTAskFromPlanQueryCall(data, planId); - const updated = profileDailyPlans.items.filter((plan) => plan.id != planId); - const updatedEmployee = employeePlans.filter((plan) => plan.id != planId); + const updated = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].filter( + (plan) => plan.id != planId + ); + const updatedEmployee = [...(employeePlans ? employeePlans : [])].filter((plan) => plan.id != planId); setProfileDailyPlans({ total: profileDailyPlans.total, items: [...updated, res.data] @@ -224,14 +230,14 @@ export function useDailyPlan() { const removeManyTaskPlans = useCallback( async (data: IRemoveTaskFromManyPlans, taskId: string) => { const res = await removeManyTaskPlanQueryCall({ taskId, data }); - const updatedProfileDailyPlans = profileDailyPlans.items + const updatedProfileDailyPlans = [...(profileDailyPlans.items ? profileDailyPlans.items : [])] .map((plan) => { const updatedTasks = plan.tasks ? plan.tasks.filter((task) => task.id !== taskId) : []; return { ...plan, tasks: updatedTasks }; }) .filter((plan) => plan.tasks && plan.tasks.length > 0); // Delete plans without tasks - const updatedEmployeePlans = employeePlans + const updatedEmployeePlans = [...(employeePlans ? employeePlans : [])] .map((plan) => { const updatedTasks = plan.tasks ? plan.tasks.filter((task) => task.id !== taskId) : []; return { ...plan, tasks: updatedTasks }; @@ -259,8 +265,10 @@ export function useDailyPlan() { const deleteDailyPlan = useCallback( async (planId: string) => { const res = await deleteDailyPlanQueryCall(planId); - const updated = profileDailyPlans.items.filter((plan) => plan.id != planId); - const updatedEmployee = employeePlans.filter((plan) => plan.id != planId); + const updated = [...(profileDailyPlans.items ? profileDailyPlans.items : [])].filter( + (plan) => plan.id != planId + ); + const updatedEmployee = [...(employeePlans ? employeePlans : [])].filter((plan) => plan.id != planId); setProfileDailyPlans({ total: updated.length, items: [...updated] }); setEmployeePlans([...updatedEmployee]); diff --git a/apps/web/lib/features/daily-plan/add-task-estimation-hours-modal.tsx b/apps/web/lib/features/daily-plan/add-task-estimation-hours-modal.tsx index 669c5c7b9..1413d496d 100644 --- a/apps/web/lib/features/daily-plan/add-task-estimation-hours-modal.tsx +++ b/apps/web/lib/features/daily-plan/add-task-estimation-hours-modal.tsx @@ -21,6 +21,7 @@ import { Popover, Transition } from '@headlessui/react'; import { ScrollArea, ScrollBar } from '@components/ui/scroll-bar'; import { Cross2Icon } from '@radix-ui/react-icons'; import { checkPastDate } from 'lib/utils'; +import { UnplanActiveTaskModal } from './unplan-active-task-modal'; /** * A modal that allows user to add task estimation / planned work time, etc. @@ -76,6 +77,10 @@ export function AddTasksEstimationHoursModal(props: IAddTasksEstimationHoursModa [activeTeamTask?.id, plan.tasks] ); const [isWorkingTimeInputFocused, setWorkingTimeInputFocused] = useState(false); + const [planEditState, setPlanEditState] = useState<{ draft: boolean; saved: boolean }>({ + draft: false, + saved: false + }); const canStartWorking = useMemo(() => { const isTodayPlan = @@ -139,7 +144,9 @@ export function AddTasksEstimationHoursModal(props: IAddTasksEstimationHoursModa // Update the plan work time only if the user changed it plan.workTimePlanned !== workTimePlanned && (await updateDailyPlan({ workTimePlanned }, plan.id ?? '')); - if (canStartWorking) { + setPlanEditState({ draft: false, saved: true }); + + if (canStartWorking && !timerStatus?.running) { handleChangeActiveTask(); if (isRenderedInSoftFlow) { @@ -152,14 +159,15 @@ export function AddTasksEstimationHoursModal(props: IAddTasksEstimationHoursModa setLoading(false); } }, [ + plan.workTimePlanned, + plan.id, + workTimePlanned, + updateDailyPlan, canStartWorking, + timerStatus?.running, handleChangeActiveTask, - handleCloseModal, - plan.id, - plan.workTimePlanned, isRenderedInSoftFlow, - updateDailyPlan, - workTimePlanned + handleCloseModal ]); /** @@ -235,7 +243,7 @@ export function AddTasksEstimationHoursModal(props: IAddTasksEstimationHoursModa const StartWorkingButton = ( + + + + + + ); +}