Extract ActionsDropDown from Entity
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 04 Apr 2017 16:45:05 +0200
changeset 78 9463a36b693b
parent 77 956d78e0d931
child 79 1070e0148bd8
Extract ActionsDropDown from Entity
src/components/BaseViews.js
src/components/Entity.js
--- a/src/components/BaseViews.js	Tue Apr 04 16:31:05 2017 +0200
+++ b/src/components/BaseViews.js	Tue Apr 04 16:45:05 2017 +0200
@@ -1,6 +1,8 @@
 import React from 'react';
 import {Link} from 'react-router';
-import {PropTypeAction, PropTypesEntityModel, PropTypesEntitiesModel} from '../model';
+import {isEmpty} from 'lodash/lang';
+import {PropTypeAction, PropTypesEntitiesModel, PropTypesResourceModel} from '../model';
+import {appendPath} from '../utils';
 
 export function OneLineView({entity}) {
     return (
@@ -38,17 +40,33 @@
     entities: PropTypesEntitiesModel.isRequired,
 };
 
+export function ActionsDropDown({target}) {
+    const actions = target.allowedActions;
+    if (isEmpty(actions)) {
+        return null;
+    }
+    return (
+        <DropDownButton title="actions" style="pull-right">
+            { actions.map(action => <ActionLink target={target} action={action} key={action} />) }
+        </DropDownButton>
+    );
+}
+
+ActionsDropDown.propTypes = {
+    target: PropTypesResourceModel.isRequired,
+}
+
 export function ActionLink({target, action}) {
     let {route} = target;
     switch (action) {
         case 'delete':
-            route += '/delete';
+            route = appendPath(route, '/delete');
             break;
         case 'edit':
-            route += '/edit';
+            route = appendPath(route, '/edit');
             break;
         case 'create':
-            route += '/new';
+            route = appendPath(route, '/new');
             break;
         default:
             return null;
@@ -58,7 +76,7 @@
 
 ActionLink.propTypes = {
     action: PropTypeAction.isRequired,
-    target: PropTypesEntityModel.isRequired,
+    target: PropTypesResourceModel.isRequired,
 };
 
 export function DropDownButton(props) {
--- a/src/components/Entity.js	Tue Apr 04 16:31:05 2017 +0200
+++ b/src/components/Entity.js	Tue Apr 04 16:45:05 2017 +0200
@@ -8,7 +8,7 @@
 import {wrapEntityData, PropTypeJsonaryWrapper} from '../jsonaryutils'
 import {PropTypesEntityModel} from '../model';
 import {buildFormData} from '../utils';
-import {ActionLink, DropDownButton, OneLineView} from './BaseViews';
+import {ActionsDropDown, OneLineView} from './BaseViews';
 import {WorkflowActions} from './Workflow';
 
 function EntityAttribute({attribute}) {
@@ -241,15 +241,7 @@
     }
 
     renderActions() {
-        const actions = this.props.entity.allowedActions;
-        if (isEmpty(actions)) {
-            return null;
-        }
-        return (
-            <DropDownButton title="actions" style="pull-right">
-                { actions.map(action => <ActionLink target={this.props.entity} action={action} key={action} />) }
-            </DropDownButton>
-        );
+        return <ActionsDropDown target={this.props.entity} />;
     }
 
     renderAttributes() {