Skip to content

Commit

Permalink
feat(move): handle share updates for move goal out of shared goal wit…
Browse files Browse the repository at this point in the history
…h descendants
  • Loading branch information
vinaybadgujar102 committed Oct 26, 2024
1 parent 6005a8a commit 36b2660
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
16 changes: 8 additions & 8 deletions src/api/GoalsAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ export const unarchiveUserGoal = async (goal: GoalItem) => {
await unarchiveGoal(goal);
};

export const removeGoal = async (goal: GoalItem) => {
export const removeGoal = async (goal: GoalItem, permanently = false) => {
await deleteHintItem(goal.id);
await Promise.allSettled([
db.goalsCollection.delete(goal.id).catch((err) => console.log("failed to delete", err)),
addDeletedGoal(goal),
permanently ? null : addDeletedGoal(goal),
]);
};

export const removeChildrenGoals = async (parentGoalId: string) => {
export const removeChildrenGoals = async (parentGoalId: string, permanently = false) => {
const childrenGoals = await getChildrenGoals(parentGoalId);
if (childrenGoals.length === 0) {
return;
}
childrenGoals.forEach((goal) => {
removeChildrenGoals(goal.id);
removeGoal(goal);
removeChildrenGoals(goal.id, permanently);
removeGoal(goal, permanently);
});
};

Expand Down Expand Up @@ -312,9 +312,9 @@ export const notifyNewColabRequest = async (id: string, relId: string) => {
// });
// };

export const removeGoalWithChildrens = async (goal: GoalItem) => {
await removeChildrenGoals(goal.id);
await removeGoal(goal);
export const removeGoalWithChildrens = async (goal: GoalItem, permanently = false) => {
await removeChildrenGoals(goal.id, permanently);
await removeGoal(goal, permanently);
if (goal.parentGoalId !== "root") {
getGoal(goal.parentGoalId).then(async (parentGoal: GoalItem) => {
const parentGoalSublist = parentGoal.sublist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SubHeader from "@src/common/SubHeader";
import ContactItem from "@src/models/ContactItem";
import ZModal from "@src/common/ZModal";

import { addGoalToNewParentSublist, removeGoalFromParentSublist } from "@src/helpers/GoalController";
import { addGoalToNewParentSublist, getAllDescendants, removeGoalFromParentSublist } from "@src/helpers/GoalController";
import Header from "./Header";
import AcceptBtn from "./AcceptBtn";
import IgnoreBtn from "./IgnoreBtn";
Expand Down Expand Up @@ -157,13 +157,48 @@ const DisplayChangesModal = ({ currentMainGoal }: { currentMainGoal: GoalItem })
const removeChanges =
currentDisplay === "subgoals" || currentDisplay === "newGoalMoved"
? newGoals.map(({ goal }) => goal.id)
: [goalUnderReview.id];
: currentDisplay === "moved"
? [goalUnderReview.id, ...(await getAllDescendants(goalUnderReview.id)).map((goal: GoalItem) => goal.id)]
: [goalUnderReview.id];

if (currentDisplay !== "none") {
await deleteGoalChangesInID(currentMainGoal.id, participants[activePPT].relId, currentDisplay, removeChanges);
}
setCurrentDisplay("none");
};

const handleMoveChanges = async () => {
if (!goalUnderReview) {
console.log("No goal under review.");
return;
}
const parentGoal = await getGoal(goalUnderReview.parentGoalId);

if (!parentGoal || parentGoal.id === goalUnderReview.parentGoalId) {
await removeGoalWithChildrens(goalUnderReview, true)
.then(() => {
deleteChanges();
})
.then(() => {
setCurrentDisplay("none");
});
} else {
await Promise.all([
updateGoal(goalUnderReview.id, { parentGoalId: parentGoal.id }),
removeGoalFromParentSublist(goalUnderReview.id, oldParentTitle),
addGoalToNewParentSublist(goalUnderReview.id, parentGoal.id),
]);

// TODO: handle this later
// await sendUpdatedGoal(
// goalUnderReview.id,
// [],
// true,
// updatesIntent === "suggestion" ? [] : [participants[activePPT].relId],
// );
}
};

const acceptChanges = async () => {
if (!goalUnderReview) {
return;
Expand All @@ -172,26 +207,7 @@ const DisplayChangesModal = ({ currentMainGoal }: { currentMainGoal: GoalItem })
await deleteChanges();
}
if (currentDisplay === "moved") {
const parentGoal = await getGoal(goalUnderReview.parentGoalId);

if (!parentGoal) {
await deleteChanges().then(() => {
console.log("Goal moved to non-shared goal");
});
} else {
await Promise.all([
updateGoal(goalUnderReview.id, { parentGoalId: parentGoal.id }),
removeGoalFromParentSublist(goalUnderReview.id, oldParentTitle),
addGoalToNewParentSublist(goalUnderReview.id, parentGoal.id),
]);

await sendUpdatedGoal(
goalUnderReview.id,
[],
true,
updatesIntent === "suggestion" ? [] : [participants[activePPT].relId],
);
}
await handleMoveChanges();
}
if (currentDisplay === "subgoals" || currentDisplay === "newGoalMoved") {
const goalsToBeSelected = newGoals
Expand Down Expand Up @@ -259,6 +275,7 @@ const DisplayChangesModal = ({ currentMainGoal }: { currentMainGoal: GoalItem })
console.log("🚀 ~ getChanges ~ changedGoal:", changedGoal);
if (changedGoal) {
setGoalUnderReview({ ...changedGoal });
// TODO: remove the newGoalsMoved and try handle in subgoal only
if (typeAtPriority === "subgoals" || typeAtPriority === "newGoalMoved") {
setNewGoals(goals || []);
} else if (typeAtPriority === "modifiedGoals") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getMovedSubgoalsList = (
{newParentTitle === "Non-shared goal" && (
<div className="warning-message">
<InfoCircleOutlined />
<span>The new parent goal is not shared. Changes will be ignored if accepted.</span>
<span>The new parent goal is not shared. The goal will be deleted for you.</span>
</div>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/GoalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const getGoalAncestors = async (goalId: string): Promise<string[]> => {
return ancestors;
};

const getAllDescendants = async (goalId: string): Promise<GoalItem[]> => {
export const getAllDescendants = async (goalId: string): Promise<GoalItem[]> => {
const descendants: GoalItem[] = [];

const processGoalAndChildren = async (currentGoalId: string) => {
Expand Down

0 comments on commit 36b2660

Please sign in to comment.