diff --git a/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.scss b/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.scss index ed1f3d62c..d2e654010 100644 --- a/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.scss +++ b/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.scss @@ -156,3 +156,8 @@ gap: 10px; justify-content: center; } + +.schedule-status { + margin-top: 8px; + font-size: 14px; +} diff --git a/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx b/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx index 56541b32f..cea958c67 100644 --- a/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx +++ b/src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx @@ -21,6 +21,8 @@ import { ILocationState } from "@src/Interfaces"; import { useLocation, useNavigate } from "react-router-dom"; import { suggestedGoalState } from "@src/store/SuggestedGoalState"; import { getHistoryUptoGoal } from "@src/helpers/GoalProcessor"; +import { ISchedulerOutput } from "@src/Interfaces/IScheduler"; +import useScheduler from "@src/hooks/useScheduler"; import { colorPalleteList, calDays, convertOnFilterToArray, getSelectedLanguage } from "../../../utils"; import "./ConfigGoal.scss"; @@ -277,8 +279,65 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf setHintOption(hint?.hintOptionEnabled || false); }; + const { checkGoalSchedule } = useScheduler(); + const titlePlaceholder = t(`${type !== "Budget" ? "goal" : "budget"}Title`); + type ScheduleStatus = "pending" | "scheduled" | "impossible" | "future" | null; + + const [scheduleStatus, setScheduleStatus] = useState(null); + + const checkSchedulingStatus = async (schedulerOutput: ISchedulerOutput | undefined, goalId: string) => { + if (!schedulerOutput) return "pending"; + + const { scheduled, impossible } = schedulerOutput; + + if (impossible?.some((task) => task.id === goalId)) { + return "impossible"; + } + + const isScheduledInNext7Days = scheduled.some((day) => day.tasks.some((task) => task.goalid === goalId)); + + return isScheduledInNext7Days ? "scheduled" : "future"; + }; + + const getScheduleStatusText = (status: ScheduleStatus) => { + switch (status) { + case "scheduled": + return "Auto scheduled"; + case "impossible": + return "! Impossible"; + case "future": + return "Scheduled someday"; + default: + return ""; + } + }; + + const checkSchedule = async () => { + // if (t) { + // setScheduleStatus(null); + // console.log("no duration"); + // return; + // } + + setScheduleStatus("pending"); + const schedulerOutput = await checkGoalSchedule(getFinalTags()); + console.log("schedulerOutput", schedulerOutput); + const status = await checkSchedulingStatus(schedulerOutput, goal.id); + console.log("status", status); + setScheduleStatus(status); + }; + + useEffect(() => { + const debounceTimer = setTimeout(() => { + console.log("checking schedule"); + checkSchedule(); + }, 1000); + + return () => clearTimeout(debounceTimer); + }, [tags.duration, afterTime, beforeTime, tags.on]); + return ( {t(`${action} Budget`)} + {scheduleStatus && ( +
{getScheduleStatusText(scheduleStatus)}
+ )} ) : ( @@ -425,6 +487,9 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf {t(`${action} Goal`)} + {scheduleStatus && ( +
{getScheduleStatusText(scheduleStatus)}
+ )}
{t("duration")} { + try { + const activeGoals: GoalItem[] = await getAllGoals(); + const goalsWithConfig = [...activeGoals, goal]; + + const { schedulerInput } = await organizeDataForInptPrep(goalsWithConfig); + + await init(); + const res = schedule(schedulerInput); + + return res; + } catch (error) { + console.log("Error checking goal schedule:", error); + return undefined; + } + }; + return { tasks, tasksStatus, setTasksStatus, + checkGoalSchedule, }; }