2019-12-04 18:39:57 +00:00
|
|
|
|
import React from 'react';
|
2019-12-03 22:00:21 +00:00
|
|
|
|
import Graph from './Graph.js';
|
|
|
|
|
import {types} from '../data/mock.js';
|
|
|
|
|
import {useAsync} from '../util.js';
|
|
|
|
|
import {symptomsSubgraph} from '../fetch.js';
|
|
|
|
|
|
2019-12-04 04:20:06 +00:00
|
|
|
|
const DiseaseGraph = ({terms, setTerms, results}) =>
|
2019-12-03 22:00:21 +00:00
|
|
|
|
{
|
|
|
|
|
const {nodes, edges} = useAsync({
|
|
|
|
|
nodes: {},
|
|
|
|
|
edges: []
|
|
|
|
|
}, symptomsSubgraph, results);
|
|
|
|
|
|
2019-12-04 04:20:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Rendu d’un nœud du graphe.
|
|
|
|
|
*
|
|
|
|
|
* @param id Identifiant du nœud à afficher.
|
|
|
|
|
*/
|
2019-12-03 22:00:21 +00:00
|
|
|
|
const render = id =>
|
|
|
|
|
{
|
|
|
|
|
const isTerm = terms.some(({id: termId}) => termId === id);
|
|
|
|
|
const isDisease = nodes[id].types.includes(types.disease);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span className={[
|
|
|
|
|
'SearchResults_result',
|
|
|
|
|
isTerm ? 'SearchResults_result-term' : '',
|
|
|
|
|
isDisease ? 'SearchResults_result-disease' : '',
|
|
|
|
|
].join(' ')}>{nodes[id].name}</span>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-04 04:20:06 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gère le clic sur un nœud du graphe.
|
|
|
|
|
*
|
|
|
|
|
* @param id Identifiant du nœud cliqué.
|
|
|
|
|
*/
|
|
|
|
|
const handleNodeClick = id =>
|
|
|
|
|
{
|
|
|
|
|
const result = results.find(({id: termId}) => termId === id);
|
|
|
|
|
const termIndex = terms.findIndex(({id: termId}) => termId === id);
|
|
|
|
|
|
|
|
|
|
if (result !== undefined)
|
|
|
|
|
{
|
|
|
|
|
if (termIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
// Retrait d’un terme déjà dans la requête
|
|
|
|
|
setTerms([
|
|
|
|
|
...terms.slice(0, termIndex),
|
|
|
|
|
...terms.slice(termIndex + 1)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Ajout d’un nouveau terme dans la requête
|
|
|
|
|
setTerms(terms.concat([result]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-03 22:00:21 +00:00
|
|
|
|
return (
|
|
|
|
|
<Graph
|
|
|
|
|
nodes={Object.keys(nodes)}
|
|
|
|
|
edges={edges}
|
2019-12-04 04:20:06 +00:00
|
|
|
|
emptyMessage="Aucune maladie ne corresond à ces symptômes"
|
2019-12-03 22:00:21 +00:00
|
|
|
|
render={render}
|
2019-12-04 04:20:06 +00:00
|
|
|
|
onNodeClick={handleNodeClick}
|
2019-12-03 22:00:21 +00:00
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DiseaseGraph;
|