Skip to content

Commit

Permalink
feat: v1 of show scheduled status in edit modal
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaybadgujar102 committed Nov 7, 2024
1 parent ccd2a62 commit 841b075
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,8 @@
gap: 10px;
justify-content: center;
}

.schedule-status {
margin-top: 8px;
font-size: 14px;
}
65 changes: 65 additions & 0 deletions src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<ScheduleStatus>(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 (
<ZModal
open
Expand Down Expand Up @@ -415,6 +474,9 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
<button type="submit" className="action-btn place-middle gap-16">
{t(`${action} Budget`)}
</button>
{scheduleStatus && (
<div className={`schedule-status ${scheduleStatus}`}>{getScheduleStatusText(scheduleStatus)}</div>
)}
</div>
</>
) : (
Expand All @@ -425,6 +487,9 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
{t(`${action} Goal`)}
</button>
</div>
{scheduleStatus && (
<div className={`schedule-status ${scheduleStatus}`}>{getScheduleStatusText(scheduleStatus)}</div>
)}
<div className="place-middle justify-fs gap-16">
<span>{t("duration")}</span>
<input
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useScheduler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,28 @@ function useScheduler() {
}
}, [action]);

const checkGoalSchedule = async (goal: GoalItem) => {
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,
};
}

Expand Down

0 comments on commit 841b075

Please sign in to comment.