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

Delete node on the frontend should remove it from the graph #1311

Merged
merged 4 commits into from
Apr 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
setActualNode(selectedNode)
}
} catch (error) {
console.log(error)

Check warning on line 54 in src/components/ModalsContainer/RemoveNodeModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement

Check warning on line 54 in src/components/ModalsContainer/RemoveNodeModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 54 in src/components/ModalsContainer/RemoveNodeModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement
} finally {
setTopicIsLoading(false)
}
Expand Down Expand Up @@ -88,16 +88,18 @@

setLoading(true)

const selectedNodeId = selectedNode?.ref_id as string

try {
await deleteNode(refId)

removeNode(refId)
removeNode(selectedNodeId)
setSelectedNode(null)

closeHandler()
closeEditNodeModal()
} catch (error) {
console.log(error)

Check warning on line 102 in src/components/ModalsContainer/RemoveNodeModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement

Check warning on line 102 in src/components/ModalsContainer/RemoveNodeModal/Body/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

console.warn(error)
} finally {
Expand Down
7 changes: 3 additions & 4 deletions src/components/Universe/Graph/Cubes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Select } from '@react-three/drei'
import { ThreeEvent } from '@react-three/fiber'
import { memo, useCallback } from 'react'
import { Object3D } from 'three'
import { useGraphData } from '~/components/DataRetriever'
import { useAppStore } from '~/stores/useAppStore'
import { useDataStore, useSelectedNode } from '~/stores/useDataStore'
import { useDataStore, useSelectedNode, useUpdateGraphData } from '~/stores/useDataStore'
import { NodeExtended } from '~/types'
import { BlurryInstances } from './BlurryInstances'
import { Cube } from './Cube'
Expand All @@ -14,7 +13,7 @@ import { TextNode } from './Text'
import { isMainTopic } from './constants'

export const Cubes = memo(() => {
const data = useGraphData()
const data = useUpdateGraphData()
const selectedNode = useSelectedNode()
const nearbyNodeIds = useDataStore((s) => s.nearbyNodeIds)
const setHoveredNode = useDataStore((s) => s.setHoveredNode)
Expand Down Expand Up @@ -87,7 +86,7 @@ export const Cubes = memo(() => {
>
<BlurryInstances hide={hideUniverse} />
<RelevanceBadges />
{data.nodes
{data?.nodes
.filter((f) => {
const isSelected = f?.ref_id === selectedNode?.ref_id
const isNearbyOrPersistent = nearbyNodeIds.includes(f.ref_id || '') || isMainTopic(f)
Expand Down
33 changes: 33 additions & 0 deletions src/stores/useDataStore/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,37 @@ describe('useDataStore', () => {
it('should return views in correct order', () => {
expect(graphStyles).toEqual(['sphere', 'force', 'split', 'earth'])
})

it('removes node correctly', () => {
const initialData = {
nodes: [
{ id: 'node_1', ref_id: 'node_1' },
{ id: 'node_2', ref_id: 'node_2' },
{ id: 'node_3', ref_id: 'node_3' },
],
links: [{ source: '1', target: '3' }],
}

const { removeNode, setData } = useDataStore.getState()

// Set initial data
setData(initialData)

// Remove node with id '2'
removeNode('node_2')

// Get updated state data
const updatedData = useDataStore.getState().data

// Expected result after removing node '2'
const expectedData = {
nodes: [
{ id: 'node_1', ref_id: 'node_1' },
{ id: 'node_3', ref_id: 'node_3' },
],
links: [{ source: '1', target: '3' }],
}

expect(updatedData).toEqual(expectedData)
})
})
8 changes: 7 additions & 1 deletion src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export const useDataStore = create<DataStore>()(

const removeData = {
...data,
nodes: data.nodes.filter((el) => el.ref_id !== id && el.id !== id && el.id !== id && el.ref_id !== id),
nodes: data.nodes.filter((el) => el.ref_id !== id && el.id !== id),
}

set({ data: removeData })
Expand All @@ -284,3 +284,9 @@ export const useFilteredNodes = () =>
s.sidebarFilter === 'Other' ? !i.node_type : i.node_type === s.sidebarFilter,
)
})

export const useUpdateGraphData = () => {
const data = useDataStore((state) => state.data)

return data
}
Loading