Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Updating a Node Should Render Changes on the Frontend Immediately #1275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import { validateImageInputType } from '~/components/ModalsContainer/EditNodeNameModal/utils'
import { Flex } from '~/components/common/Flex'
import { getTopicsData, putNodeData } from '~/network/fetchSourcesData'
import { useSelectedNode } from '~/stores/useDataStore'
import { useDataStore, useSelectedNode } from '~/stores/useDataStore'
import { useModal } from '~/stores/useModalStore'
import { Topic } from '~/types'
import { NodeExtended, Topic } from '~/types'
import { colors } from '~/utils/colors'
import { TitleEditor } from '../Title'

Expand Down Expand Up @@ -61,7 +61,7 @@

setActualTopicNode(node)
} catch (error) {
console.log(error)

Check warning on line 64 in src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 64 in src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement

Check warning on line 64 in src/components/ModalsContainer/EditNodeNameModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement
} finally {
setTopicIsLoading(false)
}
Expand Down Expand Up @@ -90,9 +90,14 @@
setLoading(true)

const propName = 'name'
const updatedData = { [propName]: topicValue.trim(), image_url: imageUrl.trim() }

try {
await putNodeData(node?.ref_id || '', { [propName]: topicValue.trim(), image_url: imageUrl.trim() })
await putNodeData(node?.ref_id || '', updatedData)

const { updateNode } = useDataStore.getState()

updateNode({ ...node, ...updatedData } as NodeExtended)

closeHandler()
} catch (error) {
Expand Down
25 changes: 25 additions & 0 deletions src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type DataStore = {
setHideNodeDetails: (_: boolean) => void
setTeachMe: (_: boolean) => void
addNewNode: (node: NodeExtended) => void
updateNode: (updatedNode: NodeExtended) => void
removeNode: (id: string) => void
setSidebarFilterCounts: (filterCounts: SidebarFilterWithCount[]) => void
}
Expand Down Expand Up @@ -106,6 +107,7 @@ const defaultData: Omit<
| 'setHideNodeDetails'
| 'setTeachMe'
| 'addNewNode'
| 'updateNode'
| 'removeNode'
> = {
categoryFilter: null,
Expand Down Expand Up @@ -219,6 +221,29 @@ export const useDataStore = create<DataStore>()(
setShowSelectionGraph: (showSelectionGraph) => set({ showSelectionGraph }),
setHideNodeDetails: (hideNodeDetails) => set({ hideNodeDetails }),
setTeachMe: (showTeachMe) => set({ showTeachMe }),
updateNode: (updatedNode) => {
set((state) => {
const nodes = state.data?.nodes || []
const links = state.data?.links || []

const updatedNodes = nodes.map((node) => {
if (node.ref_id === updatedNode.ref_id) {
return { ...node, ...updatedNode }
}

return node
})

return {
...state,
data: {
...state.data,
nodes: updatedNodes,
links,
},
}
})
},
addNewNode: (node) => {
const { data } = get()

Expand Down
Loading