Let ActionLink take url of the target as prop
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 04 May 2017 17:57:10 +0200
changeset 182 353a3e2767ab
parent 181 6aa82b1bd05e
child 183 faaa51e8a501
Let ActionLink take url of the target as prop
src/components/BaseViews.js
test/index.js
--- a/src/components/BaseViews.js	Thu May 04 17:37:10 2017 +0200
+++ b/src/components/BaseViews.js	Thu May 04 17:57:10 2017 +0200
@@ -46,7 +46,7 @@
     }
     return (
         <DropDownButton title="actions" style="pull-right">
-            { actions.map(action => <ActionLink target={target} action={action} key={action} />) }
+            { actions.map(action => <ActionLink targetUrl={target.url} action={action} key={action} />) }
         </DropDownButton>
     );
 }
@@ -56,27 +56,27 @@
     ignore: PropTypes.arrayOf(PropTypes.string),
 };
 
-export function ActionLink({target, action}) {
-    let {route} = target;
+export function ActionLink({targetUrl, action}) {
+    let url = '';
     switch (action) {
         case 'delete':
-            route = appendPath(route, '/delete');
+            url = appendPath(targetUrl, '/delete');
             break;
         case 'edit':
-            route = appendPath(route, '/edit');
+            url = appendPath(targetUrl, '/edit');
             break;
         case 'create':
-            route = appendPath(route, '/new');
+            url = appendPath(targetUrl, '/new');
             break;
         default:
             return null;
     }
-    return <Link to={route} title={action} >{action}</Link>;
+    return <Link to={url} title={action} >{action}</Link>;
 }
 
 ActionLink.propTypes = {
     action: PropTypeAction.isRequired,
-    target: PropTypesResourceModel.isRequired,
+    targetUrl: PropTypes.string.isRequired,
 };
 
 export function DropDownButton(props) {
--- a/test/index.js	Thu May 04 17:37:10 2017 +0200
+++ b/test/index.js	Thu May 04 17:57:10 2017 +0200
@@ -201,15 +201,13 @@
 });
 
 describe('<ActionLink />', () => {
-    let targetResource;
+    let targetUrl;
     beforeEach(() => {
-        targetResource = {
-            route: "/any/123",
-        };
+        targetUrl = "/any/123";
     });
 
     it('given a "create" action, it renders a link pointing to "create" view', () => {
-        const wrapper = shallow(<ActionLink target={targetResource} action='create'/>);
+        const wrapper = shallow(<ActionLink targetUrl={targetUrl} action='create'/>);
 
         expect(wrapper.is('Link')).to.be.equal(true);
         expect(wrapper.prop('to')).to.be.equal('/any/123/new');
@@ -217,7 +215,7 @@
     });
 
     it('given an "edit" action, it renders a link pointing to "edit" view', () => {
-        const wrapper = shallow(<ActionLink target={targetResource} action='edit'/>);
+        const wrapper = shallow(<ActionLink targetUrl={targetUrl} action='edit'/>);
 
         expect(wrapper.is('Link')).to.be.equal(true);
         expect(wrapper.prop('to')).to.be.equal( '/any/123/edit');
@@ -225,7 +223,7 @@
     });
 
     it('given a "delete" action, it renders a link pointing to "delete" view', () => {
-        const wrapper = shallow(<ActionLink target={targetResource} action='delete'/>);
+        const wrapper = shallow(<ActionLink targetUrl={targetUrl} action='delete'/>);
 
         expect(wrapper.is('Link')).to.be.equal(true);
         expect(wrapper.prop('to')).to.be.equal( '/any/123/delete');
@@ -233,7 +231,7 @@
     });
 
     it('given an unknown action, it renders nothing', () => {
-        const wrapper = shallow(<ActionLink target={targetResource} action='foo'/>);
+        const wrapper = shallow(<ActionLink targetUrl={targetUrl} action='foo'/>);
 
         expect(wrapper.html()).to.be.equal(null);
     });