Skip to content

Commit

Permalink
fix(render-changes): updating a node on the frontend immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Apr 17, 2024
1 parent 93905f5 commit af03fe9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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 styled from 'styled-components'
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 @@ -90,9 +90,14 @@ export const Body = () => {
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

0 comments on commit af03fe9

Please sign in to comment.