RelatedEntities don't rely on etype, eid, rtype
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 28 Apr 2017 15:24:59 +0200
changeset 155 12900847264e
parent 154 b2335ad43c16
child 156 7989ac4a50d3
RelatedEntities don't rely on etype, eid, rtype
src/components/Entity.js
--- a/src/components/Entity.js	Fri Apr 28 15:30:56 2017 +0200
+++ b/src/components/Entity.js	Fri Apr 28 15:24:59 2017 +0200
@@ -80,15 +80,14 @@
     // XXX This is very similar to Entities component...
     constructor(props) {
         super(props);
-        this.state = {related: [],
-                      etype: this.props.etype,
-                      eid: this.props.eid,
-                      rtype: this.props.rtype};
+        this.state = {
+            related: [],
+            link: props.link,
+        };
     }
 
     componentDidMount() {
-        const {etype, eid, rtype} = this.state;
-        Api.getRelated(etype, eid, rtype)
+        HypermediaClient.getResource(this.state.link.href)
             .then(entities => this.setState({related: entities}))
             .catch(error => {
                 console.error(`error on RelatedEntities  ${error}`);
@@ -96,14 +95,12 @@
     }
 
     componentWillReceiveProps(nextProps) {
-        this.setState({etype: nextProps.etype,
-                       eid: nextProps.eid,
-                       rtype: nextProps.rtype});
+        this.setState({link: nextProps.link});
         this.componentDidMount();
     }
 
     renderTitle() {
-        const title = this.props.rtype;
+        const title = this.state.link.title;
         return (
             <div>
                 { title }
@@ -148,9 +145,7 @@
 }
 
 RelatedEntities.propTypes = {
-    etype: PropTypes.string.isRequired,
-    eid: PropTypes.string.isRequired,
-    rtype: PropTypes.string.isRequired,
+    link: PropTypes.object.isRequired,
 };
 
 export class Entity extends React.Component {
@@ -292,9 +287,8 @@
     renderRelated() {
         const links = this.props.entity.data.links('related');
         if (links.length > 0) {
-            const {etype, eid} = this.props.entity;
             return Array.prototype.map.call(links, (link) => {
-                return <RelatedEntities key={link.title} etype={etype} eid={eid} rtype={link.title}/>
+                return <RelatedEntities key={link.title} link={link} />
             });
         }
         return null;