Skip to content

Commit

Permalink
fix: location history problem while try to add suggested subgoal
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaybadgujar102 committed Oct 14, 2024
1 parent 6167b99 commit 395f280
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { useParentGoalContext } from "@src/contexts/parentGoal-context";
import useGoalActions from "@src/hooks/useGoalActions";
import useGoalStore from "@src/hooks/useGoalStore";
import { unarchiveUserGoal } from "@src/api/GoalsAPI";
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 { colorPalleteList, calDays, convertOnFilterToArray, getSelectedLanguage } from "../../../utils";

import "./ConfigGoal.scss";
Expand All @@ -41,6 +44,9 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
parentData: { parentGoal },
} = useParentGoalContext();

const location = useLocation();
const navigate = useNavigate();

let defaultColorIndex = Math.floor(Math.random() * colorPalleteList.length - 1) + 1;
let defaultAfterTime = isEditMode ? goal.afterTime || 9 : 9;
let defaultBeforeTime = isEditMode ? goal.beforeTime || 18 : 18;
Expand Down Expand Up @@ -153,7 +159,12 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
}
if (suggestedGoal) {
await unarchiveUserGoal(suggestedGoal);
navigate(`/goals/${suggestedGoal.parentGoalId === "root" ? "" : suggestedGoal.parentGoalId}`, {
state: { ...location.state },
replace: true,
});
setSuggestedGoal(null);
return;
}
window.history.back();
};
Expand Down Expand Up @@ -217,15 +228,17 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
handleWeekSliderChange(perWeekHrs);
}, [perDayHrs, setPerDayHrs, tags.on]);

// const modalStyle = {
// transform: `translate(0, ${isKeyboardOpen ? "-45%" : "0"})`,
// transition: "transform 0.3s ease-in-out",
// };

const { openEditMode } = useGoalStore();

const onSuggestionClick = async (selectedGoal: GoalItem) => {
await openEditMode(selectedGoal);
const updatedGoalsHistory = await getHistoryUptoGoal(selectedGoal.parentGoalId);

const newState: ILocationState = {
...location.state,
goalsHistory: updatedGoalsHistory,
};

openEditMode(selectedGoal, newState);
setSuggestedGoal(selectedGoal);
setTitle(selectedGoal.title);
setColorIndex(colorPalleteList.indexOf(selectedGoal.goalColor));
Expand Down
17 changes: 11 additions & 6 deletions src/hooks/useGoalStore.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { useRecoilValue } from "recoil";
import { GoalItem, TGoalCategory } from "@src/models/GoalItem";
import { GoalItem } from "@src/models/GoalItem";
import { displayConfirmation } from "@src/store";
import { ILocationState } from "@src/Interfaces";

const useGoalStore = () => {
const { partnerId } = useParams();
const navigate = useNavigate();
const location = useLocation();
const showConfirmation = useRecoilValue(displayConfirmation);

const openEditMode = (goal: GoalItem) => {
const openEditMode = (goal: GoalItem, customState?: ILocationState) => {
const prefix = `${partnerId ? `/partners/${partnerId}/` : "/"}goals`;

const newState = {
...location.state,
goalType: goal.category === "Budget" ? "Budget" : "Goal",
...customState,
};

navigate(`${prefix}/${goal.parentGoalId}/${goal.id}?type=${goal.category}&mode=edit`, {
state: {
...location.state,
goalType: goal.category === "Budget" ? "Budget" : "Goal",
},
state: newState,
replace: true,
});
};
Expand Down

0 comments on commit 395f280

Please sign in to comment.