From 8af2cb946333aa4e9e6e9e879931471c8ca5748a Mon Sep 17 00:00:00 2001 From: vinaybadgujar102 Date: Mon, 27 May 2024 19:53:56 +0530 Subject: [PATCH] add function to add and remove goalId from relevant goal's sublist --- src/helpers/GoalController.ts | 37 ++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/helpers/GoalController.ts b/src/helpers/GoalController.ts index 61f41b679..4101a6126 100644 --- a/src/helpers/GoalController.ts +++ b/src/helpers/GoalController.ts @@ -136,16 +136,31 @@ export const deleteSharedGoal = async (goal: GoalItem) => { } }; -export const moveGoalHierarchy = async (goal: GoalItem, parentGoalId: string) => { - await updateGoal(goal.id, { parentGoalId }); - if (goal.parentGoalId !== "root") { - getGoal(goal.parentGoalId).then(async (parentGoal: GoalItem) => { - const parentGoalSublist = parentGoal.sublist; - const childGoalIndex = parentGoalSublist.indexOf(goal.id); - if (childGoalIndex !== -1) { - parentGoalSublist.splice(childGoalIndex, 1); - } - await updateGoal(parentGoal.id, { sublist: parentGoalSublist }); - }); +const removeGoalFromParentSublist = async (goalId: string, parentGoalId: string) => { + const parentGoal = await getGoal(parentGoalId); + if (!parentGoal) return; + const parentGoalSublist = parentGoal.sublist; + const childGoalIndex = parentGoalSublist.indexOf(goalId); + if (childGoalIndex !== -1) { + parentGoalSublist.splice(childGoalIndex, 1); + } + await updateGoal(parentGoal.id, { sublist: parentGoalSublist }); +}; + +const addGoalIdToTargetGoalSublist = async (goalId: string, targetGoalId: string) => { + const targetGoal = await getGoal(targetGoalId); + if (!targetGoal) return; + const targetGoalSublist = targetGoal.sublist; + targetGoalSublist.push(goalId); + await updateGoal(targetGoal.id, { sublist: targetGoalSublist }); +}; + +export const moveGoalHierarchy = async (goalToMove: GoalItem, targetGoalId: string) => { + await updateGoal(goalToMove.id, { parentGoalId: targetGoalId }); + if (goalToMove.parentGoalId !== "root") { + await Promise.all([ + removeGoalFromParentSublist(goalToMove.id, goalToMove.parentGoalId), + addGoalIdToTargetGoalSublist(goalToMove.id, targetGoalId), + ]); } };