diff --git a/src/Interfaces/IContactMessages.ts b/src/Interfaces/IContactMessages.ts new file mode 100644 index 000000000..0a1d1c04b --- /dev/null +++ b/src/Interfaces/IContactMessages.ts @@ -0,0 +1,15 @@ +import { GoalItem } from "@src/models/GoalItem"; + +export interface SharedGoalMessage { + relId: string; + goalWithChildrens: GoalItem[]; + lastProcessedTimestamp: string; + type: "shareMessage"; + installId: string; + TTL: number; +} + +export interface SharedGoalMessageResponse { + success: boolean; + response: SharedGoalMessage[]; +} diff --git a/src/Interfaces/IPopupModals.ts b/src/Interfaces/IPopupModals.ts index d5a368b8d..b8382b095 100644 --- a/src/Interfaces/IPopupModals.ts +++ b/src/Interfaces/IPopupModals.ts @@ -9,6 +9,7 @@ export interface TConfirmActionState { addHint: boolean; deleteHint: boolean; reportHint: boolean; + move: boolean; }; collaboration: { colabRequest: boolean; @@ -20,7 +21,7 @@ export interface TConfirmActionState { export interface TConfirmGoalAction { actionCategory: "goal"; - actionName: "archive" | "delete" | "shareAnonymously" | "shareWithOne" | "restore"; + actionName: "archive" | "delete" | "shareAnonymously" | "shareWithOne" | "restore" | "move"; } export interface TConfirmColabGoalAction { diff --git a/src/api/GoalsAPI/index.ts b/src/api/GoalsAPI/index.ts index b0cd37cae..73241e145 100644 --- a/src/api/GoalsAPI/index.ts +++ b/src/api/GoalsAPI/index.ts @@ -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); }); }; @@ -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; diff --git a/src/api/SharedWMAPI/index.ts b/src/api/SharedWMAPI/index.ts index 00818799c..62e59a69a 100644 --- a/src/api/SharedWMAPI/index.ts +++ b/src/api/SharedWMAPI/index.ts @@ -2,7 +2,8 @@ import { db } from "@models"; import { GoalItem } from "@src/models/GoalItem"; import { createGoalObjectFromTags } from "@src/helpers/GoalProcessor"; -import { addDeletedGoal, addGoal } from "../GoalsAPI"; +import { addGoal } from "../GoalsAPI"; +import { getContactByRelId } from "../ContactsAPI"; export const addSharedWMSublist = async (parentGoalId: string, goalIds: string[]) => { db.transaction("rw", db.sharedWMCollection, async () => { @@ -17,29 +18,53 @@ export const addSharedWMSublist = async (parentGoalId: string, goalIds: string[] }); }; -export const addSharedWMGoal = async (goalDetails: object) => { +export const addSharedWMGoal = async (goalDetails: GoalItem, relId = "") => { + console.log("[addSharedWMGoal] Input goal details:", goalDetails); + console.log("[addSharedWMGoal] Input relId:", relId); + const { participants } = goalDetails; - const newGoal = createGoalObjectFromTags({ ...goalDetails, typeOfGoal: "shared" }); - if (participants) newGoal.participants = participants; + let updatedParticipants = participants || []; + + if (relId) { + const contact = await getContactByRelId(relId); + if (contact) { + const contactExists = updatedParticipants.some((p) => p.relId === relId); + if (!contactExists) { + updatedParticipants = [...updatedParticipants, { ...contact, following: true, type: "sharer" }]; + } + } + } + + console.log("[addSharedWMGoal] Updated participants:", updatedParticipants); + const newGoal = createGoalObjectFromTags({ + ...goalDetails, + typeOfGoal: "shared", + participants: updatedParticipants, + }); + await db .transaction("rw", db.sharedWMCollection, async () => { await db.sharedWMCollection.add(newGoal); + console.log("[addSharedWMGoal] Goal added to sharedWMCollection"); }) .then(async () => { const { parentGoalId } = newGoal; if (parentGoalId !== "root") { + console.log("[addSharedWMGoal] Adding goal to parent sublist. ParentId:", parentGoalId); await addSharedWMSublist(parentGoalId, [newGoal.id]); } }) .catch((e) => { - console.log(e.stack || e); + console.error("[addSharedWMGoal] Error:", e.stack || e); }); + + console.log("[addSharedWMGoal] Successfully created goal with ID:", newGoal.id); return newGoal.id; }; -export const addGoalsInSharedWM = async (goals: GoalItem[]) => { +export const addGoalsInSharedWM = async (goals: GoalItem[], relId: string) => { goals.forEach((ele) => { - addSharedWMGoal(ele).then((res) => console.log(res, "added")); + addSharedWMGoal(ele, relId).then((res) => console.log(res, "added")); }); }; @@ -83,7 +108,7 @@ export const getRootGoalsOfPartner = async (relId: string) => { ).reverse(); }; -export const updateSharedWMGoal = async (id: string, changes: object) => { +export const updateSharedWMGoal = async (id: string, changes: Partial) => { db.transaction("rw", db.sharedWMCollection, async () => { await db.sharedWMCollection.update(id, changes).then((updated) => updated); }).catch((e) => { @@ -157,3 +182,19 @@ export const convertSharedWMGoalToColab = async (goal: GoalItem) => { }) .catch((err) => console.log(err)); }; + +export const updateSharedWMParentSublist = async (oldParentId: string, newParentId: string, goalId: string) => { + // Remove from old parent + const oldParentGoal = await getSharedWMGoal(oldParentId); + if (oldParentGoal?.sublist) { + const updatedOldSublist = oldParentGoal.sublist.filter((id) => id !== goalId); + await updateSharedWMGoal(oldParentId, { sublist: updatedOldSublist }); + } + + // Add to new parent + const newParentGoal = await getSharedWMGoal(newParentId); + if (newParentGoal) { + const updatedNewSublist = [...(newParentGoal.sublist || []), goalId]; + await updateSharedWMGoal(newParentId, { sublist: updatedNewSublist }); + } +}; diff --git a/src/common/ConfirmationModal.tsx b/src/common/ConfirmationModal.tsx index 4520ede48..a7120438d 100644 --- a/src/common/ConfirmationModal.tsx +++ b/src/common/ConfirmationModal.tsx @@ -1,7 +1,7 @@ import { Checkbox } from "antd"; import React, { useState } from "react"; import { useRecoilState, useRecoilValue } from "recoil"; -import { useTranslation } from "react-i18next"; +import { Trans, useTranslation } from "react-i18next"; import { darkModeState, displayConfirmation } from "@src/store"; import { ConfirmationModalProps } from "@src/Interfaces/IPopupModals"; @@ -51,7 +51,7 @@ const ConfirmationModal: React.FC = ({ action, handleCli {t(headerKey)}

- {t("note")}: {t(noteKey)} + {t("note")}: , ul: