Skip to content

Commit

Permalink
fix(layout): reset page when datasource change
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonestla committed Dec 21, 2023
1 parent 8dee5e9 commit 2d358c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
8 changes: 4 additions & 4 deletions client/src/layout/Graph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Alert title="No results found" description="Your query returned no results" type="warning" closable />;
}

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
Expand Down
60 changes: 35 additions & 25 deletions client/src/pages/home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 (
<Container className="fr-my-15w">
Expand All @@ -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' && (
Expand Down Expand Up @@ -295,39 +303,41 @@ export default function Home() {
closable
/>
)}
{!isFetching && data?.graph && (
{!isFetching && displayGraph && (
<Container className="fr-mt-2w">
{(graphOptions.length > 1) && (
{(Object.keys(data.graph).length > 1) && (
<RadioGroup
isInline
label={selectedOption}
label={selectedGraph}
>
{graphOptions.map((option) => (
{Object.keys(data.graph).map((option) => (
<Radio
label={option}
value={option}
defaultChecked={option === selectedOption}
onChange={(event) => setSelectedOption(event.target.value)}
defaultChecked={option === Object.keys(data.graph)[0]}
onChange={(event) => setSelectedGraph(event.target.value)}
/>
))}
</RadioGroup>
)}
<Row gutters>
<Col>
<Graph data={data} selectedOption={selectedOption} />
</Col>
<Col>
<div key={selectedOption} style={{ height: '400px' }}>
<VOSviewerOnline
data={graphEncodeToJson(data.graph[selectedOption])}
parameters={{ attraction: 1, largest_component: false, simple_ui: true }}
/>
</div>
</Col>
</Row>
{(selectedGraph in data.graph) && (
<Row gutters>
<Col>
<Graph data={data} selectedGraph={selectedGraph} />
</Col>
<Col>
<div key={selectedGraph} style={{ height: '400px' }}>
<VOSviewerOnline
data={graphEncodeToJson(data.graph[selectedGraph])}
parameters={{ attraction: 1, largest_component: false, simple_ui: true }}
/>
</div>
</Col>
</Row>
)}
<Button
className="fr-btn fr-btn--tertiary fr-btn--icon-right fr-icon-download-line"
onClick={() => exportJson(graphEncodeToJson(data.graph[selectedOption]))}
onClick={() => exportJson(graphEncodeToJson(data.graph[selectedGraph]))}
>
Download graph
</Button>
Expand Down

0 comments on commit 2d358c9

Please sign in to comment.