1
1
import { useEffect , useState , useRef } from "react" ;
2
2
import classNames from "classnames" ;
3
- import "../../../../App.css" ;
4
- import { useNotification } from "../../../../components/NotificationProvider" ;
5
- import { fetchAssembliesByTaxonIDs , fetchTaxonTree } from "../../../../api" ;
6
- import SpeciesProfilePictureViewer from "../../../../components/SpeciesProfilePictureViewer" ;
3
+ import "../../../../../../ App.css" ;
4
+ import { useNotification } from "../../../../../../ components/NotificationProvider" ;
5
+ import { fetchTaxonTree } from "../../ ../../../../api" ;
6
+ import SpeciesProfilePictureViewer from "../../../../../../ components/SpeciesProfilePictureViewer" ;
7
7
import { Expand , Vulnerability } from "grommet-icons" ;
8
8
9
9
import Tree from "react-d3-tree" ;
10
- import LoadingSpinner from "../../../../components/LoadingSpinner" ;
11
- import Button from "../../../../components/Button" ;
12
- import AssembliesGridElement from "../AssembliesList/components/ AssembliesGridElement" ;
10
+ import LoadingSpinner from "../../../../../../ components/LoadingSpinner" ;
11
+ import Button from "../../../../../../ components/Button" ;
12
+ import AssembliesGridElement from "../AssembliesGridElement" ;
13
13
14
- const AssembliesTreeViewer = ( ) => {
14
+ const AssembliesTreeViewer = ( { filter , setFilter , assemblies , loading } ) => {
15
15
const [ tree , setTree ] = useState ( { } ) ;
16
16
const [ fullTree , setFullTree ] = useState ( { } ) ;
17
- const [ taxa , setTaxa ] = useState ( [ ] ) ;
18
17
const [ height , setHeight ] = useState ( 0 ) ;
19
18
const [ width , setWidth ] = useState ( 0 ) ;
20
19
const [ expandTree , setExpandTree ] = useState ( false ) ;
21
20
const [ loadingTree , setLoadingTree ] = useState ( false ) ;
22
- const [ showElements , setShowElements ] = useState ( 10 ) ;
23
- const [ currentNode , setCurrentNode ] = useState ( "" ) ;
21
+ const [ currentNode , setCurrentNode ] = useState ( "root" ) ;
24
22
const ref = useRef ( null ) ;
25
23
const cardsRef = useRef ( null ) ;
26
24
@@ -31,6 +29,12 @@ const AssembliesTreeViewer = () => {
31
29
// eslint-disable-next-line react-hooks/exhaustive-deps
32
30
} , [ ] ) ;
33
31
32
+ useEffect ( ( ) => {
33
+ if ( ! filter || ! filter . taxonIDs ) {
34
+ setCurrentNode ( "root" ) ;
35
+ }
36
+ } , [ filter ] ) ;
37
+
34
38
// notifications
35
39
const dispatch = useNotification ( ) ;
36
40
@@ -83,18 +87,9 @@ const AssembliesTreeViewer = () => {
83
87
} ;
84
88
85
89
const loadTaxa = async ( nodeDatum ) => {
86
- const userID = JSON . parse ( sessionStorage . getItem ( "userID" ) || "" ) ;
87
- const token = JSON . parse ( sessionStorage . getItem ( "token" ) || "" ) ;
88
-
89
- const response = await fetchAssembliesByTaxonIDs ( getChildrenTaxIds ( nodeDatum ) , userID , token ) ;
90
-
91
- if ( response && response . payload ) {
92
- setTaxa ( response . payload ) ;
93
- }
94
-
95
- if ( response && response . notification && response . notification . length > 0 ) {
96
- response . notification . map ( ( not ) => handleNewNotification ( not ) ) ;
97
- }
90
+ setFilter ( ( prevState ) => {
91
+ return { ...prevState , taxonIDs : getChildrenTaxIds ( nodeDatum ) . sort ( ) } ;
92
+ } ) ;
98
93
} ;
99
94
100
95
const getChildrenTaxIds = ( nodeDatum ) => {
@@ -139,7 +134,6 @@ const AssembliesTreeViewer = () => {
139
134
onClick = { ( ) => {
140
135
loadTaxa ( nodeDatum ) ;
141
136
executeScroll ( ) ;
142
- setShowElements ( 10 ) ;
143
137
setCurrentNode ( nodeDatum . name ) ;
144
138
// toggleNode();
145
139
} }
@@ -231,65 +225,37 @@ const AssembliesTreeViewer = () => {
231
225
</ div >
232
226
< div className = "mt-8 min-h-1/2" ref = { cardsRef } >
233
227
< div className = "flex" >
234
- < div className = "w-full bg-gray-500 rounded-lg px-4 p-2 text-white font-bold mb-4" >
228
+ < div className = "w-full bg-gray-500 rounded-lg px-4 p-2 text-white font-bold mb-4 flex " >
235
229
Results:
230
+ { loading && (
231
+ < div className = "px-4" >
232
+ < LoadingSpinner label = "Loading..." />
233
+ </ div >
234
+ ) }
236
235
</ div >
237
236
< div className = "w-32 ml-4" >
238
237
< Button label = "Scroll to top" color = "secondary" onClick = { ( ) => executeScrollTree ( ) } />
239
238
</ div >
240
239
</ div >
241
- { taxa && taxa . length > 0 ? (
240
+ { assemblies && assemblies . length > 0 ? (
242
241
< div >
243
242
< div className = "ml-12" >
244
243
< span > Last common ancestor:</ span >
245
244
< span className = "font-bold text-xl ml-2" > { currentNode } </ span >
246
245
</ div >
247
246
< hr className = "m-8 mt-2 shadow" />
248
247
< div className = "animate-grow-y rounded-lg grid gap-8 grid-cols-2 mt-8 px-8" >
249
- { taxa . map ( ( assembly , index ) => {
250
- if ( index < showElements ) {
251
- return (
252
- < div key = { assembly . id } className = "animate-fade-in" >
253
- < AssembliesGridElement
254
- assembly = { assembly }
255
- fcatMode = { 1 }
256
- renderDelay = { index + 1 }
257
- />
258
- </ div >
259
- ) ;
260
- } else {
261
- return < div key = { assembly . id } /> ;
262
- }
263
- } ) }
264
- </ div >
265
- < hr className = "m-8 shadow" />
266
- < div className = "flex justify-around" >
267
- { showElements > 10 && (
268
- < div className = "mt-2" >
269
- < div className = "flex justify-center" >
270
- < div className = "w-32" >
271
- < Button
272
- color = "nav"
273
- label = "View less..."
274
- onClick = { ( ) => setShowElements ( ( prevState ) => prevState - 10 ) }
275
- />
276
- </ div >
277
- </ div >
278
- </ div >
279
- ) }
280
- { showElements < taxa . length && (
281
- < div className = "mt-2" >
282
- < div className = "flex justify-center" >
283
- < div className = "w-32" >
284
- < Button
285
- color = "nav"
286
- label = "View more..."
287
- onClick = { ( ) => setShowElements ( ( prevState ) => prevState + 10 ) }
288
- />
289
- </ div >
248
+ { assemblies . map ( ( assembly , index ) => {
249
+ return (
250
+ < div key = { assembly . id } className = "animate-fade-in" >
251
+ < AssembliesGridElement
252
+ assembly = { assembly }
253
+ fcatMode = { 1 }
254
+ renderDelay = { index + 1 }
255
+ />
290
256
</ div >
291
- </ div >
292
- ) }
257
+ ) ;
258
+ } ) }
293
259
</ div >
294
260
</ div >
295
261
) : (
0 commit comments