From 0d1491d4c6cd3958ca67010a3f0828f20efae19a Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Mon, 6 Jan 2025 11:53:35 +0500 Subject: [PATCH 1/3] fix(payload): correct payload structure for node updates --- .../EditNodeNameModal/Body/index.tsx | 21 ++++++++++--- src/stores/useDataStore/index.ts | 31 +++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx b/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx index 7b3f8c39a..e18d257a7 100644 --- a/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx +++ b/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx @@ -9,7 +9,7 @@ import { getTopicsData, putNodeData } from '~/network/fetchSourcesData' import { useDataStore } from '~/stores/useDataStore' import { useSelectedNode } from '~/stores/useGraphStore' import { useModal } from '~/stores/useModalStore' -import { NodeExtended, Topic } from '~/types' +import { NodeExtended, NodeRequest, Topic } from '~/types' import { colors } from '~/utils/colors' import { TitleEditor } from '../Title' @@ -86,14 +86,27 @@ export const Body = () => { const handleSave = async () => { setLoading(true) - const updatedData = getValues() + const formData = getValues() + + const nodeData = { + node_type: node?.node_type, + properties: { + name: formData.name?.trim(), + ...(formData.image_url && { image_url: formData.image_url }), + }, + ref_id: node?.ref_id, + } try { - await putNodeData(node?.ref_id || '', { node_type: node?.node_type, node_data: updatedData }) + await putNodeData(node?.ref_id || '', nodeData as unknown as NodeRequest) const { updateNode } = useDataStore.getState() - updateNode({ ...node, ...updatedData } as NodeExtended) + updateNode({ + ...node, + name: formData.name?.trim(), + ...(formData.image_url && { image_url: formData.image_url }), + } as NodeExtended) closeHandler() } catch (error) { diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index 40a61f453..e7a187b43 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -370,8 +370,35 @@ export const useDataStore = create()( setSources: (sources) => set({ sources }), setHideNodeDetails: (hideNodeDetails) => set({ hideNodeDetails }), setSeedQuestions: (questions) => set({ seedQuestions: questions }), - updateNode: (updatedNode) => { - console.info(updatedNode) + updateNode: (updatedNode: NodeExtended) => { + set((state) => { + const nodesNormalized = new Map(state.nodesNormalized) + + if (updatedNode.ref_id) { + const existingNode = nodesNormalized.get(updatedNode.ref_id) + + if (existingNode) { + nodesNormalized.set(updatedNode.ref_id, { + ...existingNode, + ...updatedNode, + }) + } + } + + const dataInitial = state.dataInitial + ? { + ...state.dataInitial, + nodes: state.dataInitial.nodes.map((node) => + node.ref_id === updatedNode.ref_id ? { ...node, ...updatedNode } : node, + ), + } + : null + + return { + nodesNormalized, + dataInitial, + } + }) }, removeNode: (id) => { From d8b67e1ab1fbace42d4447955efde49cb3fbcc89 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Mon, 6 Jan 2025 23:49:48 +0500 Subject: [PATCH 2/3] fix(store): node update logic in data store --- src/stores/useDataStore/index.ts | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index e7a187b43..55ba74f05 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -372,32 +372,22 @@ export const useDataStore = create()( setSeedQuestions: (questions) => set({ seedQuestions: questions }), updateNode: (updatedNode: NodeExtended) => { set((state) => { - const nodesNormalized = new Map(state.nodesNormalized) + if (!updatedNode.ref_id) { + return state + } - if (updatedNode.ref_id) { - const existingNode = nodesNormalized.get(updatedNode.ref_id) + const existingNode = state.nodesNormalized.get(updatedNode.ref_id) - if (existingNode) { - nodesNormalized.set(updatedNode.ref_id, { - ...existingNode, - ...updatedNode, - }) - } + if (!existingNode) { + return state } - const dataInitial = state.dataInitial - ? { - ...state.dataInitial, - nodes: state.dataInitial.nodes.map((node) => - node.ref_id === updatedNode.ref_id ? { ...node, ...updatedNode } : node, - ), - } - : null + state.nodesNormalized.set(updatedNode.ref_id, { + ...existingNode, + ...updatedNode, + }) - return { - nodesNormalized, - dataInitial, - } + return state }) }, From ddc31094de0fed4f9eb329060db18d6d4f28bf62 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Tue, 7 Jan 2025 08:08:18 +0500 Subject: [PATCH 3/3] fix(node): update node data payload and store state management --- .../EditNodeNameModal/Body/index.tsx | 10 ++++----- src/stores/useDataStore/index.ts | 21 +++++-------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx b/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx index e18d257a7..57142a4df 100644 --- a/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx +++ b/src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx @@ -86,13 +86,13 @@ export const Body = () => { const handleSave = async () => { setLoading(true) - const formData = getValues() + const updatedData = getValues() const nodeData = { node_type: node?.node_type, properties: { - name: formData.name?.trim(), - ...(formData.image_url && { image_url: formData.image_url }), + name: updatedData.name, + ...(updatedData.image_url && { image_url: updatedData.image_url }), }, ref_id: node?.ref_id, } @@ -104,8 +104,8 @@ export const Body = () => { updateNode({ ...node, - name: formData.name?.trim(), - ...(formData.image_url && { image_url: formData.image_url }), + name: updatedData.name, + ...(updatedData.image_url && { image_url: updatedData.image_url }), } as NodeExtended) closeHandler() diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index 55ba74f05..27bdd7023 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -370,25 +370,14 @@ export const useDataStore = create()( setSources: (sources) => set({ sources }), setHideNodeDetails: (hideNodeDetails) => set({ hideNodeDetails }), setSeedQuestions: (questions) => set({ seedQuestions: questions }), - updateNode: (updatedNode: NodeExtended) => { - set((state) => { - if (!updatedNode.ref_id) { - return state - } - - const existingNode = state.nodesNormalized.get(updatedNode.ref_id) + updateNode: (updatedNode) => { + const { nodesNormalized } = get() - if (!existingNode) { - return state - } + const newNodesNormalized = new Map(nodesNormalized) - state.nodesNormalized.set(updatedNode.ref_id, { - ...existingNode, - ...updatedNode, - }) + newNodesNormalized.set(updatedNode.ref_id, updatedNode) - return state - }) + set({ nodesNormalized: newNodesNormalized }) }, removeNode: (id) => {