From 2d358c979e442b221f47de33141a722accb8d12d Mon Sep 17 00:00:00 2001 From: victor barbier Date: Thu, 21 Dec 2023 11:59:41 +0100 Subject: [PATCH] fix(layout): reset page when datasource change --- client/src/layout/Graph.jsx | 8 ++--- client/src/pages/home.jsx | 60 +++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/client/src/layout/Graph.jsx b/client/src/layout/Graph.jsx index f2cb7d37..f2be353d 100644 --- a/client/src/layout/Graph.jsx +++ b/client/src/layout/Graph.jsx @@ -56,21 +56,21 @@ const highlightGraph = (graph, selectedNode) => { return graph; }; -export default function Graph({ data, selectedOption }) { +export default function Graph({ data, selectedGraph }) { console.log('data', data); const { publications, structures } = data; const [selectedNode, setSelectedNode] = useState(null); - console.log('selectedOption', selectedOption); - const graph = UndirectedGraph.from(data.graph[selectedOption]); + console.log('selectedGraph', selectedGraph); + const graph = UndirectedGraph.from(data.graph[selectedGraph]); // Return alert if graph empty if (graph.order === 0) { return ; } - const communities = groupBy(data.graph[selectedOption].nodes, ({ attributes }) => attributes.community); + const communities = groupBy(data.graph[selectedGraph].nodes, ({ attributes }) => attributes.community); // console.log('communities', communities); // Update nodes color diff --git a/client/src/pages/home.jsx b/client/src/pages/home.jsx index 0e6ee564..cbffd59d 100644 --- a/client/src/pages/home.jsx +++ b/client/src/pages/home.jsx @@ -59,6 +59,8 @@ export default function Home() { const [formEndYear, setFormEndYear] = useState(searchParams.getAll('endyear')[0] || 2023); const [formCountries, setFormCountries] = useState(searchParams.getAll('countries') || ['FR']); const [isError, setFormIsError] = useState(false); + const [displayGraph, setDisplayGraph] = useState(false); + const [selectedGraph, setSelectedGraph] = useState('main'); useEffect( () => setSearchParams({ @@ -156,8 +158,12 @@ export default function Home() { }, ]; - const graphOptions = Object.keys(data?.graph || { main: 0 }); - const [selectedOption, setSelectedOption] = useState(graphOptions[0]); + useEffect(() => { + if (data?.graph) { + setSelectedGraph(Object.keys(data.graph)[0]); + setDisplayGraph(true); + } + }, [data, setSelectedGraph, setDisplayGraph]); return ( @@ -175,9 +181,11 @@ export default function Home() { selected={formDatasource} onChange={(e) => { setFormDatasource(e.target.value); - // setFormType('keyword'); - // setFormQueries([]); - // setFormCondition('OR') + setSelectedGraph('main'); + setDisplayGraph(false); + setFormType('keyword'); + setFormQueries([]); + setFormCondition('OR'); }} /> {formDatasource !== 'scanr-agg' && ( @@ -295,39 +303,41 @@ export default function Home() { closable /> )} - {!isFetching && data?.graph && ( + {!isFetching && displayGraph && ( - {(graphOptions.length > 1) && ( + {(Object.keys(data.graph).length > 1) && ( - {graphOptions.map((option) => ( + {Object.keys(data.graph).map((option) => ( setSelectedOption(event.target.value)} + defaultChecked={option === Object.keys(data.graph)[0]} + onChange={(event) => setSelectedGraph(event.target.value)} /> ))} )} - - - - - -
- -
- -
+ {(selectedGraph in data.graph) && ( + + + + + +
+ +
+ +
+ )}